cl; img=imread('mask.jpg'); imshow(img); [x,y]=size(img); img_man=zeros(x,y); img_com=zeros(x,y); %% 直方图均衡化算法 Max=max(max(img)); Min=min(min(img)); Hist=zeros(1,256); for i=1:x for j=1:y Hist(img(i,j)+1)=Hist(img(i,j)+1)+1; end end figure,plot(Hist); p=zeros(1,256); for i=1:256 p(i)=Hist(i)/(x*y); end figure,plot(p); c=zeros(1,256); for i=1:256 c(i)=sum(p(1:i)); end figure,plot(c); for i=1:x for j=1:y img_man(i,j)=c(img(i,j)+1)*(Max-Min)+Min; end end figure,imshow(uint8(img_man)) Hist2=zeros(1,256); for i=1:x for j=1:y Hist2(img_man(i,j)+1)=Hist2(img_man(i,j)+1)+1; end end figure,plot(Hist2);
%% matlab直方图均衡化函数 img_com=histeq(img); figure,imshow(img_com) Hist3=zeros(1,256); for i=1:x for j=1:y Hist3(img_com(i,j)+1)=Hist3(img_com(i,j)+1)+1; end end figure,plot(Hist3);