1、数字图像处理之频率滤波 实验四、频域滤波一、 实验目的1 了解频域滤波的方法;2 掌握频域滤波的基本步骤。二、 实验内容1 使用二维快速傅立叶变换函数fft2( )及其反变换函数ifft2( )对图象进行变换;2 自己编写函数生成各种频域滤波器;3 比较各种滤波器的特点。三、 实验步骤1 图象的傅立叶变换a. 对图象 1.bmp 做傅立叶变换。 x=imread(1.bmp); f=fft2(x); imshow(real(f) %显示变换后的实部图像 figure f1=fftshift(f); imshow(real(f1)变换后的实部图像中心平移后图像b. 对图象 cameraman.t
2、if 进行傅立叶变换,分别显示变换后的实部和虚部图象。思考:对图象 cameraman.tif 进行傅立叶变换,并显示其幅度谱|F(U,V)|。结果类似下图。显示结果命令imshow(uint8(y/256)程序如下:x=imread(cameraman.tif);f=fft2(x);f1=fftshift(f);y0=abs(f);y1=abs(f1);subplot(1,3,1),imshow(x)title(sourceimage)subplot(1,3,2),imshow(uint8(y0/256)title(F|(u,v)|)subplot(1,3,3),imshow(uint8(y
3、1/256)title(中心平移)2 频域滤波的步骤a. 求图象的傅立叶变换得 F=fft2(x)b. 用函数F=fftshit(F) 进行移位c. 生成一个和F 一样大小的滤波矩阵 H .d. 用F和H相乘得到 G , G=F.*He. 求G的反傅立叶变换得到 g 就是我们经过处理的图象。这其中的关键就是如何得到H 。3 理想低通滤波器a. 函数dftuv( )在文件夹中,它用生成二维变量空间如: U V=dftuv(11,11)b. 生成理想低通滤波器U V=dftuv(51,51); D=sqrt(U.2+V.2); H=double(D close all Q=0.7F=imread(
4、cameraman.tif) U V=dftuv(size(F,1),size(F,2); D=sqrt(U.2+V.2); H=double(D= size(F,1)/2*Q); %修改系数Q为0.5,0.3,0.2 FF=fft2(F); G=FF.*H; imshow(real(fftshift(FF) figure imshow(real(fftshift(G)g=real(ifft2(G);figureimshow(uint8(g)在以原点为圆心,以D0为半径的圆内无衰减的通过所有频率而在该圆外切断所有频率的二维低通滤波器,称为理想低通滤波器。思考:观察理想低通滤波器不同滤波半径对滤
5、波结果的影响。即系数Q分别为0.7、0.5、0.3、0.2时,结果如何?并加以解释说明。close all Q1=0.7F=imread(cameraman.tif) U V=dftuv(size(F,1),size(F,2); D=sqrt(U.2+V.2); H=double(D= size(F,1)/2*Q1); %修改系数Q为0.5,0.3,0.2 FF=fft2(F); G=FF.*H; subplot(4,3,1),imshow(real(fftshift(FF) subplot(4,3,2),imshow(real(fftshift(G) title(Q=0.7)g=real(i
6、fft2(G); subplot(4,3,3),imshow(uint8(g) Q2=0.5F=imread(cameraman.tif) U V=dftuv(size(F,1),size(F,2); D=sqrt(U.2+V.2); H=double(D= size(F,1)/2*Q2); %修改系数Q为0.5,0.3,0.2 FF=fft2(F); G=FF.*H; subplot(4,3,4),imshow(real(fftshift(FF) subplot(4,3,5),imshow(real(fftshift(G) title(Q=0.5)g=real(ifft2(G); subpl
7、ot(4,3,6),imshow(uint8(g) Q3=0.3F=imread(cameraman.tif) U V=dftuv(size(F,1),size(F,2); D=sqrt(U.2+V.2); H=double(D= size(F,1)/2*Q3); %修改系数Q为0.5,0.3,0.2 FF=fft2(F); G=FF.*H; subplot(4,3,7),imshow(real(fftshift(FF) subplot(4,3,8),imshow(real(fftshift(G) title(Q=0.3)g=real(ifft2(G); subplot(4,3,9),imsh
8、ow(uint8(g) Q4=0.2F=imread(cameraman.tif) U V=dftuv(size(F,1),size(F,2); D=sqrt(U.2+V.2); H=double(D close allf=imread(cameraman.tif);PQ=size(f);U V=dftuv(PQ(1),PQ(2);D0=0.07*PQ(2);F=fft2(f,PQ(1),PQ(2); % 与F=fft2(f)相同H=exp(-(U.2+V.2)/(2*(D02); %高斯低通mesh(U,V,H)figure G=F.*H; imshow(real(fftshift(F) f
9、igure imshow(real(fftshift(G)g=real(ifft2(G);figureimshow(uint8(g)思考:使用二阶巴特沃兹滤波器对图象cameraman.tif进行低通滤波。H=1./(1+(D./d0).(2*n);d0取15、30、80,n=2表示二阶)close alld0=30;n=2;f=imread(cameraman.tif);PQ=size(f);U V=dftuv(PQ(1),PQ(2);D=sqrt(U.2+V.2);F=fft2(f,PQ(1),PQ(2); % 与F=fft2(f)相同H=1./(1+(D./d0).(2*n) %d0取1
10、5、30、80,n=2表示二阶) %高斯低通mesh(U,V,H)figuresubplot(1,3,1) G=F.*H; imshow(real(fftshift(F)subplot(1,3,2) imshow(real(fftshift(G)g=real(ifft2(G);subplot(1,3,3)imshow(uint8(g) 5. 高通滤波器(相当于锐化) a.我们可以参照理想低通滤波器的思路,得到 U V=dftuv(51,51); D=sqrt(U.2+V.2); H=double(D=115); Mesh(U,V,H)b.对图象cameraman.tif进行高通滤波 close
11、 allx=imread(cameraman.tif); U V=dftuv(size(x,1),size(x,2); D=sqrt(U.2+V.2); H=double(D= size(x,1)/2*0.4); F=fft2(x); G=F.*H; imshow(real(fftshift(F) figure imshow(real(fftshift(G)g=real(ifft2(G);figureimshow(uint8(g)w=histeq(g);figureimshow(w)高通滤波器会衰减傅里叶变换中的低频分量而不会扰乱高频信息。D0越高,图像边缘越清晰,失真越小。思考: 使用高斯高
12、通滤波器对图象cameraman.tif进行高通滤波。H1=1-exp(-(U.2+V.2)./(2*(D02);close allx=imread(cameraman.tif); U V=dftuv(size(x,1),size(x,2); D0=15 H=1-exp(-(U.2+V.2)./(2*(D02); F=fft2(x); G=F.*H;g=real(ifft2(G);subplot(3,2,1),imshow(uint8(g) title(D0=15)w=histeq(g);subplot(3,2,2),imshow(w) D0=30 H=1-exp(-(U.2+V.2)./(2
13、*(D02); F=fft2(x); G=F.*H;g=real(ifft2(G);subplot(3,2,3),imshow(uint8(g) title(D0=30)w=histeq(g);subplot(3,2,4),imshow(w) D0=80 H=1-exp(-(U.2+V.2)./(2*(D02); F=fft2(x); G=F.*H;g=real(ifft2(G);subplot(3,2,5),imshow(uint8(g) title(D0=80)w=histeq(g);subplot(3,2,6),imshow(w)用高斯高通滤波器对图像进行滤波后,相对于前面滤波器,图像会
14、更加平滑。四、实验心得与体会 通过这次实验,我学会并了解了许多有关数字图像处理方面的知识。以前只是看着课本上的内容学习,对很多知识只是生分的了解,但是不懂得如何应用,通过做实验,自己动手,对图像做一系列的处理和变换,也明白了图像滤波器对图像的影响作用,体会到了各种参数对数字图像的影响。巩固了课上的知识。通过此次实验也发现了自己的而许多问题,在对于MATLAB软件的应用上面我还有许多不足,尤其是对如何编写M文件方面很难掌握,仍有待加强。五、实验报要求1、 按照实验步骤做实验;2、 记录实验结果,并作出总结;3、 按照实验报告格式写出报告。附:有关dftuv() 的说明clear allclose
15、 allx=imread(cameraman.tif);xm,xn=size(x);M2=floor(xm/2);N2=floor(xn/2);%-U,V=dftuv(M,N) %零点在四周 u=0:(xm-1); v=0:(xn-1); idx=find(uxm/2); %找出U中满足条件的数据的位置 u(idx)=u(idx)-xm; idy=find(vxn/2); v(idy)=v(idy)-xn; V,U=meshgrid(v,u); D=sqrt(U.2+V.2); %由dftuv()生成的U,V构造D%-U1,V1 零点在中间u1=-M2:1:M2-1;v1=-N2:1:N2-1
16、;U1,V1=meshgrid(u1,v1);D1=sqrt(U1.2+V1.2); %由U1,V1构造 D1 %-ILPF by dftuv-H=double(D=50); F=fft2(x); G=H.*F; %不需要进行中心平移 g=ifft2(G);figure % tu 1 subplot(121); imshow(real(G); title(D=50,ILPF,by dftuv) subplot(122); imshow(uint8(real(g),); title(ifft)%-ILPF by u1,v1-H=double(D1=50); F=fft2(x); G=H.*fftshift(F); %需要中心平移 g=ifft2(ifftshift(G); %反中心平移figure % tu 2 subplot(121); imshow(real(G); title(D=50,ILPF,by u1,v1) subplot(122); imshow(uint8(real(g),); title(ifft)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1