先贴个代码,等有空补充原理
function J=HistgramEqualize(I)
row=size(I,1);
col=size(I,2);
N=zeros(1,256);%直方图,此处不能使用‘uint8’,否则会超出上限255
A=zeros(1,256);%累加数组
LUT=zeros(1,256,'uint8');%查找表
for i=1:row
for j=1:col
k=I(i,j);
N(k+1)=N(k+1)+1;
end
end
A(1)=N(1);
sum=0;
for g=2:256
A(g)=A(g-1)+N(g);
if(N(g))
sum=sum+1;
end
end
% sum=255;%灰度级直接设为0-255
for g=1:256
LUT(g)=uint8(sum*A(g)/A(256));
end
J=zeros(row,col,'uint8');
for i=1:row
for j=1:col
k=I(i,j);
J(i,j)=LUT(k+1);
end
end