Life2Coding
Brightness and Contrast Control Using OpenCV

Brightness and Contrast Controlling

An image must have the proper brightness and contrast for easy viewing. Brightness refers to the overall lightness or darkness of the image. Contrast is the difference in brightness between objects or regions. For example, a white rabbit running across a snowy field has poor contrast, while a black dog against the same white background has good contrast.

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:
c Brightness and Contrast Control Using OpenCV
Brightness and Contrast Control

 

life2coding_icon [] Brightness and Contrast Control Using OpenCV

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.