Understanding Histogram Equalization for Image Enhancement Using MATLAB

NeuralNinja
3 min readMar 6, 2023

--

Introduction:

Image manipulation through mathematical procedures is known as image processing. One of the fundamental methods in image processing for improving contrast in images is histogram equalization. It is a non-linear technique that redistributes the intensity of the pixels in an image to generate a histogram that is more uniform. In this blog post we’ll go over a piece of code that applies the Histogram Equalization approach to an image.

Code Explanation:

For an image, the provided code applies histogram equalization. To better comprehend the code, let’s dissect it into its component pieces.

Firstly, the code reads an image using the “imread()” method and stores its dimensions in “r” and “c”. Then, two matrices named “himg1” and “pdf,” which will be used to determine the probability of pixels and the image’s histogram, respectively, are formed with zeros. The number of rows and columns in the image are multiplied to create the variable “val,” which will be used to determine the likelihood that pixels will be present. The algorithm then loops through each pixel in the image to determine the histogram and probability of each pixel. The code extracts the value of each pixel, updates the “himg1” matrix at the appropriate index, and uses the “pdf” matrix to determine the probability of the pixel.

The “cprob” matrix is used by the code to determine the cumulative probability of pixels after computing the probability of each individual pixel. Initialized with zeros, the “floor” matrix will eventually be utilized to floor the pixel values. As the first index of “cprob” has already been initialized with the probability of the first pixel, the loop begins from index 2. The probability of the current pixel is added to the probability of the previous pixel in the loop to update the “cprob” matrix. Finally, using the “round()” function to round the result to the nearest integer, it determines the value of the pixel to be used as flooring by multiplying the cumulative probability by the maximum intensity value (255 for an 8-bit image).

To store the equalized image, the code generates a new matrix called “hequalized img” with zeros. The program iterates over each pixel in the original image, adding 1 to its value before flooring it with the “floor” matrix. The corresponding pixel of the “hequalized img” matrix is then given the floor value.

Conclusion:

Finally, the code uses MATLAB to apply Histogram Equalization to an image. It computes the histogram and pixel probabilities for the image before applying the Histogram Equalization approach to compute the cumulative probabilities and floor the pixel values. The equalized image is then created in a new matrix as a last step. To improve the contrast of images, the fundamental image processing technique known as histogram equalization is performed.

%This code implements Histogram Equalization technique on an image using MATLAB.
%Reading the input image and storing its dimensions in r and c

original_img=imread('');
[r, c]=size(original_img);

%Creating matrices with zeros for calculating the histogram of the image and the probability of pixels himg1=zeros(r,c);
pdf=zeros(r,c);

%Multiplying rows and columns of the image to calculate val for probability of pixels
val=r*c;

%Setting the maximum intensity value of 8-bit image
I=255;

%Looping through each pixel of the image to calculate the histogram and probability of pixels
for i=1:r
for j=1:c
%Extracting the pixel value of the image
z=original_img(i,j);
%Updating the histogram matrix at the corresponding index
himg1(z+1)=himg1(z+1)+1;
%Calculating the probability of the pixel using the probability matrix
pdf(z+1)=himg1(z+1)/val;
end
end

%Initializing matrices with zeros for calculating the cumulative probability and flooring the pixel values
cprob=zeros(1,length(himg1));
floor=zeros(1,256);

%Looping through each pixel of the image to calculate the cumulative probability and floor the pixel values
for i=2:length(himg1)
%Calculating the cumulative probability by adding the probability of the current pixel to the probability of the previous pixel
cprob(i)=pdf(i)+cprob(i-1);
%Calculating the pixel value to be used for flooring by multiplying the cumulative probability by the maximum intensity value (255 for an 8-bit image) and rounding it to the nearest integer using the "round()" function
floor(i)=round(cprob(i)*I);
end

%Creating a new matrix with zeros for storing the equalized image
hequalized_img=zeros(r,c,'uint8');

%Looping through each pixel of the original image, flooring the pixel value using the "floor" matrix, and assigning the floor value to the corresponding pixel of the "hequalized_img" matrix
for i=1:r
for j=1:c %Adding 1 to the pixel value to match the index of the "floor" matrix
hequalized_img(i,j)=floor(original_img(i,j)+1);
end
end

--

--

No responses yet