You are on page 1of 7

Assignment No-

Title- Histogram of Image & Histogram Equalization

Submitted By- Mr. Gaikwad A. S.

Class- ME First Year (Signal Processing)

___________________________________________________________________________

Histogram
This histogram is a graph showing the number of pixels in an
image at each different intensity value found in that image. For
an 8-bit grayscale image there are 256 different possible
intensities, and so the histogram will graphically display 256
numbers showing the distribution of pixels amongst those
grayscale values.
Mathematical Expression for Histogram is
PXi=Px=i=nin , 0≤i≤L
L being the total number of gray levels in the image, n being the total number of pixels in the
image, and PXi being in fact the image's histogram for pixel value i, normalized to [0,1].
Example:
Consider 1D Data X(n)=[1, 2, 3, 4, 1, 3, 4, 3, 2, 3, 5, 3, 2, 1, 1]
Then Histogram of above 1D Data is
Data Histogram Count
1 4
2 3
3 5
4 2
5 1
The above histogram can be plotted using Graph

Histogram Equalization
Let us also define the cumulative distribution function corresponding to Pxas

which is also the image's accumulated normalized histogram.

We would like to create a transformation of the form y = T(x) to produce a new image {y},
such that its CDF will be linearized across the value range, i.e.

For some constant K. The properties of the CDF allow us to perform such a transform. It is
defined as

Notice that the T maps the levels into the range [0, 1]. In order to map the values back into
their original range, the following simple transformation needs to be applied on the result:

Matlab Code for Histogram Equalization


Software Requirement- Matlab 7.9
Main Code-

%% Clear all work space & close all window

close all;
clc;
clear all;

%% Read image in imgorig variable

imgorig=imread('cameraman.tif');
figure,imshow(uint8(imgorig));
title('Original Image');

[m,n]=size(imgorig);
histt(1,1:256)=0;

%% histogram calculation in histt

for i=1:m
for j=1:n
index=imgorig(i,j);
histt(1,index+1)=histt(1,index+1)+1;
end
end

%% show the histogram of image

figure,bar(0:255,histt,0.001);
title('Histrogram of Original Image');
xlabel('Gray Level L');
ylabel('Histogram Count n');

%% commulative Distributation function CDF

for i=0:255
sum1=0;
for j=0:i
sum1=sum1+histt(1,j+1);
end
cdfx(1,i+1)=sum1;
end

%% show CDF
figure,bar(0:255,cdfx,0.001);
title('CDF');

%% Histogram Equalization

z=sort(cdfx);
count=1;

while(z(1,count)==0)
count=count+1;
end
temp=z(1,count);
cdfmin=temp;

for i=1:m
for j=1:n
v=imgorig(i,j);
c=cdfx(1,v+1);
if(c~=0)
calcul=((cdfx(1,v+1)-cdfmin)/((m*n)-cdfmin))*255;
imgnew(i,j)=round(calcul);
end
end
end
imgnew1=uint8(imgnew);

%% show Histogram Equalized Image

figure,imshow(uint8(imgnew));
title('Equalized Image');

%% Histogram of Equalized Image


[m,n]=size(imgnew1);
histt(1,1:256)=0;

for i=1:m
for j=1:n
index=imgnew1(i,j);
histt(1,index+1)=histt(1,index+1)+1;
end
end

figure,bar(0:255,histt,0.001);
title('Histrogram of Equalized Image');
xlabel('Gray Level L');
ylabel('Histogram Count n');

%% MSE & PSNR Calculation


msee=0;
[m,n]=size(imgnew1);
cc=double((double(imgorig)-double(imgnew1)).^2);

for i=1:m
for j=1:n
msee=msee+cc(i,j);
end
end
msee=msee/(m*n)
maxx=double(max(max(imgorig)))
psnrr=10*log10((maxx.^2/msee))

Result of Histogram Equalization


Fig. 1 Original Image Named as Cameraman.tif

Fig. 2 Histogram of Original Image


Fig. 3 Cumulative Distribution Function

Fig. 4 Histogram Equalized Image


Fig. 4 Histogram of Equalized Image

MSE- Mean Square Error

MSE=i=1mj=1n[xi,j-x ' (i,j)]2m*n

Where x = Original Image

x'= New Output image

Ideally MSE value should be Zero & Practically as small as Possible.

Resultant MSE value for Equalized Image is MSE = 804.9859

PSNR- Peak Signal to Noise Ratio

PSNR=10*log10MAXi2MSE

OR

PSNR=10*log10MAXiMSE

Where MAXi=Maximum possible pixel Value

MSE= Mean Square Error

Resultant MSE value for Equalized Image is PSNR= 19.0045

You might also like