直方图均衡
function Hisequalization()
%读取图像
input_image=imread('./a.bmp', 'bmp');
F=double(input_image);
[m,n]=size(F);
figure;
subplot(221);
imshow(uint8(F));
title('????');
subplot(222);
imhist(uint8(F));
His=zeros(1,256);%
for x=1:m
for y=1:n
M(x,y)=F(x,y);
a=F(x,y);
His(a+1)=His(a+1)+1;
end
end
for x=1:256
normHis(x)=His(x)/(m*n);%????????????????
if(x~=1)
cumuHis(x)=cumuHis(x-1)+normHis(x);%??????????
else
cumuHis(x)=normHis(x);
end
end
for i=1:256
Map(i)=floor(cumuHis(i)*255+0.5);%????
end
for x=1:m
for y=1:n
tmp=M(x,y)+1;
M(x,y)=Map(tmp);
end
end
subplot(223);
imshow(uint8(M));
title('??????');
subplot(224);
imhist(uint8(M));
%imwrite(uint8(M),'./ttttttt.bmp');
对于彩色图:
function Hisequalization2()
input_image=imread('./test.png', 'png');
F=double(input_image);
[m,n,o]=size(F);
figure;
subplot(241);
imshow(uint8(F));
title('????');
for z=1:3
for x=1:m
for y=1:n
m(x,y)=F(x,y,z);
end
end
subplot(2,4,1+z);
imhist(uint8(m));
end
for z=1:3
His=zeros(1,256);
for x=1:m
for y=1:n
M(x,y,z)=F(x,y,z);
a=F(x,y,z);
His(a+1)=His(a+1)+1;
end
end
sizeof(His);
normHis=zeros(1,256);
for x=1:256
normHis(x)=His(x)/(m*n);
if(x~=1)
cumuHis(x)=cumuHis(x-1)+normHis(x);
else
cumuHis(x)=normHis(x);
end
end
for i=1:256
Map(i)=floor(cumuHis(i)*255+0.5);
end
for x=1:m
for y=1:n
tmp=M(x,y,z)+1;
M(x,y,z)=Map(tmp);
end
end
for x=1:m
for y=1:n
m(x,y)=M(x,y,z);
end
end
subplot(2,4,z+5);
imhest(uint8(m));
end
subplot(225);
imshow(uint8(M));
title('??????');