At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Machine Vision » Beginner

Histogram Equalization

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:

The image to be equalized

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.

Standard 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:

Standard cumulative frequency

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:

Equalized 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:

Standard histogram Standard histogram

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:

Colour equalization

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).

References

Efford, Nick. Digital Image Processing: A Practical Introduction Using Java. Addison-Wesley. Essex: 2000.

Submitted: 26/11/2004

Article content copyright © James Matthews, 2004.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- The Latest (03/04/2012)
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -