Change Contrast of Image or Video


Changing the contrast is also a point operation on each pixel. The easiest way to increase the contrast of an image is, multiplying each pixel value by a number larger than 1.

                         new_img (i, j) = img(i, j) * c                     c > 1


The easiest way to decrease the contrast is, multiplying each pixel value by a number smaller than 1.

                         new_img (i, j) = img(i, j) * c                     c < 1



There are more advance methods to adjust contrast of an image such as histogram equalization. Such method adjust the contrast of an image such that color distribution is balanced equally. We will discuss the histogram equalization in the next lesson.


e.g- Say, this is your original image
Example Image
Example Image



By multiplying each pixel value by 2, you can effectively double the contrast of an image. Here is the image of which the contrast is increased. 
I have considered this image as a 8 bit unsigned image. So, any pixel value should be from 0 to 255. If the resulting image has values more than 255, it should be rounded off to 255.


Contrast Increased
Contrast Increased


By multiplying each pixel value by 0.5, you can effectively halve the contrast of an image. Here is the image of which contrast is decreased.


Contrast Decreased
Contrast Decreased


Change Contrast of an Image

How to increase or decrease the contrast of an image is demonstrated in the following OpenCV C++ example. Keep in mind that this is the very basic way of changing contrast. In the next lesson, I'll show you how to change the contrast using histogram equalization.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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); //open and read the image


if (img.empty())
{
cout << "Image cannot be loaded..!!" << endl;
return -1;
}

Mat imgH;
img.convertTo(imgH, -1, 2, 0); //increase the contrast (double)

Mat imgL;
img.convertTo(imgL, -1, 0.5, 0); //decrease the contrast (halve)

//create windows
namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("High Contrast", CV_WINDOW_AUTOSIZE);
namedWindow("Low Contrast", CV_WINDOW_AUTOSIZE);

//show the image
imshow("Original Image", img);
imshow("High Contrast", imgH);
imshow("Low Contrast", imgL);

waitKey(0); //wait for key press

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 contrast is increased by the OpenCV program.
Contrast is increased with OpenCV
Contrast is increased with OpenCV


Here is the image of which contrast is decreased by the OpenCV program.

Contrast is decreased with OpenCV
Contrast is decreased with OpenCV

New OpenCV methods

  • void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 )
This OpenCV function converts  image into another format with scaling. Scaling is done according to the following formula.
                                              m[i,j] = alfa * img[i,j] + beta

Here is the parameters of this function
    • OutputArray m - Store the converted image
    • int rtype - Depth of the output image. If the value of rtype is negative, output type is same as the input image. I have used a negative value in the above program because I don't want to change the depth of the original image. Possible inputs to this parameter
      • CV_8U
      • CV_32S
      • CV_64F
      • -1
Complete list of depths can be found in Basics of OpenCV API
    • double alpha - Multiplication factor; Every pixel will be multiplied by this value
    • double beta - This value will be added to very pixels after multiplying with the above value.
Here is the formula again. Here m[i, j] means a pixel at ith row and jth column.
                              m[i,j] = alfa * img[i,j] + beta


Change the Contrast of a Video

It is similar to the above program except that you have to change the contrast for each and every frame of the video. Here is the example OpenCV program.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
    }

    //create windows
    namedWindow("Original Video",CV_WINDOW_AUTOSIZE); 
    namedWindow("Contrast Increased",CV_WINDOW_AUTOSIZE); 
    namedWindow("Contrast Decreased",CV_WINDOW_AUTOSIZE); 

    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.convertTo(imgH, -1, 2, 0); //increase the contrast (double)

Mat imgL;
frame.convertTo(imgL, -1, 0.5, 0); //decrease the contrast (halve)

//show the image
        imshow("Original Video", frame); 
imshow("Contrast Increased", imgH); 
imshow("Contrast Decreased", imgL); 

       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)

All the OpenCV methods in the above example program have been discussed in previous lessons. So, I am not going to repeat them again.





0 Response to "Change Contrast of Image or Video"

Post a Comment