Histogram Equalization Without Using histeq() Function in MATLAB


In digital image processing, histogram equalization is a common method of improving the visual quality of a digital image. We can perform histogram equalization of a digital image using MATLAB. We have a built-in function "histeq" in MATLAB to perform this task. However, we can also perform histogram equalization without using the MATLAB’s "histeq" function.

In this tutorial, I will show you the process of histogram equalization without using "histeq" function in MATLAB. But before that let’s get an overview of histogram and histogram equalization in digital image processing.

What is a Histogram and Histogram Equalization?

In digital image processing, a graphical representation which is used to display the distribution of pixel intensity values in a digital image is referred to as a histogram.

Sometimes, in an image, the pixel intensities are higher in a specific region resulting in poor contrast levels and visual quality of the image. There is a technique called histogram equalization that allows to redistribute the pixel intensities of the image to get an enhanced image.

Histogram equalization is one of the common techniques that make the histogram of an image uniform to improve its contrast.

Let us now discuss the process of histogram equalization without using the "histeq" function in MATLAB.

Histogram Equalization Without Using Histeq Function in MATLAB

We can utilize MATLAB to perform histogram equalization of an image. For this, MATLAB provides a built-in function "histeq". However, we can also perform histogram equalization without using this function.

Here are the steps to be followed to equalize the histogram of an image without using the "histeq" function in MATLAB.

  • Step (1) − Read the input image using the "imread" function.

  • Step (2) − Convert the input image to grayscale if required. For this, use the "rgb2gray" function.

  • Step (3) − Create the histogram of the grayscale image using the "imhist" function.

  • Step (4) − Calculate the probability density function of the image.

  • Step (5) − Calculate the cumulative distribution function of the image.

  • Step (6) − Equalize the histogram.

  • Step (7) − Display the results.

This is how we can perform histogram equalization of an image without using the "histeq" function in MATLAB.

Example

Now, let us take an example to understand the implementation of these steps in MATLAB programming.

% MATLAB code to perform histogram equalization without using histeq function
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425269.jpg');

% Convert the input image to grayscale if necessary
gray_img = rgb2gray(img);

% Create the histogram of the input image
hist = imhist(gray_img);

% Calculate the probability density function of the image
PDF = hist / numel(gray_img);

% Calculate the cumulative distribution function
CDF = cumsum(PDF);

% Equalize the histogram
eq_hist = uint8(255 * CDF(gray_img + 1));

% Display the input and equalized images and their histograms
figure;
subplot(2, 2, 1), imshow(gray_img), title('Input Image');
subplot(2, 2, 2), imhist(gray_img), title('Input Image Histogram');
subplot(2, 2, 3), imshow(eq_hist), title('Equalized Image');
subplot(2, 2, 4), imhist(eq_hist), title('Equalized Image Histogram');

Output

When you run this code, it will produce the following output −

Code Explanation

In this MATLAB example, we start by reading the input image using the "imread" function and store it in a variable "img". Next, we convert the input image to grayscale by using the "rgb2gray" function and store the resulting image in a variable "gray_img".

After that, we calculate the compute the histogram of the image using the "imhist" function and store the result in the variable "hist". Then, by using this histogram, we calculate the probability density function of the image and store the result in the variable "PDF". Next, we compute the cumulative distribution function of the image using the "cumsum" function and store the result in the "CDF" variable.

Then, we perform histogram equalization of the image utilizing the "CDF" and store the enhanced image in the variable "eq_hist".

Finally, we use the "imshow" function to display the input and equalized images and the "imhist" function to display their histograms.

Conclusion

This is all about histogram equalization without using MATLAB’s "histeq" function. In this tutorial, I explained the step-by-step process to perform the histogram equalization of an image without using the "histeq" function. I have also included an example in MATLAB programming to demonstrate the implementation of the steps involved.

Updated on: 05-Oct-2023

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements