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:
This article was published as a part of the Data Science Blogathon.
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.
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.
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.
The graph shown above is nothing but the Histogram of the data given in the table. After showing this plot it became something like..
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 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.
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.
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.
The goal is to achieve a uniform pdf distribution as shown below:
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)
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.
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');
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.
There are variations of the histogram equalization. A few of them are discussed next.
In this section, we will be exploring different types of histogram equalizations:
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.
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) can be divided into two parts
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.
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.
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.
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.
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.
Gamma correction adjusts the brightness and contrast of an image. It often works alongside histogram equalization to achieve the desired results.
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.
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.
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