Open In App

What Color Histogram Equalization in MATLAB?

Last Updated : 07 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Histogram is a graph-based representation technique between a number of pixels and intensity values. it is a plot of the frequency of occurrence of an event. So in this article, we will understand how we generate the and Equalize Histogram of a color image. 

Histogram Equalization

Histogram Equalization is a process for increasing the contrast in an image by spreading the histogram out to be approximately uniformly distributed. It is referred to as digital image processing basically it is used to enhance the contrast of the color image, it is one of the simplest and less complex methods for enhancing the quality of the color image.

For Enhancing the Histogram and Equalization MATLAB has histeq in-built function, using this histeq function we can enhance the contrast of the image by transforming the values in an intensity image. basically, this function returned the transformed colormap in newmap.

Steps for Histogram Equalization 

Step 1: First of all open your MATLAB and create the new script for Histogram Equalization and write the code, one more thing is that you have to ensure that whatever image you have selected for equalization that image should be inside the MATLAB editor, if your image is not there in the MATLAB then you will get an error like the image is doesn’t exist.  but here we select the GFG logo image, then only you can perform the Histogram Equalization of an image.

Matlab




% program for perform the Histogram Equalization.
close all;
clear all;
clc;
I = imread('GeeksforGeeks.png'); % Read the image.
I1 = imhist(I);  % create a histogram plot by define function.


  

Command Window
>> imshow(I1)
>> imhist(I)

After running the code in MATLAB you have to give the above command in MATLAB Command Window, then you will get the output as a figure1. 

Why we use these commands because of that, first of all, we save the value of (I) into (I1), but the in-built function imhist() it’s a histogram function that reads the values and plots the figure1 simultaneously.  

 

As you can see in figure-1 the histogram plot is the Vertical line which contains some numbers that show the intensity level values.

Step 2: Now we will implement a little bit of our code and see the original image with a histogram plot. earlier we got only a histogram plot. so let’s see the code.

Matlab




% program for perform the Histogram Equalization. 
clear all;
close all;
clc;
I = imread('GeeksforGeeks.png'); % Read the image.
subplot(211);
imshow(I);
subplot(212);  % divide the figure int m-by-n grid and create axes.
imhist(I);  % create a histogram plot by define function.


After implementing the code in MATLAB then just run the above code then you will get a new figure1 image with a histogram plot. 

 

Step 3: Now we will use the histogram function histeq() and create the new figure1. for that, we just implement our code following. 

Matlab




% Program for perform the Histogram Equalization.
close all;
clear all;
clc;
I = imread('GeeksforGeeks.png'); % Read the image using this function.
figure,
subplot(211);  % divide the figure into m-by-n grid and create axes.
imshow(I), title('Original Image')
subplot(212);
imhist(I);   % create a histogram plot by define function.
figure,
  
I2=histeq(I);  


So, histeq() function is used for equalizing the image in the above code this function equalizes the GFG image and creates a new figure2. But figure 2 will be blank because in figure 2 we haven’t saved anything. simple just run the code.

 

Step 4: Now we will see the histogram equalize the image in figure2, so for that, we just add the last command in the above code. we will use imshow() which will show the histogram equalize image in figure2, just run the code again you will get a new figure2.

imshow(I2);

histogram equalize image

As you can see figures before the equalization image and after equalization, the image both have some differences, figure1 image has some change intensity values which are averaged out and give the equalized image in figure2.

Step 5: So this is the final step, in this step we will see the equalized image with a histogram plot(graph). for creating the histogram plot we just use subplot() and imshow() functions showing the histogram equalized image with the histogram plot, so let’s see the implemented code below.

Matlab




% Program for perform the Histogram Equalization.
close all;
clear all;
clc;
I = imread('GeeksforGeeks.png'); % Read the image using this function.
figure,
subplot(211);  % divide the figure into m-by-n grid and create axes.
imshow(I), title('Original Image')
subplot(212);
imhist(I);   % create a histogram plot by define function.
  
figure,
  
I2=histeq(I);  
subplot(211)
imshow(I2); title('histogram equalized image')
subplot(212);
imhist(I2);


 

After writing the code in the MATLAB editor, just run the code again you will get equalized image with a histogram plot.

 

Now as you can see the output in figure2 equalized image histogram plot has some change in intensity level values compared to the original image histogram plot. in figure2 we got the average intensity level values. that is showing the difference between the original image and the histogram equalized image, so we can analyze both via their histogram plot.

If you want to perform this Histogram Equalization in your MATLAB, so it’s not essential to select the same image or any particular image you can select any random image and perform it.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads