Histogram Equalization {#tutorial_histogram_equalization} ====================== Goal ---- In this tutorial you will learn: - What an image histogram is and why it is useful - To equalize histograms of images by using the OpenCV cv::equalizeHist 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. ![image](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. ![image](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 to *Learning OpenCV*). For the histogram \f$H(i)\f$, its *cumulative distribution* \f$H^{'}(i)\f$ is: \f[H^{'}(i) = \sum_{0 \le j < i} H(j)\f] To use this as a remapping function, we have to normalize \f$H^{'}(i)\f$ 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: ![image](images/Histogram_Equalization_Theory_2.jpg) - Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image: \f[equalized( x, y ) = H^{'}( src(x,y) )\f] Code ---- - **What does this program do?** - Loads an image - Convert the original image to grayscale - Equalize the Histogram by using the OpenCV function @ref cv::EqualizeHist - Display the source and equalized images in a window. - **Downloadable code**: Click [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp) - **Code at glance:** @code{.cpp} #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include #include 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 "<"<