Task: Histogram equalization

Write a python script/function, that does global histogram equalization of an gray-scale image. The solution should consist of the following steps:

  • load image, convert to gray-scale if needed,
  • compute histogram using 256 bins,
  • compute relative cummulative histogram,
  • draw graphs of both histograms,
  • create enhanced image - replace the intensity of every pixel by corresponding value from cummulative histogram (i.e. the c.h. serves as an intensity lookup table),
  • compute histogram and cummulative histogram of the enhanced image,
  • show the enhanced image and its histograms.

Some tips:

  • Image intensities are floating point values in the range <0;1>; an intensity must be reasonably multiplied and converted to an integer value to serve as an index to an array, e.g. index = int( img[i,j] * 255 ) maps <0;1> to {0,1,...,255}.
  • h = np.zeros( (256) ) preallocates 1-D array (vector) with 256 elements all set to zero
  • len( img.shape ) returns the number of dimensions of the numpy array img; 3 for a colour image, 2 for a gray-scale one

Example results

In [6]:
%run task_histeq