This article looks at how to enhance an image by equalizing its histogram. Often images (especially scanned images) have a limited range of colours, or are lacking contrast. Enhancing the image can allow for improved detail, but can also aid other machine vision operations such as segmentation. For example, look at our example image below:
Note how the image is extremely grey; it lacks detail since the range of colours seems limited to mid grey-levels. We can verify this by looking at the image's histogram.
A histogram simply plots the frequency at which each grey level occurs from 0 (black) to 255 (white). Here you can see how the majority of the grey levels in the image are bunched between about 105 and 205.
So given this data, how can we enhance the image automatically? Our goal with histogram equalization is to expand the colours within the image to fill the entire 0-255 spectrum. To do this, we need to calculate the cumulative frequencies within the image. The cumulative frequency for grey level g is defined as the sum of the histogram data 0 to g. We can graph the cumulative frequencies for our image:
If you look carefully you can see the correlation between the histogram and cumulative frequencies. Note how the maximum of the cumulative frequency graph will always be equal to the number of pixels in the image (307200, 640x480 in our case). With all this in mind, it is now extremely easy to redistribute the colours across the entire spectrum. In fact, if we assume the cumulate frequency is stored in an array, our histogram equalization can be written as:
alpha = 255 / numPixels for each pixel g(x,y) = cumulativeFrequency[f(x,y)] * alpha end for
If you think about this a little, it makes perfect sense. For example, in our original histogram the grey level 200 appears at the very edge of our spectrum, but because the cumulative frequency at 200 is extremely high (306237), any pixels with this value will be given the new pixel value of 254. Take a look at the new image:
Look how much clearer the image seems, details in the cars and the floors and walls are a lot sharper. Looking at the histogram and cumulative frequency graphs for the equalized image are even more telling:
The equalized histogram looks like the original histogram, although it has been "stretched" across the entire spectrum. The cumulative frequency graph shows the (almost) linear nature of grey level frequencies within the image. Note that the same process can be applied to colour images by performing the process on the red, green and blue channels separately as this image shows:
If you want to experiment further, this article was written using the Generation5 JDK's Histogram and EqualizeFilter classes (although the JDK's built-in histogram visualization was not used, since Gnuplot provided nicer looking results).
Efford, Nick. Digital Image Processing: A Practical Introduction Using Java. Addison-Wesley. Essex: 2000.
Article content copyright © James Matthews, 2004.
All content copyright © 1998-2007, Generation5 unless otherwise noted.