Image(Index)=Project{m}(k);
end
subplot(2,4,(m-1)*4+4),imshow(Image,[]);
title(['变换后的结果图',num2str(m)]);
end
执行结果:
图2直方图规定化
实验结果分析:
由实验结果可知,采用直方图规定化技术后,原图的直方图逼近规定化的直方图,从而有相应的变换后的结果图1和变换后的结果图2。
3.灰度图像常用平常、锐化滤波
程序代码:
clearall;
closeall;
%0.原图
I=double(imread('lena.tiff'));
subplot(2,4,1);imshow(I,[]);title('原图');
%1.均值低通滤波
H=fspecial('average',5);
F{1}=double(filter2(H,I));
subplot(2,4,2);imshow(F{1},[]);title('均值低通滤波');
%2.gaussian低通滤波
H=fspecial('gaussian',7,3);
F{2}=double(filter2(H,I));
subplot(2,4,3);imshow(F{2},[]);title('高斯低通滤波');
%3.增强图像=原图-均值低通滤波
F{3}=2*I-F{1};
subplot(2,4,4);imshow(uint8(F{3}),[]);title('原图-均值低通滤波');
%4.增强图像=原图-高斯低通滤波
F{4}=2*I-F{2};
subplot(2,4,5);imshow(uint8(F{4}),[]);title('原图-高斯低通滤波');
%5.'prewitt'边缘算子增强
H=fspecial('prewitt');
F{5}=uint8(I+filter2(H,I));
subplot(2,4,6);imshow(F{5},[]);title('prewitt边缘算子增强');
%6.'soble'边缘算子增强
H=fspecial('sobel');
F{6}=uint8(I+filter2(H,I));
subplot(2,4,7);imshow(F{6},[]);title('sobel边缘算子增强');
执行结果:
图3灰色图像平滑、锐化
实验结果分析:
由实验结果可知,均值和高斯滤波都使原图模糊,而采用原图减去低通滤波图像方法、prewitt算子、sobel算子都可以增强图像边缘。
4.频率域滤波:
对于给定图像+噪声,使用不同的频域滤波器对图像进行滤波处理。
(1)用Butterworth低通滤波器实现图像信号的滤波运算。
程序代码:
clearall;
closeall;
%(a)读入并显示图像electric.tif;
I=imread('electric.tif');
subplot(2,3,1),imshow(I);title('原图像');
%(b)利用imnoise命令在图electric.tif上加入高斯(gaussian)噪声;
J=imnoise(I,'gaussian',0,0.01);
subplot(2,3,2),imshow(J);title('加入高斯噪声的图像');
%(c)用Butterworth低通滤波器实现图像信号的滤波运算,变换不同的截止频率d
I1=fftshift(fft2(J));
[m,n]=size(I1);
N=2;d1=30;d2=50;d3=70;d4=90;
n1=floor(m/2);n2=floor(n/2);
fori=1:
m
forj=1:
n
d=sqrt((i-n1)^2+(j-n2)^2);
H1=1/(1+(d/d1)^(2*N));
H2=1/(1+(d/d2)^(2*N));
H3=1/(1+(d/d3)^(2*N));
H4=1/(1+(d/d4)^(2*N));
I3(i,j)=H1*I1(i,j);
I5(i,j)=H2*I1(i,j);
I7(i,j)=H3*I1(i,j);
I9(i,j)=H4*I1(i,j);
end
end
I3=ifftshift(I3);
I4=real(ifft2(I3));
I5=ifftshift(I5);
I6=real(ifft2(I5));
I7=ifftshift(I7);
I8=real(ifft2(I7));
I9=ifftshift(I9);
I10=real(ifft2(I9));
subplot(2,3,3),imshow(I4,[]),title('Butterworth低通滤波器d1=30');
subplot(2,3,4),imshow(I6,[]),title('Butterworth低通滤波器d2=50');
subplot(2,3,5),imshow(I8,[]),title('Butterworth低通滤波器d3=70');
subplot(2,3,6),imshow(I10,[]),title('Butterworth低通滤波器d4=90');
执行结果:
图4Butterworth低通滤波器滤波结果
实验结果分析:
由实验结果可知,采用Butterworth低通滤波器对加噪声的图像进行滤波运算,滤波器的截止频率d越小,滤波后的图像越模糊。
(2)用理想低通滤波器实现图像信号的滤波运算
程序代码:
clearall;
closeall;
I=imread('electric.tif');
subplot(2,3,1),imshow(I);title('原图像');
F=imnoise(I,'gaussian',0,0.01);
subplot(2,3,2),imshow(F);title('加入高斯噪声的图像');
%傅里叶变换并把频谱中心移到中点
F1=fft2(F);
I1=fftshift(F1);
%构建理想低通滤波器
[m,n]=size(I1);
d1=30;d2=50;d3=70;d4=90;
n1=floor(n/2);n2=floor(m/2);
foru=1:
m
forv=1:
n
ifsqrt((u-n1)^2+(v-n2)^2)<=d1
H1(u,v)=1;
else
H1(u,v)=0;
end
end
end
foru=1:
m
forv=1:
n
ifsqrt((u-n1)^2+(v-n2)^2)<=d2
H2(u,v)=1;
else
H2(u,v)=0;
end
end
end
foru=1:
m
forv=1:
n
ifsqrt((u-n1)^2+(v-n2)^2)<=d3
H3(u,v)=1;
else
H3(u,v)=0;
end
end
end
foru=1:
m
forv=1:
n
ifsqrt((u-n1)^2+(v-n2)^2)<=d4
H4(u,v)=1;
else
H4(u,v)=0;
end
end
end
J1=I1.*H1;
J2=I1.*H2;
J3=I1.*H3;
J4=I1.*H4;
K1=ifft2(ifftshift(J1));
K2=ifft2(ifftshift(J2));
K3=ifft2(ifftshift(J3));
K4=ifft2(ifftshift(J4));
C1=real(K1);
C2=real(K2);
C3=real(K3);
C4=real(K4);
subplot(2,3,3);imshow(C1,[]);title('滤波结果图像,d1=30');
subplot(2,3,4);imshow(C2,[]);title('滤波结果图像,d2=50');
subplot(2,3,5);imshow(C3,[]);title('滤波结果图像,d3=70');
subplot(2,3,6);imshow(C4,[]);title('滤波结果图像,d4=90');
执行结果:
图5理想低通滤波器滤波结果
实验结果分析:
由实验结果可知,采用理想低通滤波器对加噪声的图像进行滤波运算,滤波器的滤波半径d越小,滤波后的图像越模糊,且出现明显的振铃现象,相比于巴特沃斯低通滤波器,理想低通滤波器的滤波效果会比较差。
(3)采用巴特沃斯高通滤波器对room.tif图像进行锐化滤波,并显示滤波结果图像。
程序代码:
clearall;
closeall;
%(d)读入并显示原始图像room.tif;
I=imread('room.tif');
subplot(1,3,1),imshow(I);title('原图像room');
%(e)采用巴特沃斯高通滤波器对room.tif图像进行锐化滤波,变换不同的滤波半径
I1=fftshift(fft2(I));
[m,n]=size(I1);
N=2;d1=10;d2=50;
n1=floor(m/2);n2=floor(n/2);
fori=1:
m
forj=1:
n
d=sqrt((i-n1)^2+(j-n2)^2);
H1=1/(1+(d1/d)^(2*N));
H2=1/(1+(d2/d)^(2*N));
I3(i,j)=H1*I1(i,j);
I5(i,j)=H2*I1(i,j);
end
end
I3=ifftshift(I3);
I4=real(ifft2(I3));
I5=ifftshift(I5);
I6=real(ifft2(I5));
subplot(1,3,2),imshow(I4,[]),title('Butterworth高通滤波器d1=30');
subplot(1,3,3),imshow(I6,[]),title('Butterworth高通滤波器d2=50');
执行结果:
图6巴特沃斯高通滤波器锐化滤波结果
实验结果分析:
由实验结果可知,采用Butterworth高通滤波器对图像进行滤波运算,滤波后图像的边缘和细节变的更加突出,是一种对图像的锐化处理。
三、实验中遇到问题及解决方法
实验中遇到的问题有:
初期对图像在频域空间的滤波不太了解,不懂得如何利用傅立叶变换进行频域滤波;解决的方法:
通过参考课本中例题及参考程序,逐步分析,加深理解。
四、实验心得体会
通过此次试验,初步掌握图像空域增强算法的基本原理并能在实际应用及MATLAB中实现;通过熟悉各类滤波器对图像处理的应用;加深对图像增强的感性认识,巩固所学理论知识。
五、源程序清单
%%
%1.基于幂次变换的图像增强
clearall;
closeall;
I{1}=double(imread('fig534b.tif'));
I{1}=I{1}/255;
figure,subplot(2,4,1);imshow(I{1},[]);holdon
I{2}=double(imread('room.tif'));
I{2}=I{2}/255;
subplot(2,4,5);imshow(I{2},[]);holdon
form=1:
2
Index=0;
forlemta=[0.55]
Index=Index+1;
F{m}{Index}=I{m}.^lemta;
subplot(2,4,(m-1)*4+Index+1),imshow(F{m}{Index},[])
end
end
%%
%2.直方图规定化处理
clearall
clc
closeall
%0.读图像
I=double(imread('lena.tiff'));
subplot(2,4,1);
imshow(I,[]);
title('原图')
N=32;
Hist_image=hist(I(:
),N);
Hist_image=Hist_image/sum(Hist_image);
Hist_image_cumulation=cumsum(Hist_image);%累计直方图
subplot(245);
stem(0:
N-1,Hist_image);
title('原直方图');
%1.设计目标直方图
Index=0:
N-1;
%正态分布直方图
Hist{1}=exp(-(Index-N/2).^2/N);
Hist{1}=Hist{1}/sum(Hist{1});
Hist_cumulation{1}=cumsum(Hist{1});
subplot(242);
stem([0:
N-1],Hist{1});
title('规定化直方图1');
%倒三角形状直方图
Hist{2}=abs(2*N-1-2*Index);
Hist{2}=Hist{2}/sum(Hist{2});
Hist_cumulation{2}=cumsum(Hist{2});
subplot(246);
stem(0:
N-1,Hist{2});
title('规定化直方图2');
%2.规定化处理
Project{1}=zeros(N);
Project{2}=zeros(N);
Hist_result{1}=zeros(N);
Hist_result{2}=zeros(N);
form=1:
2
Image=I;
%SML处理(SML,SingleMappingLaw单映射规则
fork=1:
N
Temp=abs(Hist_image_cumulation(k)-Hist_cumulation{m});
[Temp1,Project{m}(k)]=min(Temp);
end
%2.2变换后直方图
fork=1:
N
Temp=find(Project{m}==k);
ifisempty(Temp)
Hist_result{m}(k)=0;
else
Hist_result{m}(k)=sum(Hist_image(Temp));
end
end
subplot(2,4,(m-1)*4+3);
stem(0:
N-1,Hist_result{m});
title(['变换后的直方图',num2str(m)]);
%2.3结果图
Step=256/N;
forK=1:
N
Index=find(I>=Step*(k-1)&IImage(Index)=Project{m}(k);
end
subplot(2,4,(m-1)*4+4),imshow(Image,[]);
title(['变换后的结果图',num2str(m)]);
end
%%
%3.灰度图像常用平常、锐化滤波
clearall;
closeall;
%0.原图
I=double(imread('lena.tiff'));
subplot(2,4,1);imshow(I,[]);title('原图');
%1.均值低通滤波
H=fspecial('average',5);
F{1}=double(filter2(H,I));
subplot(2,4,2);imshow(F{1},[]);title('均值低通滤波');
%2.gaussian低通滤波
H=fspecial('gaussian',7,3);
F{2}=double(filter2(H,I));
subplot(2,4,3);imshow(F{2},[]);title('高斯低通滤波');
%3.增强图像=原图-均值低通滤波
F{3}=2*I-F{1};
subplot(2,4,4);imshow(uint8(F{3}),[]);title('原图-均值低通滤波');
%4.增强图像=原图-高斯低通滤波
F{4}=2*I-F{2};
subplot(2,4,5);imshow(uint8(F{4}),[]);title('原图-高斯低通滤波');
%5.'prewitt'边缘算子增强
H=fspecial('prewitt');
F{5}=uint8(I+filter2(H,I));
subplot(2,4,6);imshow(F{5},[]);title('prewitt边缘算子增强');
%6.'soble'边缘算子增强
H=fspecial('sobel');
F{6}=uint8(I+filter2(H,I));
subplot(2,4,7);imshow(F{6},[]);title('sobel边缘算子增强');
%%
%4.
(1)Butterworth低通滤波器实现图像信号的滤波运算
clearall;
closeall;
%(a)读入并显示图像electric.tif;
I=imread('electric.tif');
subplot(2,3,1),imshow(I);title('原图像');
%(b)利用imnoise命令在图像electric.tif上加入高斯(gaussian)噪声;
J=imnoise(I,'gaussian',0,0.01);
subplot(2,3,2),imshow(J);title('加入高斯噪声的图像');
%(c)用Butterworth低通滤波器实现图像信号的滤波运算,变换不同的截止频率d:
d1=30;d2=50;d3=70;d4=90
I1=fftshift(fft2(J));
[m,n]=size(I1);
N=2;d1=30;d2=50;d3=70;d4=90;
n1=floor(m/2);n2=floor(n/2);
fori=1:
m
forj=1:
n
d=sqrt((i-n1)^2+(j-n2)^2);
H1=1/(1+(d/d1)^(2*N));
H2=1/(1+(d/d2)^(2*N));
H3=1/(1+(d/d3)^(2*N));
H4=1/(1+(d/d4)^(2*N));
I3(i,j)=H1*I1(i,j);
I5(i,j)=H2*I1(i,j);
I7(i,j)=H3*I1(i,j);
I9(i,j)=H4*I1(i,j);
end
end
I3=ifftshift(I3);