Change Contrast

Changing contrast of an image is also a commonly used point operation. In this operation, the value of each and every pixels in an image should be multiplied by a positive constant which is not equal to one. To change the contrast of a video, the same operation should be performed on each frame in the video.

In order to increase the contrast of an image, each and every pixel in the image should be multiplied by a positive constant larger that one.

new_image (i, j) = image(i, j) * c                    c > 1



In order to decrease the contrast of an image, each and every pixel in the image should be multiplied by a positive constant smaller that one.

new_image (i, j) = image(i, j) * c                0 < c < 1



There are more advance methods to enhance the contrast of an image such as histogram equalization. It enhances the contrast of an image such that the intensity distribution is balanced equally in the available space. We will discuss the histogram equalization in the next lesson.


e.g- Say, this is your original image. Let's assume the data type of the image is CV_8U. (i.e. - Pixels in the image is 8 bit unsigned. Please refer OpenCV C++ API for more information.) Hence the valid value range of every pixels in the image should be 0 - 255. 

Original Image
Original Image



Say, you want to increase the contrast of the original image by a factor of 2. Therefore you should multiply each pixels in the original image by 2. You must make sure that pixel values in the output image should not exceed the maximum allowable limit after the multiplication. If it exceeds the maximum limit, you must assign the maximum value instead of the correct value.

Here is the image of which the contrast is increased by a factor of 2. You may have already noticed that the pixel value at (0, 0) location is 255, although it should be 288 after multiplying 144 by 2. It is because the maximum allowable pixel value of this image is 255.


Image of which the contrast is increased by factor of 2
Image of which the contrast is increased by factor of 2


Say, you want to halve the contrast of the original image. Therefore you should multiply each pixels in the original image by 0.5. Here is the image of which the contrast is decreased.


Image of which the contrast is decreased
Image of which the contrast is decreased



Change the Contrast of an Image with OpenCV


Now I am going to show you how to increase and decrease the contrast of an image using an OpenCV C++ example.

It is recommended to go through the Load & Display Image first in order to understand the following example better.
//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // Read the image file
    Mat image = imread("D:/My OpenCV Website/Christmas.jpg");

    // Check for failure
    if (image.empty())
    {
        cout << "Could not open or find the image" << endl;
        cin.get(); //wait for any key press
        return -1;
    }

    Mat imageContrastHigh2;
    image.convertTo(imageContrastHigh2, -1, 2, 0); //increase the contrast by 2

    Mat imageContrastHigh4;
    image.convertTo(imageContrastHigh4, -1, 4, 0); //increase the contrast by 4

    Mat imageContrastLow0_5;
    image.convertTo(imageContrastLow0_5, -1, 0.5, 0); //decrease the contrast by 0.5

    Mat imageContrastLow0_25;
    image.convertTo(imageContrastLow0_25, -1, 0.25, 0); //decrease the contrast by 0.25


    //Defining window names for above images
    String windowNameOriginalImage = "Original Image";
    String windowNameContrastHigh2 = "Contrast Increased by 2";
    String windowNameContrastHigh4 = "Contrast Increased by 4";
    String windowNameContrastLow0_5 = "Contrast Decreased by 0.5";
    String windowNameContrastLow0_25 = "Contrast Decreased by 0.25";

    //Create and open windows for above images
    namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
    namedWindow(windowNameContrastHigh2, WINDOW_NORMAL);
    namedWindow(windowNameContrastHigh4, WINDOW_NORMAL);
    namedWindow(windowNameContrastLow0_5, WINDOW_NORMAL);
    namedWindow(windowNameContrastLow0_25, WINDOW_NORMAL);

    //Show above images inside the created windows.
    imshow(windowNameOriginalImage, image);
    imshow(windowNameContrastHigh2, imageContrastHigh2);
    imshow(windowNameContrastHigh4, imageContrastHigh4);
    imshow(windowNameContrastLow0_5, imageContrastLow0_5);
    imshow(windowNameContrastLow0_25, imageContrastLow0_25);

    waitKey(0); // Wait for any key stroke

    destroyAllWindows(); //destroy all open windows

    return 0;
}
Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/Christmas.jpg" in the code with a valid location to an image in your computer. Then you should see a set of images like the below.

Images of which contrast is increased
Contrast Increased

Images of which the contrast is decreased
Contrast Decreased


Explanation


Let's go through the above example line by line.

// Read the image file
Mat image = imread("D:/My OpenCV Website/Christmas.jpg");

// Check for failure
if (image.empty())
{
    cout << "Could not open or find the image" << endl;
    cin.get(); //wait for any key press
    return -1;
}
This code segment loads an image from the specified file. If it is failed to load the image, the program will exit.



Mat imageContrastHigh2;
image.convertTo(imageContrastHigh2, -1, 2, 0); //increase the contrast by 2

Mat imageContrastHigh4;
image.convertTo(imageContrastHigh4, -1, 4, 0); //increase the contrast by 4

Mat imageContrastLow0_5;
image.convertTo(imageContrastLow0_5, -1, 0.5, 0); //decrease the contrast by 0.5

