Change Contrast of an Image

Steps to Change Contrast of an Image | OpenCV with Visual Studio in Windows 10. In this Tutorial, we are going to increase and decrease contrast 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 Contrast 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 Contrast Effect
     Mat imgInc;
     img.convertTo(imgInc, -1, 2, 0);  //increase (double) contrast 
    
     Mat imgDec;
     img.convertTo(imgDec, -1, 0.5, 0); //decrease (halve) contrast
    
     namedWindow("Inc contrast", CV_WINDOW_AUTOSIZE);
     namedWindow("Dec contrast", CV_WINDOW_AUTOSIZE);
    
     imshow("Inc contrast", imgInc);
     imshow("Dec contrast", imgDec);
    
     //Wait Key press
     cvWaitKey(0);
    
     //destroy windows
     destroyAllWindows();
     return 0;
    }
    
    OR
    Get ChangeContrast.cpp from Github:
    Download ChangeContrast.cpp

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

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


Explaination :
Function used:
img.convertTo(imgInc, -1, 2, 0) - This function can process images and increase(double) contrast.
img.convertTo(imgDec, -1, 0.5, 0) - This function can process images and decrease(halve) contrast.
  • 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 Brightness of an Image


  • 1 comment: