为图像中的每个灰度值分配新值。
clc
close all
clear all
%% HISTOGRAM EQULAIZER
%%
I1= imread ('C:\Users\sepideh\Pictures\dip\PC040311.jpg');
zz=rgb2gray(I1);
figure,subplot(1,2,1),imshow(zz), title('original image')
subplot(1,2,2),imhist(zz),title('original image histogram')
%% Calculating the CDF
hst=imhist(zz);
j=1;
cdff(1,1)=hst(1,1);
for i=2:256
cdff(i)=hst(i)+cdff(i-j);
end
cdff1=cdff';
cdf_min=min(cdff);
[row col]=size(zz);
mn=row*col;
figure, plot(cdff), title('CDF of Image')
%% calcuting new intensity
for indx=1:length(cdff)
h(indx)=round((cdff(indx)-cdf_min)/(mn-cdf_min)*255);
end
h1=h';
figure,plot(h1), title('New value for General Histogram')
%% EQULIZED IMAGE
HIm=uint8(zeros(size(zz,1),size(zz,2)));
for i=1:row;
for j=1:col;
HIm(i,j) = h((zz(i,j)+1));
end
end
figure,subplot(1,2,1),imshow(HIm), title('Equlized Image')
subplot(1,2,2),imhist(HIm) ,title('Equlized image histogram')