Mat imageContrastLow0_25;
image.convertTo(imageContrastLow0_25, -1, 0.25, 0); //decrease the contrast by 0.25
The above code segment will multiply pixel values by the specified amount and store it in the given output image. If the specified value is larger than one, the contrast of the output image will be increased. If the specified value is smaller than one, the contrast of the output image will be decreased.

void Mat::convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const

This function converts the each pixel value to the target data type and changes the value as per the following formula.

 pixel_value_of_output_image(x, y) = pixel_value_of_input_image(x, y) * alpha + beta;
  1. m - Output image. This data structure will be reallocated if required.

  2. rtype - Type of the output image. If rtype is a negative value, the type of the output image will be same as the input image.

  3. alpha - Each pixels in the input image will be multiplied by this number before assigning to the output image.

  4. beta - This value will be added to each pixels in the input image and assigned to the output image.


//Defining window names for above images
String windowNameOriginalImage = "Original Image";
String windowNameContrastHigh2 = "Contrast Increased by 2";
String windowNameContrastHigh4 = "Contrast Increased by 4";
String windowNameContrastLow0_5 = "Contrast Decreased by 0.5";
String windowNameContrastLow0_25 = "Contrast Decreased by 0.25";

//Create and open windows for above images
namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameContrastHigh2, WINDOW_NORMAL);
namedWindow(windowNameContrastHigh4, WINDOW_NORMAL);
namedWindow(windowNameContrastLow0_5, WINDOW_NORMAL);
namedWindow(windowNameContrastLow0_25, WINDOW_NORMAL);

//Show above images inside the created windows.
imshow(windowNameOriginalImage, image);
imshow(windowNameContrastHigh2, imageContrastHigh2);
imshow(windowNameContrastHigh4, imageContrastHigh4);
imshow(windowNameContrastLow0_5, imageContrastLow0_5);
imshow(windowNameContrastLow0_25, imageContrastLow0_25);
The above code snippet will create windows and show images in them. As windows are created by passing the flag WINDOW_NORMAL, windows can be resized freely.


waitKey(0); // Wait for any key stroke

destroyAllWindows(); //destroy all open windows
The above code segment will wait until any key is pressed. After the key is pressed, all created windows will be destroyed.


Summary


With the above example, you have learnt

  • How to load an image from a file
  • How to increase and decrease the contrast of an image
  • How to create windows and show images in them
  • How to keep you program waiting for a key press
  • How to destroy all opened windows



Change the Contrast of a Video with OpenCV


Now I am going to show you how to increase and decrease the contrast of a video using an OpenCV C++ example. This is pretty much similar to the previous example.

It is recommended to go through the Play Video from File or Camera first in order to understand the following example better.
//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    //open the video file for reading
    VideoCapture cap("D:/My OpenCV Website/A Herd of Deer Running.mp4");

    // if not success, exit program
    if (cap.isOpened() == false)
    {
        cout << "Cannot open the video file" << endl;
        cin.get(); //wait for any key press
        return -1;
    }

    //Defining window names for above images
    String windowNameOriginalImage = "Original Image";
    String windowNameContrastHigh2 = "Contrast Increased by 2";
    String windowNameContrastHigh4 = "Contrast Increased by 4";
    String windowNameContrastLow0_5 = "Contrast Decreased by 0.5";
    String windowNameContrastLow0_25 = "Contrast Decreased by 0.25";

    //Create and open windows for above images
    namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
    namedWindow(windowNameContrastHigh2, WINDOW_NORMAL);
    namedWindow(windowNameContrastHigh4, WINDOW_NORMAL);
    namedWindow(windowNameContrastLow0_5, WINDOW_NORMAL);
    namedWindow(windowNameContrastLow0_25, WINDOW_NORMAL);

    while (true)
    {
        Mat frame;
        bool bSuccess = cap.read(frame); // read a new frame from video 

         //Breaking the while loop at the end of the video
        if (bSuccess == false)
        {
            cout << "Found the end of the video" << endl;
            break;
        }

        Mat frameContrastHigh2;
        frame.convertTo(frameContrastHigh2, -1, 2, 0); //increase the contrast by 2

        Mat frameContrastHigh4;
        frame.convertTo(frameContrastHigh4, -1, 4, 0); //increase the contrast by 4

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

        Mat frameContrastLow0_25;
        frame.convertTo(frameContrastLow0_25, -1, 0.25, 0); //decrease the contrast by 0.25

        //Show above images inside the created windows.
        imshow(windowNameOriginalImage, frame);
        imshow(windowNameContrastHigh2, frameContrastHigh2);
        imshow(windowNameContrastHigh4, frameContrastHigh4);
        imshow(windowNameContrastLow0_5, frameContrastLow0_5);
        imshow(windowNameContrastLow0_25, frameContrastLow0_25);

        //wait for for 10 ms until any key is pressed.  
        //If the 'Esc' key is pressed, break the while loop.
        //If the any other key is pressed, continue the loop 
        //If any key is not pressed withing 10 ms, continue the loop
        if (waitKey(10) == 27)
        {
            cout << "Esc key is pressed by user. Stoppig the video" << endl;
            break;
        }
    }

    return 0;
}
Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/A Herd of Deer Running.mp4" in the code with a valid location to a video in your computer. Then you should see a  set of videos with their contrast increased and decreased along with the original video.


Next Tutorial : Histogram Equalization


No comments:

Post a Comment