Quick Guide to Histogram Equalization for Clearer Images

Neel Last Updated : 24 Oct, 2024
10 min read

There are several methods for extracting data from an image. The image and essential information must be kept in mind when selecting a technique. This article focuses on “Histogram Equalization,” one of the ways for extracting information from an image

After reading this article you will be able to:

  • Understand what are the histograms
  • What is Histogram Equalization for images?
  • Classification of Histogram Equalization methods
  • Identify use cases for applying different types of Histogram Equalization techniques

This article was published as a part of the Data Science Blogathon.

Let’s Start with an Example

Look at the image given below:

The image contains few trees, buildings, and lights but everything seems blackish. Can we make the image more clear so that more details become visible which are not visible right now (maybe by performing some operations on it)? The answer is yes, and that’s what we are going to do today. You must have guessed correctly that it is possible with histogram equalization. It is an algorithm used for enhancing an image in such a way that it becomes more pleasant to Human eyes. Before diving into an algorithm it’s important to understand what is the histogram.

What is Histogram? 

A histogram is a graphical representation of certain data similar to a bar chart. Let us start with a non-technical example to understand the same.

Example 

I asked some of my friends if they understood “What does histogram mean?” after showing them a definition of the Histogram. Here are the responses I received.

Source: Author

We can plot this information (/data) using the following graph. It shows the frequency of three different answers (Yes, no & somewhat) which is 5, 15 and 7 respectively.

Source: Author

The graph shown above is nothing but the Histogram of the data given in the table. After showing this plot it became something like..

Source: Author

So, histograms change as the data change and they give us a visual representation of how the data is spread. Basically, they show the frequency distribution of an attribute present in the data. Histograms are not limited to images. But today, we are focusing on image histograms.

Image Histograms

Image is nothing but an array of pixels. In RGB format it can be considered as a 3D array (3 overlapping 2D arrays of Red, Green and Blue components of the image). If the image size is axbx3 then it contains a number of rows, b number of columns and total a*b pixels in one colour array/frame. Here, 3 represents 3 frames for colours Red, Green and Blue.

For the sake of understating, we are going to convert it into a grayscale image. A grayscale image is a 2D array where pixel values are a combination of white and black colours only. An image histogram is the distribution of image pixels’ values. Data type uint8 (which is mostly used one) represents that each pixel is represented using 8 bits. As a consequence, pixels can achieve values between 0 and 255 ( 28 = 256). Now, let’s take a look at the image Histogram for the image shown at starting of the article.

Source: Author

x-axis and y-axis represent the intensity of pixels and the number of pixels respectively. As discussed, pixels can take any value ranging from 0 to 255. Higher the pixel value brighter the pixel. As shown in the colour graph parallel to the x-axis: high and low pixel values represent white and black colours respectively.

Histogram Equalization

As can be seen in the histogram, most of the pixels have intensity values between 0 to 50. The little part of the image that the human eye can recognize as white is also black (Not completely). In other words, the black part of the image is black and white part of the image is not white but it is “less black”. What if we can represent the white part with higher intensity while not affecting the black part (much). By doing so we can achieve more contrast in the image. This technique is nothing but, Histogram Equalization. Now we will look into mathematical representation for the same.

Math Behind the Histogram Equalization

The goal is to achieve a uniform pdf distribution as shown below:

Source: Histogram Equalization, Source: Gonzalez and Woods

Here L is the maximum value a pixel can achieve. Pr(r) is probability density function (pdf) of the image before equalization. Ps(s) is pdf of the image after performing equalization. Ps(s) is an equalized histogram that is uniformly distributed among all the possible values. Transfer function shown below converts Pr(r) to Ps(s). (Assume, for now, we will prove it in next few minutes)

Source: Author

Now, differentiation of s with respect to r is:

Relation between Pr(r) and Ps(s) can be achieved as:

So, as can be seen, Ps(s) is normalized distribution. We can say that equalization of the histogram can be achieved by an assumed transfer function.

Note: Probability density function(PDF) is a mathematical term that represents the probability of a random variable. Actually, In case of an image, we have a fixed number of pixels on the y-axis and not the probability of no. of pixels. But it is a mathematical convention to represent the same. So, do not get confused looking at pdf. Pr(r=r1) is just the number of pixels that have intensity value r=r1.

