OpenCV  3.3.0-dev
Open Source Computer Vision
Histogram Equalization

Goal

In this tutorial you will learn:

Theory

What is an Image Histogram?

Histogram_Equalization_Theory_0.jpg

What is Histogram Equalization?

Histogram_Equalization_Theory_1.jpg

How does it work?

Code

Explanation

  1. Declare the source and destination images as well as the windows names:
    Mat src, dst;
    char* source_window = "Source image";
    char* equalized_window = "Equalized Image";
  2. Load the source image:
    src = imread( argv[1], 1 );
    if( !src.data )
    { cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
    return -1;}
  3. Convert it to grayscale:
    cvtColor( src, src, COLOR_BGR2GRAY );
  4. Apply histogram equalization with the function cv::equalizeHist :
    equalizeHist( src, dst );
    As it can be easily seen, the only arguments are the original image and the output (equalized) image.
  5. Display both images (original and equalized) :
    namedWindow( source_window, WINDOW_AUTOSIZE );
    namedWindow( equalized_window, WINDOW_AUTOSIZE );
    imshow( source_window, src );
    imshow( equalized_window, dst );
  6. Wait until user exists the program
    return 0;

Results

  1. To appreciate better the results of equalization, let's introduce an image with not much contrast, such as:

    Histogram_Equalization_Original_Image.jpg

    which, by the way, has this histogram:

    Histogram_Equalization_Original_Histogram.jpg

    notice that the pixels are clustered around the center of the histogram.

  2. After applying the equalization with our program, we get this result:

    Histogram_Equalization_Equalized_Image.jpg

    this image has certainly more contrast. Check out its new histogram like this:

    Histogram_Equalization_Equalized_Histogram.jpg

    Notice how the number of pixels is more distributed through the intensity range.

Note
Are you wondering how did we draw the Histogram figures shown above? Check out the following tutorial!