Changing brightness is a point operation on each pixel. If you want to increase the brightness, you have to add some constant value to each and every pixel.
new_img (i, j) = img(i, j) + c
If you want to decrease the brightness, you have to subtract some constant value from each and every pixel.
new_img (i, j) = img(i, j) - c
e.g- Say, this is your original image
Original Image |
Say, you want to increase the brightness of the image by 20 units. Here is the output image of which the brightness is increased by 20 units.
Image of which brightness is increased |
Say, you want to decrease the brightness of the image by 20 units. Here is the output image of which the brightness is decreased by 20 units.
Image of which brightness is decreased |
Note :
You may already notice that although the 1st pixel of the above image should have (12 - 20) = -8, I have put 0. It is because pixels never have negative values. Any pixel value is bounded below by 0 and bounded above by 2^(bit depth).
Change the Brightness of an Image
Now I am going to show you how to increase or decrease brightness of an image using an OpenCV C++ example.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_COLOR);
if (img.empty())
{
cout << "Image cannot be loaded..!!" << endl;
return -1;
}
Mat imgH = img + Scalar(75, 75, 75); //increase the brightness by 75 units
//img.convertTo(imgH, -1, 1, 75);
Mat imgL = img + Scalar(-75, -75, -75); //decrease the brightness by 75 units
//img.convertTo(imgL, -1, 1, -75);
namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("High Brightness", CV_WINDOW_AUTOSIZE);
namedWindow("Low Brightness", CV_WINDOW_AUTOSIZE);
imshow("Original Image", img);
imshow("High Brightness", imgH);
imshow("Low Brightness", imgL);
waitKey(0);
destroyAllWindows(); //destroy all open windows
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)
Here is the original image.
original image |
Here is the image of which brightness is increased by the OpenCV program.
Brightness is increased with OpenCV |
Here is the image of which brightness is decreased by the OpenCV program.
Brightness is decreased with OpenCV |
New OpenCV functions
- Mat imgH = img + Scalar(75, 75, 75);
This line of code adds 75 to each and every pixel in the 3 channels (B, G, R channels) of 'img'. Then it assigns this new image to 'imgH'.
Instead you can use this function also.
img.convertTo(imgH, -1, 1, 75);
Instead you can use this function also.
img.convertTo(imgH, -1, 1, 75);
- Mat imgL = img + Scalar(-75, -75, -75);
This line of code subtracts 75 from each and every pixel in the 3 channels (B, G, R channels) of 'img'. Then it assigns this new image to'imgL'.
Instead you can use this function also.
img.convertTo(imgL, -1, 1, -75);
Instead you can use this function also.
img.convertTo(imgL, -1, 1, -75);
Change the Brightness of a Video
Now I am going to show you how to increase or decrease the brightness of a video using an OpenCV C++ example. This is pretty much similar to the previous example.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap("C:/Users/SHERMAL/Desktop/SampleVideo.wmv"); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
namedWindow("Original Video",CV_WINDOW_AUTOSIZE); //create a window called "Original Video"
namedWindow("Brightness Increased",CV_WINDOW_AUTOSIZE); //create a window called "Brightness Increased"
namedWindow("Brightness Decreased",CV_WINDOW_AUTOSIZE); //create a window called "Brightness Decreased"
while(1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
Mat imgH = frame + Scalar(50, 50, 50); //increase the brightness by 75 units
Mat imgL = frame + Scalar(-50, -50, -50); //decrease the brightness by 75 units
imshow("Original Video", frame); //show the frame in "Original Video" window
imshow("Brightness Increased", imgH); //show the frame of which brightness increased
imshow("Brightness Decreased", imgL); //show the frame of which brightness decreased
if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)
No comments:
Post a Comment