Now we will see MATLAB code which equalizes the histogram of the image and generates a new image having an equalized histogram.

MATLAB code

Read the image and convert it to a grayscale image

clc;
clear all;
I=imread('university.png');
original_img=rgb2gray(I); % conversion of image from rgb to gray scale format
[r,c]=size(original_img);

Define a variable named histogram having index 1 to 256. Possible pixel values are 0 to (28-1)=255 because each pixel is represented using 8 bits.

histogram=zeros(1,256);

Calculate histogram (frequency of each possible pixel value) for an original image

for i=1:r
for j=1:c
z=original_img(i,j);
histogram(z+1)=histogram(z+1)+1; %Formation of Historam of the image
end
end

Implement transfer function using the formula discussed previously. Here, summation is used instead of integration because the variable used is discreet.

histogram = histogram/(r*c);
[rh,ch] = size(histogram);
sum = 0;
histogram(1,4)
transfer_function=zeros(1,256);
for i =1:ch 
a= histogram(1,i); 
if i==1 
sum=a; 
else
j=i-1;
b=transfer_function(1,j);
sum = a + b;
end
transfer_function(1,i) = sum;
end
histo_image= transfer_function .* 255;

Generate an enhanced image from an original image using transfer function

equalized_img = uint8(zeros(r,c));
for i = 1:1:r
for j = 1:1:c
equalized_img(i,j) = histo_image(original_img(i,j) + 1);
end

Calculate histogram for equalized image

equalized_histogram=zeros(1,256); %Possible pixel values 0--->1 to (2^8-1)=256--->256
%each pixel value is represented using 8-bits
for i=1:r
for j=1:c
z=equalized_img(i,j);
equalized_histogram(z+1)=equalized_histogram(z+1)+1; %Formation of Historam of the image
end
end

Plot the results:

subplot 221;
imshow(original_img);
title('Original iamge');

subplot 222;
stem(histogram) %ploting of image histogram
title('Histogram of original iamge');

subplot 223;
imshow(equalized_img);
title('Image constructed using Equalized Histogram');

subplot 224;
stem(equalized_histogram); %ploting of image histogram
title('Equalized Histogram');
Source: Author

We can see that contrast in an image can be increased by equalizing its histogram. The new image we get from the equalized histogram has higher contrast and details than the original one. Let’s take a close look at the new image.

Source: Author

There are variations of the histogram equalization. A few of them are discussed next.

Types of Histogram Equalization

In this section, we will be exploring different types of histogram equalizations:

Global Histogram Equalization

source:  Birds image

Global histogram equalization (GHE) is the most simple type of histogram equalization. As the name suggests, here, the algorithm is applied to the whole image. This is the same one discussed throughout this article. But, there is a problem with this technique. If the image already has very high contrast (it consist of both very bright and very dark parts), then small details will get removed while equalizing it. In other words, GHE is not suitable for images having a histogram that is distributed at both extremes of pixel values. We can see the effect in the image shown above.

Local Histogram Equalization

In Local histogram equalization (LHE), the algorithm is applied to a local group of pixels of the image. First of all, the image is divided into equal small regions that are known as tiles. Then the algorithm is applied to each tile, separately. This solves the problem phased by GHE. But it faces another problem. As can be seen in the image, tiles are clearly visible due to high contrast at edges. Due to this problem, this is the least used algorithm.

Contrast-Limited Adaptive Histogram Equalization (CLAHE)

Contrast-Limited Adaptive Histogram Equalization (CLAHE) can be divided into two parts

  • Adaptive Histogram Equalization and
  • Contrast-Limiter

The Contrast-Limiter reduces noise from the image by limiting contrast. Specifically, Contrast-Limiter defines limits of contrast between 14 neighbour pixels. Adaptive Histogram Equalization operates in small regions of the image (tiles). In MATLAB the default tile size is 8X8 pixels. Similar to last technique, the algorithm is applied to each tile. After that, the system combines neighbouring tiles using bilinear interpolation to eliminate boundaries (which were visible in case of LHE). Bilinear interpolation follows the algorithm which is given below.

For example, a matrix is given below (It can be assumed that this is 9 neighbouring pixels’ values).

