Change Brightness of Image or Video

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
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
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
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
original image

Here is the image of which brightness is increased by the OpenCV program.
Brightness is increased with OpenCV
Brightness is increased with OpenCV

Here is the image of which brightness is decreased by the OpenCV program.
Brightness is decreased with OpenCV
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);


  • 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);



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)






8 comments:

  1. nicely explained, but if we want to change the brightness of few particular pixel then how do we do it?

    ReplyDelete
    Replies
    1. Agreed, nicely explained tutorials. You can have a look at the similar tutorials byt the opencv website's tutorial section for more help.
      http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html#basic-linear-transform

      Delete
  2. What do the -1 and +1 in img.convertTo(imgH, -1, 1, 75); represent ?

    ReplyDelete
    Replies
    1. http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=convertto#void%20Mat::convertTo%28OutputArray%20m,%20int%20rtype,%20double%20alpha,%20double%20beta%29%20const foud the ans .. thanks

      Delete
  3. Congratulations for the work.
    Helped me a lot.
    Sorry for my english.
    I'm from Brazil.

    ReplyDelete
  4. Great tutorial, congrats and thaks a lot

    ReplyDelete
  5. Thanks a lot for the tutorials and simple and elaborate explanation. It helped a lot. I had a doubt as to how to access each element or pixel in a colour image or 3 channel image. Can you please guide me in this?

    ReplyDelete
  6. Don't you think it's a waste of time as it only talks how to do miracle by calling some functions already written inside? It would have been a better guide if you would have talked about how to play with pixels, set of pixels, blocks, rows, columns, etc. so that serious learners could develop their own.

    ReplyDelete