%导入并显示图像
I =imread('Fig5.jpg');
figure;imshow(I);[m,n]=size(I);
x =[1:256];
histogram =zeros(1,256);%初始化统计直方图矩阵
histogram1 =zeros(1,256);%初始化概率直方图矩阵
s =zeros(1,256);%初始化累计直方图矩阵
%补充:统计直方图
%行的索引:i,[1 m]%列的索引:j,[1 n]
s1 =zeros(1,256)for i =1: m
for j =1: n
s1(uint8(I(i, j))+1)=s1(uint8(I(i, j))+1)+1;
end
end
s2 =zeros(1,256);for i =1:256s2(i)=s1(i)/ m / n;
end
%figure(1),plot(x, s1);%补充归一化直方图:histogram1
%用点除./实现归一化
s2 =zeros(1,256);for i =1:256s2(i)=s1(i)./(m * n);
end
%figure(2),plot(x, s2);%补充:直方图均衡化
%补充计算累积直方图
s3 =zeros(1,256);s3(1)=s2(1)for i =2:256s3(i)=s3(i -1)+s2(i);
end
%figure(3),plot(x, s3);%补充:映射
s4 =zeros(1,256)for i =1:256s4(i)=uint8(s3(i)*255);
end
%补充:显示直方图均衡化处理的图像
I1 = I
for i =1:256for j =1:256I1(i, j)=s4(uint8(I(i, j)+1));
end
end
figure;Imshow(I1);