Histogram Equalization - Implementation

Implementation

Consider a discrete grayscale image {x} and let ni be the number of occurrences of gray level i. The probability of an occurrence of a pixel of level i in the image is

L being the total number of gray levels in the image, n being the total number of pixels in the image, and being in fact the image's histogram for pixel value i, normalized to .

Let us also define the cumulative distribution function corresponding to px as

,

which is also the image's accumulated normalized histogram.

We would like to create a transformation of the form y = T(x) to produce a new image {y}, such that its CDF will be linearized across the value range, i.e.

for some constant K. The properties of the CDF allow us to perform such a transform (see Cumulative distribution function#Inverse); it is defined as

Notice that the T maps the levels into the range . In order to map the values back into their original range, the following simple transformation needs to be applied on the result:

Read more about this topic:  Histogram Equalization