• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Histogram Equalization {#tutorial_histogram_equalization}
2======================
3
4Goal
5----
6
7In this tutorial you will learn:
8
9-   What an image histogram is and why it is useful
10-   To equalize histograms of images by using the OpenCV function @ref cv::equalizeHist
11
12Theory
13------
14
15### What is an Image Histogram?
16
17-   It is a graphical representation of the intensity distribution of an image.
18-   It quantifies the number of pixels for each intensity value considered.
19
20![](images/Histogram_Equalization_Theory_0.jpg)
21
22### What is Histogram Equalization?
23
24-   It is a method that improves the contrast in an image, in order to stretch out the intensity
25    range.
26-   To make it clearer, from the image above, you can see that the pixels seem clustered around the
27    middle of the available range of intensities. What Histogram Equalization does is to *stretch
28    out* this range. Take a look at the figure below: The green circles indicate the
29    *underpopulated* intensities. After applying the equalization, we get an histogram like the
30    figure in the center. The resulting image is shown in the picture at right.
31
32![](images/Histogram_Equalization_Theory_1.jpg)
33
34### How does it work?
35
36-   Equalization implies *mapping* one distribution (the given histogram) to another distribution (a
37    wider and more uniform distribution of intensity values) so the intensity values are spreaded
38    over the whole range.
39-   To accomplish the equalization effect, the remapping should be the *cumulative distribution
40    function (cdf)* (more details, refer to *Learning OpenCV*). For the histogram \f$H(i)\f$, its
41    *cumulative distribution* \f$H^{'}(i)\f$ is:
42
43    \f[H^{'}(i) = \sum_{0 \le j < i} H(j)\f]
44
45    To use this as a remapping function, we have to normalize \f$H^{'}(i)\f$ such that the maximum value
46    is 255 ( or the maximum value for the intensity of the image ). From the example above, the
47    cumulative function is:
48
49    ![](images/Histogram_Equalization_Theory_2.jpg)
50
51-   Finally, we use a simple remapping procedure to obtain the intensity values of the equalized
52    image:
53
54    \f[equalized( x, y ) = H^{'}( src(x,y) )\f]
55
56Code
57----
58
59-   **What does this program do?**
60    -   Loads an image
61    -   Convert the original image to grayscale
62    -   Equalize the Histogram by using the OpenCV function @ref cv::equalizeHist
63    -   Display the source and equalized images in a window.
64-   **Downloadable code**: Click
65    [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp)
66-   **Code at glance:**
67    @include samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp
68
69Explanation
70-----------
71
72-#  Declare the source and destination images as well as the windows names:
73    @code{.cpp}
74    Mat src, dst;
75
76    char* source_window = "Source image";
77    char* equalized_window = "Equalized Image";
78    @endcode
79-#  Load the source image:
80    @code{.cpp}
81    src = imread( argv[1], 1 );
82
83    if( !src.data )
84      { cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
85        return -1;}
86    @endcode
87-#  Convert it to grayscale:
88    @code{.cpp}
89    cvtColor( src, src, COLOR_BGR2GRAY );
90    @endcode
91-#  Apply histogram equalization with the function @ref cv::equalizeHist :
92    @code{.cpp}
93    equalizeHist( src, dst );
94    @endcode
95    As it can be easily seen, the only arguments are the original image and the output (equalized)
96    image.
97
98-#  Display both images (original and equalized) :
99    @code{.cpp}
100    namedWindow( source_window, WINDOW_AUTOSIZE );
101    namedWindow( equalized_window, WINDOW_AUTOSIZE );
102
103    imshow( source_window, src );
104    imshow( equalized_window, dst );
105    @endcode
106-#  Wait until user exists the program
107    @code{.cpp}
108    waitKey(0);
109    return 0;
110    @endcode
111
112Results
113-------
114
115-#  To appreciate better the results of equalization, let's introduce an image with not much
116    contrast, such as:
117
118    ![](images/Histogram_Equalization_Original_Image.jpg)
119
120    which, by the way, has this histogram:
121
122    ![](images/Histogram_Equalization_Original_Histogram.jpg)
123
124    notice that the pixels are clustered around the center of the histogram.
125
126-#  After applying the equalization with our program, we get this result:
127
128    ![](images/Histogram_Equalization_Equalized_Image.jpg)
129
130    this image has certainly more contrast. Check out its new histogram like this:
131
132    ![](images/Histogram_Equalization_Equalized_Histogram.jpg)
133
134    Notice how the number of pixels is more distributed through the intensity range.
135
136@note
137Are you wondering how did we draw the Histogram figures shown above? Check out the following
138tutorial!
139