Open In App

Histogram Equalization in Digital Image Processing

Improve
Improve
Like Article
Like
Save
Share
Report

A digital image is a two-dimensional matrix of two spatial coordinates, with each cell specifying the intensity level of the image at that point. So, we have an N x N matrix with integer values ranging from a minimum intensity level of 0 to a maximum level of L-1, where L denotes the number of intensity levels. Hence, the intensity levels of a pixel r can take on values from 0,1,2,3,…. (L-1). Generally, L = 2m, where m is the number of bits required to represent the intensity levels. Zero level intensity denotes complete black or dark, whereas L-1 level indicates complete white or absence of grayscale. 

Intensity Transformation: 

Intensity transformation is a basic digital image processing technique, where the pixel intensity levels of an image are transformed to new values using a mathematical transformation function, so as to get a new output image. In essence, intensity transformations is simply to implement the following function:

s = T(r)

where s is the new pixel intensity level and r is the original pixel intensity value of the given image and r≥0.

With different forms of the transformation function T(r), we get different output images. 

Common Intensity Transformation Functions:

1. Image negation: This reverses the grayscales of an image, making dark pixels whiter and white pixels darker. This is completely analogous to the photographic negative, hence the name. 

s = L - 1 - r

2. Log Transform: Here c is some constant. It is used for expanding the dark pixel values in an image. 

s = c log(1+r)

3. Power-law Transform: Here c and γ are some arbitrary constants. This transform can be used for a variety of purposes by varying the value of γ.

 s = c rγ

Histogram Equalization: 

The histogram of a digital image, with intensity levels between 0 and (L-1), is a function h( rk ) = nk , where rk is the kth intensity level and nk is the number of pixels in the image having that intensity level. We can also normalize the histogram by dividing it by the total number of pixels in the image. For an N x N image, we have the following definition of a normalized histogram function: 

p( rk ) = nk/N2

This p(rk) function is the probability of the occurrence of a pixel with the intensity level rk. Clearly, 

∑ p( rk ) = 1

The histogram of an image, as shown in the figure, consists of the x-axis representing the intensity levels rk and the y-axis denoting the h(rk) or the p(rk) functions. 

The histogram of an image gives important information about the grayscale and contrast of the image. If the entire histogram of an image is centered towards the left end of the x-axis, then it implies a dark image. If the histogram is more inclined towards the right end, it signifies a white or bright image. A narrow-width histogram plot at the center of the intensity axis shows a low-contrast image, as it has a few levels of grayscale. On the other hand, an evenly distributed histogram over the entire x-axis gives a high-contrast effect to the image. 

In image processing, there frequently arises the need to improve the contrast of the image. In such cases, we use an intensity transformation technique known as histogram equalization.  Histogram equalization is the process of uniformly distributing the image histogram over the entire intensity axis by choosing a proper intensity transformation function. Hence, histogram equalization is an intensity transformation process. 

The choice of the ideal transformation function for uniform distribution of the image histogram is mathematically explained below. 

Mathematical Derivation of Transformation Function for Histogram Equalization:

Let us consider that the intensity levels of the image r is continuous, unlike the discrete case in digital images. We limit the values that r can take between 0 and L-1, that is, 0 ≤ r ≤ L-1 . r = 0 represents black and r = L-1  represents white. Let us consider an arbitrary transformation function:

 s = T( r )

where s denotes the intensity levels of the resultant image. We have certain constraints on T(r)

  • T(r) must be a strictly increasing function. This makes it an injective function.
  • 0 ≤ T( r ) ≤ L-1. This makes T(r) surjective.

The above two conditions make T(r) a bijective function. We know that such functions are invertible. So we can get back r values from s. We can have a function such that  r = T-1( s )

Let us now say that the probability density function (pdf) of r is pr (x) and the cumulative distribution function (CDF) of r is Fr (x). Now the CDF of s will be :

FS( x ) = P( s \le x ) = P ( T(r) \le x ) = P ( r \le T-1(x) ) = Fr ( T-1(x) ).

We put the first condition of T(r) precisely to make the above step hold true. The second condition is needed as s is the intensity value for the output image and so must be between o and (L-1).

So, a pdf of s can be obtained by differentiating FS( x ) with respect to x. We get the following relation:

p_{s}(s)=p_{r}(r)\frac{\mathrm{d} r}{\mathrm{d} s}

Now, if we define the transformation function as follows:

s = T(r) = (L-1)\int_{0}^{r}p_{r}(x)dx

Then using this function gives us a uniform pdf for s

\frac{\mathrm{d} s}{\mathrm{d} r}=(L-1)\frac{\mathrm{d} }{\mathrm{d} r}\int_{0}^{r}p_{r}(x)dx=(L-1)p_{r}(r)

The above step used Leibnitz’s integral rule. Using the above derivative, we get:

p_{s}(s)=p_{r}(r)\frac{\mathrm{d} r}{\mathrm{d} s}=p_{r}(r)\frac{1}{(L-1)p_{r}(r)}=\frac{1}{L-1}

So the pdf of s is uniform. This is what we want.

Now, we extend the above continuous case to the discrete case. The natural replacement of the integral sign is the summation. Hence, we are left with the following histogram equalization transformation function. 

s_{k}=T(r_{k})=(L-1)\sum_{j=0}^{k}p_{r}(r_{j})=\frac{(L-1)}{N^{2}}\sum_{j=0}^{k}n_{j}

Since s must have integer values, any non-integer value obtained from the above function is rounded off to the nearest integer. 

Example 1:

Matlab

% Histogram Equalization in MATLAB
% reading image
I = imread("GFG.jfif");
figure
subplot(1,3,1)
imshow(I)
subplot(1,3,2:3)
imhist(I)

                    

Output:

 

Example 2:

Matlab

% Histogram Equalization in MATLAB using function
% histogram equalization
J = histeq(I);
figure
subplot(1,3,1)
imshow(J)
subplot(1,3,2:3)
imhist(J)

                    

Output:

 



Last Updated : 22 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads