Histogram equalization with Python and NumPy

June 7, 2009    numpy python histogram

I have been looking for a nice and clean implementation of histogram equalization with no luck so far. So I decided to write a short script and share it. You will need to have NumPy installed.

A very useful example of a graylevel transform is histogram equalization. This transform flattens the graylevel histogram of an image so that all intensities are as equally common as possible. This is often a good way to normalize image intensity before further processing and also a way to increase image contrast.

The transform function is in this case a cumulative distribution function (cdf) of the pixel values in the image (normalized to map the range of pixel values to itself).

Here's how to do it.

def histeq(im,nbr_bins=256):

#get image histogram
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #cumulative distribution function
cdf = 255 * cdf / cdf[-1] #normalize

#use linear interpolation of cdf to find new pixel values
im2 = interp(im.flatten(),bins[:-1],cdf)

return im2.reshape(im.shape), cdf

The function takes a grayscale image and the number of bins to use in the histogram as input and returns an image with equalized histogram together with the cumulative distribution function used to do the mapping of pixel values. To try this on an image,

from PIL import Image
from numpy import *

im = array(Image.open('AquaTermi_lowcontrast.jpg').convert('L'))
im2,cdf = imtools.histeq(im)

The picture below shows an example of histogram equalization (click to enlarge). The top row shows the graylevel histogram before and after equalization together with the cdf mapping. As you can see, the contrast increases and the details of the dark regions appear clearly.