And we want to translate this image 0.5 pixels to the positive horizontal direction using bilinear interpolation. First zero pad the input matrix and translate it by 0.5 pixels to the right. Now, the new matrix will be:

Then take a group of two non-overlapping columns and find the mean of two elements of a single row. There are 4 sets of non-overlapping columns in this matrix. So, the output matrix will have 4 columns. The output matrix will bee.

We can say that Contrast-Limited Adaptive Histogram Equalization solves problems phased by GHE as well as LHE. So, it is the most efficient and useful one among all three discussed.

GHE and CLAHE are compared in the brain image given above. From this image, we can conclude that Contrast-Limited Adaptive Histogram Equalization gives the best output.

What is Histogram Equalization mainly used for with example?

  • Improve image contrast: Histogram equalization can make images brighter and easier to see by redistributing the pixel values in the image. This is especially useful for low-contrast images.
  • Reveal details hidden by poor contrast: It can also make it possible to see details in images that are hidden by poor contrast. This can be helpful for medical imaging, security surveillance, and photography.
  • Improve image processing and computer vision results: It can improve the accuracy of image processing and computer vision algorithms, such as object detection and tracking algorithms, image classification algorithms, and image segmentation algorithms.
  • Enhance medical images: It is a popular technique for enhancing medical images, such as X-rays, MRI scans, and CT scans. By improving the contrast of medical images, histogram equalization can make it easier for doctors to diagnose diseases and make better treatment decisions.
  • Improve photo quality: You can also use it to improve the quality of photographs. For example, it helps make photos that are too dark or too bright look more natural and balanced. It also enhances the colors in a photo.

Example

Imagine that you have a photo of a landscape with a mountain in the background. The mountain is difficult to see because it has the same brightness as the sky. You can use histogram equalization to make the mountain more visible by spreading out the pixel values in the image. This will make the mountain brighter and the sky darker, resulting in a more contrasting image.

Conclusion

Histogram equalisation is a simple and effective approach for improving image contrast. One can choose wisely from the several types of algorithms based on his or her needs. When the majority of the data is concentrated at low or high intensities, global histograms can be useful. When both sides have information (high and low intensity), it can’t produce good results. But here, a local histogram can be employed. However, some information near the tile’s border may disappear. CLAHE solves both of these issues. You can use these techniques for a variety of applications to simplify picture analysis.

Read more blogs on our website. Click here.

References

  1. Gonzalez, Rafael C., Richard E. Woods, and Barry R. Masters. “Digital image processing.” (2009): 029901-029901.
  2. Coursera course: Image and Video Processing: From Mars to Hollywood with a stop at the Hospital, Duke University
  3. https://in.mathworks.com/

Frequently Asked Questions

Q1. What are the limitations of histogram equalization?

Histogram equalization can introduce artifacts into the image, such as noise and banding. It is important to use histogram equalization carefully and to adjust the parameters to achieve the desired results.

Q2. Are there any alternatives to histogram equalization?

Yes, there are a number of alternatives to histogram equalization, such as contrast stretching, adaptive histogram equalization, and gamma correction. The best technique to use will depend on the specific image and the desired results.

Q3.What is gamma correction?

Gamma correction adjusts the brightness and contrast of an image. It often works alongside histogram equalization to achieve the desired results.

Q4.Can I use histogram equalization on color images?

Yes, histogram equalization can be used on color images. However, it is important to apply it separately to each color channel (red, green, and blue). Otherwise, the colors in the image may be distorted.

Q5.What are some other image processing techniques that I can use to enhance the contrast of my images?

In addition to histogram equalization, several other image processing techniques enhance image contrast, such as:

1. Contrast stretching
2. Adaptive histogram equalization
3. Gamma correction
4. Unsharp masking
5. High pass filtering

The best technique to use will depend on the specific image and the desired results.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

I am last year undergraduate in EC engineering at Nirma University, Ahmedabad. My hometown is Bhavnagar, Gujarat. My areas of interest include Machine Learning, Communication Systems and Digital Logic Design.

Responses From Readers

Clear

Werner Jäger
Werner Jäger

Really great Article! Unfortunately I do not understand why the relation between Pr(r) and Ps(s) can be achieved as Ps(s) = Pr(r) abs(ds/dr) Wished you could elaborate on tat a bit more .. Thanks so much

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details