Open In App

Histogram Equalization Without Using histeq() Function in MATLAB

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

Histogram Equalization is the most famous contrast management technique for digital image processing with different image data intensity level values. or we can say it’s a Pixels brightness transformations technique. 

The histogram is basically a graph-based representation method that clarifies the numbers of pixels and intensity values.

Subtopics of Histogram Equalization 

  • Image Enhancement
  • Preprocessing
  • Segmentation
  • Classification

Image Enhancement: The process of manipulating given images and adjusting the digital image, so that our outputs are more suitable for further preprocessing and analyzing. 

Preprocessing: Preprocessing is the step to format the image or image data. using the preprocessing we can identify the – 

  • Image data transformation
  • Image data quality
  • Image data reduction
  • Image data cleaning

Segmentation: Segmentation is the process of partitioning the image into parts or in regions.

Steps: 

Step-1 First of all open your MATLAB and create the new script for Histogram Equalization and write the code, and please make sure 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 error like the image is doesn’t exists. but here we select the GFG logo image , then only you can perform the Histogram Equalization of an image.

Write the code and run in MATLAB editor for show the image in figure1

Matlab




% Program of Histogram Equalization without
% using built-in histeq() function.
  
clear all;
close all;
clc;    
% clear all workshop variable.
warning off;
I = imread('GeeksforGeeks.png');
  
% Read the image.
imshow(I); 
%showing the image in figure.


Using imread(‘ ‘); function MATLAB can read the image from the file specify by filename. and imshow() function is used for displaying the image in 2-D numeric values.

Output:

 

Step 2:   So, here we will implement our code and run then we will get a new histogram plot of the original image, without using the built-in histeq() function. so let’s see the code. 

Matlab




% A Program for perform histogram Equalization without histeq() function.
clc
close all
clear all
warning off;
  
% Read the image.
I = imread('GeeksforGeeks.png'); 
h = zeros(1,256);  
% return an array, 
% where the element's value as 0.
% traversing the array of an image.
[r c] = size(I);   
n = 0 : 255;
  
% for loop for travers.
for i= 1:r          
    for j=1:c
         h(I(i,j)+1) = h(I(i,j)+1)+1;
    end 
      
     % plot the data sequence.
stem(n, h);  
  
 % limits the number of updates
 % to 20 frames per second
drawnow limitrate;  
end  
figure;
histogram(I);


As you can see in the above code we added some commands and function, loop. so let’s understand what commands we used here.

  • h=zeros() it’s use to return an array, where the element’s value is 0.
  • [rows,cols]= size(Matrix); it’s used for traversing a matrix of an image column-wise.
  • stem(); it’s used to plot the data sequence and extend from a baseline along the axis.
  • drawnow limitrate; it is used to limit the number of updates to 20 frames per second.

 Output:

figure1 – histogram plot

So, in this figure1 as we can see the histogram plot of the image with different intensity level values.

Step 3: Now we will see the final step with histogram Equalization without using histeq() function. implement the code and run the code then you will get new figure1 as a histogram plot.

Matlab




% A Program for perform histogram 
% Equalization without histeq() function.
clc
close all
clear all
  
% Read the image.
I = imread('GeeksforGeeks.png');  
  
% return an array,
% where the element's value as 0.
h = zeros(1,256);  
  
% traversing the array of an image.
[r c] = size(I);    
no_of_pixels = r*c;
n = 0 : 255; title('histogram plot without histeq() function');
  
% loop for travers 
for i= 1:r      
    for j=1:c
         h(I(i,j)+1) = h(I(i,j)+1)+1;
    end 
end 
  
% Calculating Probability
for i=1:256
    h(i)=h(i)/no_of_pixels;
end
  
% Calculating Cumulative Probability
temp=h(1);
for i=2:256
    temp=temp+h(i);
    h(i)=temp;
end
stem(n, h);
drawnow limitrate; 
histogram(I);


In this code, we can use a different type of function that will give the output according to functions. like instead of steam we can use subplot() function that will output according to their functionality.

Output:

figure-1 Histogram Equalized plot

So the above diagram is created by MATLAB and here we used the steam() function that will give the output, basically, the steam function has plotted a particular line for each gray sale which is a little bit difficult to visualize the plot and their intensity values. so sometimes we can use the subplot function for plotting the histogram.



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

Similar Reads