Histogram Equalization in Digital Image Processing

Histogram Equalization in Digital Image Processing

A beginner-friendly introduction to histogram equalization and image contrast enhancement

Author
Nguyen Bao Huy
10:56:00 16/05/2026
5 min read
0 comments

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 =

Formula2^m
, 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:

Formulas = 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. 

Formulas = L - 1 - r

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

Formulas = 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 γ.

Formulas = c r^{\gamma}

Histogram Equalization:

The histogram of digital image, with intensity levels between 0 and (L-1), is a function

Formulah(r_k) = n_k
, where
Formular_k
is
Formulak_{th}
intensity and
Formulan_k
is the number of pixels in the image haivng that intensity levels. We can also normalize the histogram by dividing it by the total number of pixels in the image. For an NxN image, we have the following definition of normalized histogram function:

Formulap(r_k) = \frac{n_k}{N^2}

This

Formulap(r_k)
function is the probability of the occurrence of a pixel with the intensity level
Formular_k
. Clearly

Formula\sum p(r_k) = 1

The histogram of an image, as shown in the figure, consists of the x-axis representing the intensity levels

Formular_k
and the y-axis denoting the
Formulah(r_k)
or the
Formulap(r_k)
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:

Formulas = 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

Formular = T^{-1}(s)

Let us now say that the probability density function (pdf) of r is

Formulap_r(x)
and the cumulative distribution function (CDF) of r is
FormulaF_r(x)
. Now the CDF of s will be:

FormulaF_S(x) = P(s \le x) = P(T(r) \le x) = P(r \le T^{-1}(x)) = F_r(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 0 and (L-1).

So, a pdf of s can be obtained by differentiating

FormulaF_s(x)
with respect to x. We get the following relation:

Formulap_s(s) = p_r(r)\frac{dr}{ds}

Now, if we define the transformation function as follows:

Formulas = T(r) = (L-1)\int_0^r p_r(x)dx

Then using this function gives us a uniform pdf for s.

Formula\frac{ds}{dr} = (L-1)\frac{d}{dr}\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:

Formulap_s(s) = p_r(r)\frac{dr}{ds} = 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.

Formulas_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:

python
% 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:

Figure 1. Histogram Equalization in MATLAB result
Figure 1. Histogram Equalization in MATLAB result

Example 2:

javascript
% 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)
Figure 2. Histogram Equalization in MATLAB result 2
Figure 2. Histogram Equalization in MATLAB result 2

First Lesson

You are at the beginning of this curriculum.

Latest Tutorial

More chapters coming soon to this topic.

Author avatar

Nguyen Bao Huy

Lead Fullstack & AI Solutions Engineer

Specializing in Next.js App Router, React 19, TypeScript, and modern design systems. Passionate about creating seamless user experiences.

Discussion

0

No comments yet

Be the first to share your thoughts, question a concept, or provide additional tips!

Leave a Reply

Share your insights, questions, or solutions with the developer community.

Your avatar
0 / 500 characters