Saturday, June 25, 2016

Brightness and Contrast Control Using OpenCV

Posted by Md. Hanif Ali Sohag on Saturday, June 25, 2016 in | No comments
Brightness and Contrast Controlling
In this tutorial we will learn how to change the brightness and contrast of an image using OpenCV.

Operation:
Code:


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;


double alpha; /**< Simple contrast control */
int beta; /**< Simple brightness control */

int main()
{
	/// Read image given by user
	Mat image = imread("c://test//image1.jpg", CV_LOAD_IMAGE_COLOR);
	Mat new_image = Mat::zeros(image.size(), image.type());

	/// Initialize values
	cout << " Basic Linear Transforms " << endl;
	cout << " ------------------------- " << endl;
	cout << " * Enter the alpha value [1.0-3.0]: ";
	cin >> alpha;
	cout << " * Enter the beta value [0-100]: ";
	cin >> beta;

	/// Do the operation new_image(i,j) = alpha*image(i,j) + beta

	for (int y = 0; y < image.rows; y++)
	{
		for (int x = 0; x < image.cols; x++)
		{
			for (int c = 0; c < 3; c++)
			{
				new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
			}
		}
	}
		/// Create Windows
		namedWindow(" Original Image", 1);
		namedWindow(" New Image", 1);
		/// Show stuff
		imshow(" Original Image", image);
		imshow(" New Image", new_image);
		/// Wait until user press some key
		waitKey();
		return 0;
	
}

Note:

Output:

Brightness and Contrast Control

0 comments:

Post a Comment