Brightness means to change the value of each and every image pixel. This change can be done by either increasing or decreasing the pixel values of the image, by any constant. This article gives an in-depth knowledge about how can an image brightness be changed using OpenCV.
Input : Original Image Output : -> Original Image -> Image with brightness increased by 100 -> Image with brightness increased by 50 -> Image with brightness decreased by 100 -> Image with brightness decreased by 50
Code : CPP code to manipulate the image brightness
// c++ code explaining how to // increase or decrease the brightness of // an image // loading library files #include <highlevelmonitorconfigurationapi.h> #include <opencv2\highgui\highgui.hpp> #include <opencv2\opencv.hpp> using namespace cv;
using namespace std;
int main( int argc, char ** argv)
{ // Loading the Image File under testing
Mat image = imread( "C:\\Users\\dell\\Desktop\\abc.jpg" );
// Check whether the image is present or not
if (image.empty()) {
cout << "Could not open or find the image" << endl;
// waiting for any key to be pressed
return -1;
}
// Declaring the Brightness Instances
Mat imageBrighnessHigh50;
// increasing the brightness level by 50
image.convertTo(imageBrighnessHigh50, -1, 1, 50);
Mat imageBrighnessHigh100;
// increasing the brightness level by 100
image.convertTo(imageBrighnessHigh100, -1, 1, 100);
Mat imageBrighnessLow50;
// decreasing the brightness level by 50
image.convertTo(imageBrighnessLow50, -1, 1, -50);
Mat imageBrighnessLow100;
// decreasing the brightness level by 100
image.convertTo(imageBrighnessLow100, -1, 1, -100);
// Declaring the windows
// for images belonging to different brightness level
String windowNameOriginalImage = "Original Image" ;
String windowNameBrightnessHigh50 = "Brightness Increased by 50" ;
String windowNameWithBrightnessHigh100 = "Brightness Increased by 100" ;
String windowNameBrightnessLow50 = "Brightness Decreased by 50" ;
String windowNameBrightnessLow100 = "Brightness Decreased by 100" ;
// Running the window instance
// and opening it
namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameBrightnessHigh50, WINDOW_NORMAL);
namedWindow(windowNameWithBrightnessHigh100, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow50, WINDOW_NORMAL);
namedWindow(windowNameBrightnessLow100, WINDOW_NORMAL);
// Loading images inside the above created Windows
imshow(windowNameOriginalImage, image);
imshow(windowNameBrightnessHigh50, imageBrighnessHigh50);
imshow(windowNameWithBrightnessHigh100, imageBrighnessHigh100);
imshow(windowNameBrightnessLow50, imageBrighnessLow50);
imshow(windowNameBrightnessLow100, imageBrighnessLow100);
// waiting for any key to be pressed
waitKey(0);
// closing all the windows instances
// when any key is pressed.
destroyAllWindows();
return 0;
} |
Input :
Output :
Original Image
Brightness Level Increased by 50
Brightness Level Increased by 100
Brightness Level Decreased by 50
Brightness Level Decreased by 100
Explanation :
// Declaring the Brightness Instances Mat imageBrighnessHigh50; // increasing the brightness level by 50 image.convertTo(imageBrighnessHigh50, -1, 1, 50); Mat imageBrighnessHigh100; // increasing the brightness level by 100 image.convertTo(imageBrighnessHigh100, -1, 1, 100); Mat imageBrighnessLow50; // decreasing the brightness level by 50 image.convertTo(imageBrighnessLow50, -1, 1, -50); Mat imageBrighnessLow100; // decreasing the brightness level by 100 image.convertTo(imageBrighnessLow100, -1, 1, -100); |
These code lines will change the image pixel values by a specified amount. The final changed image is then stored to the given output image. The brightness level will increase if the specified amount is positive (as above is 50), otherwise will decrease if the specified amount is negative.
MAT Function :
The “MAT” function changes each image pixel value to the target data type and the values are changed as per increase or decrease.
Parameters : m : Output Image rtype : Output Image type, Output Image type is same as input if it is set to -ve value alpha : Input image pixel are multiplied with this number prior to its assignment to output image beta : adding this value to each input image pixel