课本3.3直方图均衡化中例子的实现,课本25页。
课本内容:
matlab代码:
close all;clear;clc
f=[110,0,120,120,130;110,140,130,110,130;120,140,120,120,170;130,120,0,120,170];
imwrite(uint8(f),'D:\Download\teach1.bmp');
I1=imread('D:\Download\teach1.bmp');
[M,N]=size(I1);
subplot(2,2,1);
imshow(I1); %显示原图
title('original image');
subplot(2,2,2);
h1=imhist(I1);
bar(h1); %显示原图的灰度直方图
axis([0 256,-inf,inf]);%可有可无,对直方图的坐标进行限制
%求变换函数
s=[];%建立空列表,方便往里写数据
s(1)=255*h1(1)/M/N;
for k=2:255
s(k)=s(k-1)+255*h1(k)/M/N;%课本25页3-3,经过处理后的样子(课本中的L为256)
end
%%绘制变换后的图像
I2=[];
for m=1:M
for n=1:N
for k=1:255
if I1(m,n)==k %遍历I1中的所有像素点
I2(m,n)=round(s(k));%取整
end
end
end
end
I2=uint8(I2); %由于运算后函数为double型,因此需用此句转换为uint8型以显示
subplot(2,2,3);
imshow(I2);
subplot(2,2,4);
h2=imhist(I2);
bar(h2);
运行结果: