Change Brightness of an Image

Steps to Change Brightness of an Image | OpenCV with Visual Studio in Windows 10. In this Tutorial, we are going to increase and decrease brightness of an image using OpenCV.
  1. First of all, Follow this tutorial to Install & Configure OpenCV with Visual Studio 2015

  2. Copy the Full Source Code to Change Brightness of an Image from here:
  3. #include<opencv2/highgui/highgui.hpp>
    
    using namespace cv;
    
    int main()
    {
     //Load an Image
     Mat img = imread("C:/Briefcase/Chess.jpg", CV_LOAD_IMAGE_COLOR);
     namedWindow("Image", CV_WINDOW_AUTOSIZE);
     imshow("Image", img);
    
     //Change Brightness Effect
     Mat imgInc;
     img.convertTo(imgInc, -1, 1, 25);  //increase brightness by 25 units    
    
     Mat imgDec;
     img.convertTo(imgDec, -1, 1, -25);  //decrease brightness by 25 units
    
     namedWindow("Inc Brightness", CV_WINDOW_AUTOSIZE);
     namedWindow("Dec Brightness", CV_WINDOW_AUTOSIZE);
    
     imshow("Inc Brightness", imgInc);
     imshow("Dec Brightness", imgDec);
    
     //Wait Key press
     cvWaitKey(0);
    
     //destroy windows
     destroyAllWindows();
     return 0;
    }
    
    OR
    Get ChangeBrightness.cpp from Github:
    Download ChangeBrightness.cpp

  4. Paste full source code and Run it (Ctrl+F5 or F5).

OUTPUT
Load an Image OpenCV
Load Image OpenCV
Increase Brightness of an Image
Increase Brightness of an Image - OpenCV
Decrease Brightness of an Image
Decrease Brightness of an Image - OpenCV


Explaination :
Function used: 
img.convertTo(imgInc, -1, 1, 25) - This function can process images and increase brightness by 25 units.
img.convertTo(imgDec, -1, 1, -25) - This function can process images and decrease brightness by 25 units.
  • 1st parameter is the output matrix.
  • 2nd parameter is Depth of the output image. If rtype is negative, output type is same as the input type.
  • 3rd parameter is the multiplication or scaling factor, every pixel will be multiplied by this value.
  • 4th parameter is delta factor added to the scaled values.


Like, Share and Comment Below. You may also like this:


  • Change Contrast of an Image


  • No comments:

    Post a Comment