例4.6 直方图均衡化效果实例。
%用Matlab中的histep函数实现直方图均衡化的程序如下: I=imread('circuit.tif'); figure(1)
subplot(221);imshow(I);
subplot(222);imhist(I) %imhist显示直方图 I1=histeq(I); %histeq直方图均衡化 figure(2);
subplot(221);imshow(I1) subplot(222);imhist(I1)
%直方图规定效果实例。
%用Matlab实现的程序如下: I=imread('circuit.tif'); [M,N]=size(I); for i=1:8:257 counts(i)=i; end
figure(1);plot(counts); Q=imread('circuit.tif');
N=histeq(Q,counts); %histeq直方图匹配 figure(2)
subplot(221);imshow(N); subplot(222);imhist(N); axis([0 260 0 5000]);
例4.9 用图象平均减少随机噪声。
%Matlab实现程序如下:
I=imread('zhx1.jpg'); [M,N]=size(I); II1=zeros(M,N); for i=1:16
II(:,:, i)=imnoise(I,'gaussian',0,0.01); %加高斯噪声 II1=II1+double(II(:,:, i));
if or(or(i==1,i==4),or(i==8,i==16)); figure;
imshow(uint8(II1/i)); end end
例4.10 用各种尺寸的模板平滑图象。
%领域平均法有力地抑制了噪声,同时也引起了模糊, %模糊程度与领域半径成正比。
%下面是Matlab实现的领域平均法抑制噪声的程序;
I=imread('tire.tif');
J=imnoise(I,'salt & pepper',0.02);
subplot(231),imshow(I);title('原图象');
subplot(232),imshow(J);title('添加椒盐噪声图象') k1=filter2(fspecial('average',3),J); %进行平板滤波 k2=filter2(fspecial('average',5),J); %进行平板滤波 k3=filter2(fspecial('average',7),J); %进行平板滤波 k4=filter2(fspecial('average',9),J); %进行平板滤波
subplot(233),imshow(uint8(k1));title('3*3模板平滑滤波'); subplot(234),imshow(uint8(k2));title('5*5模板平滑滤波'); subplot(235),imshow(uint8(k3));title('7*7模板平滑滤波'); subplot(236),imshow(uint8(k4));title('9*9模板平滑滤波')