Lib4U

‎"Behind every stack of books there is a flood of knowledge."

Histogram Equalization (Open CV)

Histogram_Equalization_Equalized_Histogram

Theory

What is an Image Histogram?

  • It is a graphical representation of the intensity distribution of an image.
  • It quantifies the number of pixels for each intensity value considered.

../../../../../_images/Histogram_Equalization_Theory_0.jpg

What is Histogram Equalization?

  • It is a method that improves the contrast in an image, in order to stretch out the intensity range.
  • To make it clearer, from the image above, you can see that the pixels seem clustered around the middle of the available range of intensities. What Histogram Equalization does is to stretch out this range. Take a look at the figure below: The green circles indicate the underpopulated intensities. After applying the equalization, we get an histogram like the figure in the center. The resulting image is shown in the picture at right.

../../../../../_images/Histogram_Equalization_Theory_1.jpg

How does it work?

  • Equalization implies mapping one distribution (the given histogram) to another distribution (a wider and more uniform distribution of intensity values) so the intensity values are spreaded over the whole range.
  • To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer toLearning OpenCV). For the histogram H(i), its cumulative distribution H^{'}(i) is:

    H^{'}(i) = \sum_{0 \le j < i} H(j)

    To use this as a remapping function, we have to normalize H^{'}(i) such that the maximum value is 255 ( or the maximum value for the intensity of the image ). From the example above, the cumulative function is:

    ../../../../../_images/Histogram_Equalization_Theory_2.jpg

  • Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image:

    equalized( x, y ) = H^{'}( src(x,y) )

Code

  • What does this program do?
    • Loads an image
    • Convert the original image to grayscale
    • Equalize the Histogram by using the OpenCV function EqualizeHist
    • Display the source and equalized images in a window.
  • Downloadable code: Click here
  • Code at glance:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

/**  @function main */
int main( int argc, char** argv )
{
  Mat src, dst;

  char* source_window = "Source image";
  char* equalized_window = "Equalized Image";

  /// Load image
  src = imread( argv[1], 1 );

  if( !src.data )
    { cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
      return -1;}

  /// Convert to grayscale
  cvtColor( src, src, CV_BGR2GRAY );

  /// Apply Histogram Equalization
  equalizeHist( src, dst );

  /// Display results
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );

  imshow( source_window, src );
  imshow( equalized_window, dst );

  /// Wait until user exits the program
  waitKey(0);

  return 0;
}

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, CV_BGR2GRAY );
  4. Apply histogram equalization with the function 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, CV_WINDOW_AUTOSIZE );
    namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );
    
    imshow( source_window, src );
    imshow( equalized_window, dst );
  6. Wait until user exists the program
    waitKey(0);
    return 0;

Results

  1. To appreciate better the results of equalization, let’s introduce an image with not much contrast, such as:../../../../../_images/Histogram_Equalization_Original_Image.jpgwhich, by the way, has this histogram:

    ../../../../../_images/Histogram_Equalization_Original_Histogram.jpgnotice that the pixels are clustered around the center of the histogram.

  2. After applying the equalization with our program, we get this result:../../../../../_images/Histogram_Equalization_Equalized_Image.jpgthis image has certainly more contrast. Check out its new histogram like this:

    ../../../../../_images/Histogram_Equalization_Equalized_Histogram.jpgNotice how the number of pixels is more distributed through the intensity range.

Source:

http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html

2 comments on “Histogram Equalization (Open CV)

  1. burberry 2013
    February 1, 2013

    This posting identifies and additionally defines many different headphone know-how and his or her’s differences. We get into detail to explain each one particular and most of the benefits that many offers.

  2. longchamps le pliage uk
    February 2, 2013

    Top write-up. I wait for reading a great deal more. Cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Virtual Fashion Technology

Virtual Fashion Education

toitocuaanhem

"chúng tôi chỉ là tôi tớ của anh em, vì Đức Kitô" (2Cr 4,5b)

VentureBeat

News About Tech, Money and Innovation

digitalerr0r

Modern art using the GPU

Theme Showcase

Find the perfect theme for your blog.

lsuvietnam

Learn to Learn

Gocomay's Blog

Con tằm đến thác vẫn còn vương tơ

Toán cho Vật lý

Khoa Vật lý, Đại học Sư phạm Tp.HCM - ĐT :(08)-38352020 - 109

Maths 4 Physics & more...

Blog Toán Cao Cấp (M4Ps)

Bucket List Publications

Indulge- Travel, Adventure, & New Experiences

Lib4U

‎"Behind every stack of books there is a flood of knowledge."

The WordPress.com Blog

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: