. Changing the contrast and brightness of an image

#include “opencv2/imgcodecs.hpp”
#include “opencv2/highgui.hpp”
#include
#include <stdlib.h>

using namespace cv;

// we’re NOT “using namespace std;” here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;

int main(void)
{
Mat image = imread("/home/chang/projects/opencv_GPU_example/1.jpg");
if(image.empty())
{
cout << “Could not open or find the image!\n” << endl;
return -1;
}

Mat new_image = Mat::zeros(image.size(), image.type());
double alpha = 1.0;  /* < Simple contrst control */
int beta = 0;        /* < Simple brightness control */

cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: ";

// cin >> alpha;
alpha = 2.0;
cout << "* Enter the beta value [0-100]: ";
// cin >> beta;
beta = 66;

for(int y = 0; y < image.rows; y++)
{
	for(int x = 0; x < image.cols; x++)
	{
		for(int c = 0; c < image.channels(); c++)
		{
			new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*image.at<Vec3b>(y, x)[c] + beta);
		}
	}
}

imshow("Original Image", image);
imshow("New Image", new_image);
waitKey(0);

return 0;

}

下面方法更快:
#include “opencv2/imgcodecs.hpp”
#include “opencv2/highgui.hpp”
#include
#include <stdlib.h>

using namespace cv;

// we’re NOT “using namespace std;” here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;

int main(void)
{
Mat image = imread("/home/chang/projects/opencv_GPU_example/1.jpg");
if(image.empty())
{
cout << “Could not open or find the image!\n” << endl;
return -1;
}

Mat new_image = Mat::zeros(image.size(), image.type());
double alpha = 1.0;  /* < Simple contrst control */
int beta = 0;        /* < Simple brightness control */

cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: ";

// cin >> alpha;
alpha = 2.0;
cout << "* Enter the beta value [0-100]: ";
// cin >> beta;
beta = 66;

image.convertTo(new_image, -1, alpha, beta);

imshow("Original Image", image);
imshow("New Image", new_image);
waitKey(0);

return 0;

}

发布了43 篇原创文章 · 获赞 0 · 访问量 404

猜你喜欢

转载自blog.csdn.net/weixin_42505877/article/details/103940525