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 low, has a shallow slope, will be narrow, causing to be high, to keep ;
- If is high, has a steep slope, will be wide, causing to be low, to keep .

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:

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.

**Example:**