A=imread('nv.png');
[i,j]=size(A); %图像的行和列
count = zeros(1,256);
for r=1:256
count(r)=length(find(A == (r-1))); %建立直方图
end
count=count./(i*j); %各灰度的概率
A2=A; %新建空图像
func=zeros(1,256);
counts_sum=0;
for k=1:256
counts_sum=counts_sum + count(k);%各阶段概率之和
func(k)=(256-1)*counts_sum;%量化
end
func2=round(func);%取整
for h = 1:i
for w = 1:j
for k = 1:256
if A(h,w) == k-1
A2(h,w) = func(k); %在同一坐标位置上重新赋值
break;%防止重复赋值
end
end
end
end
subplot(121),imshow(A);
subplot(122),imshow(A2);