Here we want to convert the image so that it has a particular histogram that can be arbitrarily specified. Such a mapping function can be found in three steps:

- Equalize the histogram of the input image
- Equalize the specified histogram
- Relate the two equalized histograms

We first equalize the histogram of the input image :

We then equalize the desired histogram of the output image :

The inverse of the above transform is

As the two intermediate images and both have the same equalized histogram, they are actually the same image, i.e., , and the overall transform from the given image to the desired image can be found to be:

However, as the image gray levels are discrete, the continuous mapping obtained above can only be approximated. The discrete histograms and are not necessarily identical. We therefore need to relate each gray level in with to a gray level in with , so that the mapping from to can be established.

Here are the specific steps of the algorithm:

**Step 1:**Find histogram of input image , and find its cumulative , the histogram equalization mapping function:

**Step 2:**Specify the desired histogram , and find its cumulative , the histogram equalization mapping function:

**Step 3:**Relate the two mapping above to build a lookup table for the over all mapping. Specifically, for each input level , find an output level so that best matches :

and then we setup a lookup entry .

**Example:**

The histogram of the given image and the histogram desired are shown below:

**Step 1:**Equalize to get mapping (save as previous example).0 790 0.19 0.19 1 1023 0.25 0.44 2 850 0.21 0.65 3 656 0.16 0.81 4 329 0.08 0.89 5 245 0.06 0.95 6 122 0.03 0.98 7 81 0.02 1.00 **Step 2:**Equalize to get mapping .0 0.0 0.0 1 0.0 0.0 2 0.0 0.0 3 0.15 0.15 4 0.20 0.35 5 0.30 0.65 6 0.20 0.85 7 0.15 1.0 **Step 3:**Obtain overall mapping,0 0.19 0.0 3 1 0.44 0.0 4 2 0.65 0.0 5 3 0.81 0.15 6 4 0.89 0.35 6 5 0.95 0.65 7 6 0.98 0.85 7 7 1.0 1.0 7 i 0 1 2 3 4 5 6 7 j 3 4 5 6 6 7 7 7 This is the histogram of the resulting image:

In the following example, the desired histogram is a triangle with linear increase in the lower half of the the gray level range, and linear decrease in the upper half. Again the cumulative histogram shows indeed the density histogram is such a triangle.