include(../../../m4-dip-html-macros.m4)

LESSON(Histogram Equalization)

Histogram equalization is as a contrast enhancement technique with the
objective to obtain a new enhanced image with an uniform histogram.
This can be achieved by using the normalized cumulative histogram as the
grey scale mapping function.

The intermediate steps of the histogram equalization process are:
BEGINLIST
ITEM Take the cumulative histogram of the image to be equalized
ITEM Normalize the cumulative histogram to 255
ITEM Use the normalized cumulative histogram as the mapping function of
     the original image
ENDLIST

These intermediate steps are illustrated below.

HTMLTWOFIG(original, original-histogram,
           original image, its histogram)

HTMLTWOFIG(cumulated,equalized,
           normalized cumulative histogram of the original image,
           histogram equalized image)

HTMLTWOFIG(equalized-histogram,equalized-cumulative-histogram,
           histogram of the equalized image, 
           cumulative histogram of the equalized image)

Due to the discrete nature of the problem, the resultant histogram is not
uniform as desired, but you can see from the cumulative equalized histogram
that it does approximate to a straight line.  

You can get a much uniformer histogram if you artificially increase the
quantization of the original image before applying the equalization.
You can check this in the "Other Examples" section.

OTHEREXAMPLES

To be able to get a nearly uniform histogram after equalization, you
must artificially increate the quantization of the grey level pixels of
the original image. 

The steps below give good results, for unsigned byte images:

BEGINLIST
ITEM convert the pixel data type of the original image to {\bf unsigned long},
     so that the image can hold a larger dynamic range of data
ITEM multiply the pixels by a large factor, say 1000, 
ITEM add noise uniformly distributed between 0 and 999. After that you have
     an image with a higher quantization grey level
ITEM apply the histogram equalization
ITEM divide the resultant image by 1000 and convert it back to unsigned byte
ENDLIST

Below is the resultant image and its histogram. You may notice that although
the histogram is much more uniform, the image quality is very similar to
the original method of histogram equalization.

HTMLTWOFIG( equalized-flat, equalized-flat-histogram,
            histogram equalized, its histogram)

BUILDINGWORKSPACE

LABPURPOSE(`The purpose of this lab is to experiment with the histogram
equalization technique for image contrast enhancement.')

BEGINLIST %1

ITEM Calculate the histogram of a data set.

  BEGINLIST %2

  ITEM Read the image IMAGEDIR(cathedral.viff) and display it using
the operators USERDEFINED and DISPLAYIMAGE.

    BEGINLIST %3

    ITEM USERDEFINEDMENU

    ITEM DISPLAYIMAGEMENU

    ENDLIST %3

  ITEM Determine the histogram of the image and plot it. Use 
operators HISTOGRAM and _2DPLOT.

    BEGINLIST %3

    ITEM HISTOGRAMMENU

    ITEM _2DPLOTMENU

    ITEM View the histogram as a "discrete" plot

    ENDLIST %3

  ENDLIST %2

ITEM Since the HISTOGRAM operator does not have a cumulative
histogram option, you can find the cumulative histogram by performing
a linear convolution of the histogram with a 256-height constant
kernel.

  BEGINLIST %2

  ITEM Create a constant valued image with dimensions Width = 1,
Height = 256, and constant value 1.  Use the CONSTANT operator.

    BEGINLIST %3

    ITEM CONSTANTMENU

    ENDLIST %3

  ITEM Expand the histogram that you calculated above by 2 in the
width direction using the EXPAND operator.

    BEGINLIST %3

    ITEM EXPANDMENU

    ENDLIST %3

  ITEM Convolve the constant kernel with the expanded histogram 
using LINEAROPERATOR.

    BEGINLIST %3

    ITEM LINEAROPERATORMENU

    ITEM Connect the expanded histogram to the first input (Input
Object) of LINEAROPERATOR, and the constant image to the second
input (Input Kernel).

    ENDLIST %3

  ITEM The result of the convolution is an image of dimensions Width =
2, Height = 256. Extract the first column of data using the EXTRACT
operator (both columns are identical). This is the cumulative
histogram.

    BEGINLIST %3

    ITEM EXTRACTMENU

    ENDLIST %3

  ITEM Normalize the cumulative histogram to the desired range of the
output image, e.g. 0 to 255, and convert the data type to unsigned
byte. Use the NORMALIZE operator. Plot the cumulative histogram
using the _2DPLOT operator.

    BEGINLIST %3

    ITEM NORMALIZEMENU

    ITEM _2DPLOTMENU

    ENDLIST %3

  ITEM Convert the normalized cumulative histogram to a "Map" segment
(color table), using the operator COPYFROMVALUE.

    BEGINLIST %3

    ITEM COPYFROMVALUEMENU

    ITEM Open the COPYFROMVALUE operator and select the "Map" segment.

    ENDLIST %3

  ENDLIST %2

ITEM Use the color table in the three possible ways:

  BEGINLIST %2

  ITEM Use the DISPLAYIMAGE operator directly.

    BEGINLIST %3

    ITEM DISPLAYIMAGEMENU

    ENDLIST %3

  ITEM Combine the "Map" segment to the original image file.

    BEGINLIST %3

    ITEM INSERTSEGMENTSMENU

    ITEM DISPLAYIMAGEMENU

    ITEM Connect the color table to "Input 1" and the original image
to "Input 2". Open the INSERTSEGMENTS operator and set the
parameters so that the "Map" segment is transferred.

    ENDLIST %3

  ITEM Map the "Map" segment to the original image.

    BEGINLIST %3

    ITEM MAPDATAMENU

    ITEM DISPLAYIMAGEMENU

    ITEM Use the output from the INSERTSEGMENTS operator (Step 3-B)
as the input to MAPDATA.

    ENDLIST %3

  ENDLIST %2

ITEM Calculate the histogram of the equalized image.

  BEGINLIST %2

  ITEM Calculate the histogram of the histogram equalized image
(output from Part 3-C) and plot it using the HISTOGRAM and _2DPLOT
operators.

    BEGINLIST %3

    ITEM HISTOGRAMMENU

    ITEM _2DPLOTMENU

    ENDLIST %3

  ITEM Use the operator EQUALIZE to perform the histogram
equalization directly. Check if your implementation generates the same
results as this operator (look at the resulting image and the
histogram of the image). Use the COMPAREOBJECTS operator to compare
the image data and the histograms.

    BEGINLIST %3

    ITEM EQUALIZEMENU

    ITEM DISPLAYIMAGEMENU

    ITEM HISTOGRAMMENU

    ITEM _2DPLOTMENU

    ITEM COMPAREOBJECTSMENU

    ITEM FILEVIEWERMENU

    ITEM Open the COMPAREOBJECTS pane and unselect the "Silent"
option.

    ITEM Connect the output of COMPAREOBJECTS to the FILEVIEWER

    ENDLIST %3

  ENDLIST %2

ENDLIST %1


%***************
EXERCISES

BEGINLIST %1

ITEM Apply this technique only to a selected portion of the image. Use the MASK segment to select the 
area of interest.

ITEM One solution to obtain a more uniform histogram in the resultant
image is by converting the image to floating point pixels and adding
uniform noise ranging from [0, 1] to each pixel. Equalizing this image
which has larger number of gray levels will result in a more uniform
histogram.

ENDLIST %1

WORKSPACE(equalization.wk)

ENDLESSON

