To transform the gray levels of the image so that the histogram of the
resulting image is equalized to become a constant:

The purposes:

- to equally make use of all available gray levels in the dynamic range;
- for further histogram specification.

We first assume the pixel values are continuous in the range of , and
the mapping function maps to also in the same range. We
also assume all pixels within the gray scale interval of the input image
are mapped to the corresponding range of the output image. As the number
of pixels being mapped remains unchanged, we have

For the histogram of the output image to be equalized, it needs to be
constant 1, i.e., , so that
. We
therefore have:

Integrating both sides, we get the mapping function for the histogram equalization:

where is the cumulative distribution of the gray levels of the input image:

This histogram equalization mapping can be intuitively interpreted by the following:

- If is high, has a steep slope, will be wide, causing to be low to keep ;
- If is low, has a shallow slope, will be narrow, causing to be high.

For discrete gray levels, the gray level of the input takes one of
the discrete values: , and the continuous mapping function
becomes discrete:

where and are respectively the density and cumulative histogram of the input image :

and is the number of pixels with gray level , and is the total number of pixels.

The resulting function in the range needs to be converted to the gray levels by either of the two ways:

**Example:**

Assume the images have pixels in gray levels. The following table shows the equalization process corresponding to the two conversion methods above:

0 | 790 | 0.19 | 0.19 | 1 | 0.19 | 0.19 | 0 | 0.19 | 0.19 |

1 | 1023 | 0.25 | 0.44 | 3 | 0.25 | 0.44 | 2 | 0.25 | 0.44 |

2 | 850 | 0.21 | 0.65 | 5 | 0.21 | 0.65 | 4 | 0.21 | 0.65 |

3 | 656 | 0.16 | 0.81 | 6 | 5 | 0.16 | 0.81 | ||

4 | 329 | 0.08 | 0.89 | 6 | 0.24 | 0.89 | 6 | 0.08 | 0.89 |

5 | 245 | 0.06 | 0.95 | 7 | 7 | ||||

6 | 122 | 0.03 | 0.98 | 7 | 7 | ||||

7 | 81 | 0.02 | 1.00 | 7 | 0.11 | 1.00 | 7 | 0.11 | 1.00 |

In the following example, the histogram of a given image is equalized. Although the resulting histogram may not look constant, but the cumulative histogram is a exact linear ramp indicating that the density histogram is indeed equalized. The density histogram is not guaranteed to be a constant because the pixels of the same gray level cannot be separated to satisfy a constant distribution.

**Programming issues:**

- Obtain the histogram of the input image:

- Build lookup table:

- Image Mapping:

**Example:**