Equalizing the image histogram
In the previous recipe, we showed you how the contrast of an image can be improved by stretching a histogram so that it occupies the full range of the available intensity values. This strategy indeed constitutes an easy fix that can effectively improve the quality of an image. However, in many cases, the visual deficiency of an image is not that it uses a too-narrow range of intensities.
Rather, it is that some intensity values are used much more frequently than others. The histogram shown in the first recipe of this chapter is a good example of this phenomenon. The middle-gray intensities are indeed heavily represented, while darker and brighter pixel values are rather rare. One possible way to improve the quality of an image could therefore be to make equal use of all available pixel intensities. This is the idea behind the concept of histogram equalization, that is making the image histogram as flat as possible.
How to do it...
OpenCV offers an easy-to-use...