Histogram Assignment
Histogram Assignment
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 , 0iL
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 1 2 3 4 5 The above histogram can be plotted using Graph Histogram Count 4 3 5 2 1
Histogram Equalization
, 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:
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))
Ideally MSE value should be Zero & Practically as small as Possible. Resultant MSE value for Equalized Image is MSE = 804.9859
OR
PSNR=10*log10MAXiMSE
Where MAXi=Maximum possible pixel Value MSE= Mean Square Error Resultant MSE value for Equalized Image is PSNR= 19.0045