数字图像处理之频率滤波.docx
《数字图像处理之频率滤波.docx》由会员分享,可在线阅读,更多相关《数字图像处理之频率滤波.docx(11页珍藏版)》请在冰点文库上搜索。
数字图像处理之频率滤波
实验四、频域滤波
一、实验目的
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.tif进行傅立叶变换,分别显示变换后的实部和虚部图象。
思考:
对图象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(y1/256))
title('中心平移')
2.频域滤波的步骤
a.求图象的傅立叶变换得F=fft2(x)
b.用函数F=fftshit(F)进行移位
c.生成一个和F一样大小的滤波矩阵H.
d.用F和H相乘得到G,G=F.*H
e.求G的反傅立叶变换得到g就是我们经过处理的图象。
这其中的关键就是如何得到H。
3.理想低通滤波器
a.函数dftuv()在文件夹中,它用生成二维变量空间
如:
[UV]=dftuv(11,11)
b.生成理想低通滤波器
>>[UV]=dftuv(51,51);
D=sqrt(U.^2+V.^2);
H=double(D<=15);
Mesh(U,V,H)
c.应用以上方法,对图象cameraman.tif进行低通滤波;
>>closeall
Q=0.7
F=imread('cameraman.tif')
[UV]=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));
figure
imshow(uint8(g))
在以原点为圆心,以D0为半径的圆内无衰减的通过所有频率而在该圆外切断所有频率的二维低通滤波器,称为理想低通滤波器。
思考:
观察理想低通滤波器不同滤波半径对滤波结果的影响。
即系数Q分别为0.7、0.5、0.3、0.2时,结果如何?
并加以解释说明。
closeall
Q1=0.7
F=imread('cameraman.tif')
[UV]=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(ifft2(G));
subplot(4,3,3),imshow(uint8(g))
Q2=0.5
F=imread('cameraman.tif')
[UV]=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));
subplot(4,3,6),imshow(uint8(g))
Q3=0.3
F=imread('cameraman.tif')
[UV]=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),imshow(uint8(g))
Q4=0.2
F=imread('cameraman.tif')
[UV]=dftuv(size(F,1),size(F,2));
D=sqrt(U.^2+V.^2);
H=double(D<=size(F,1)/2*Q4);%修改系数Q为0.5,0.3,0.2
FF=fft2(F);
G=FF.*H;
subplot(4,3,10),imshow(real(fftshift(FF)))
subplot(4,3,11),imshow(real(fftshift(G)))
title('Q=0.2')
g=real(ifft2(G));
subplot(4,3,12),imshow(uint8(g))
随着Q值越小,即滤波半径越小,滤波后的图像越模糊,且滤波半径变小会出现振铃效应。
随着滤波器半径的增大,滤除的功率越来越少,导致模糊也越来越减弱。
4.巴特沃兹低通滤波器
>>closeall
f=imread('cameraman.tif');
PQ=size(f);
[UV]=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*(D0^2)));%高斯低通
mesh(U,V,H)
figure
G=F.*H;
imshow(real(fftshift(F)))
figure
imshow(real(fftshift(G)))
g=real(ifft2(G));
figure
imshow(uint8(g))
思考:
使用二阶巴特沃兹滤波器对图象cameraman.tif进行低通滤波。
H=1./(1+(D./d0).^(2*n));d0取15、30、80,n=2表示二阶)
closeall
d0=30;
n=2;
f=imread('cameraman.tif');
PQ=size(f);
[UV]=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取15、30、80,n=2表示二阶)%高斯低通
mesh(U,V,H)
figure
subplot(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.我们可以参照理想低通滤波器的思路,得到
>>[UV]=dftuv(51,51);
D=sqrt(U.^2+V.^2);
H=double(D>=115);
Mesh(U,V,H)
b.对图象cameraman.tif进行高通滤波
closeall
x=imread('cameraman.tif');
[UV]=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));
figure
imshow(uint8(g))
w=histeq(g);
figure
imshow(w)
高通滤波器会衰减傅里叶变换中的低频分量而不会扰乱高频信息。
D0越高,图像边缘越清晰,失真越小。
思考:
使用高斯高通滤波器对图象cameraman.tif进行高通滤波。
H1=1-exp(-(U.^2+V.^2)./(2*(D0^2)));
closeall
x=imread('cameraman.tif');
[UV]=dftuv(size(x,1),size(x,2));
D0=15
H=1-exp(-(U.^2+V.^2)./(2*(D0^2)));
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*(D0^2)));
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*(D0^2)));
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)
用高斯高通滤波器对图像进行滤波后,相对于前面滤波器,图像会更加平滑。
四、实验心得与体会
通过这次实验,我学会并了解了许多有关数字图像处理方面的知识。
以前只是看着课本上的内容学习,对很多知识只是生分的了解,但是不懂得如何应用,通过做实验,自己动手,对图像做一系列的处理和变换,也明白了图像滤波器对图像的影响作用,体会到了各种参数对数字图像的影响。
巩固了课上的知识。
通过此次实验也发现了自己的而许多问题,在对于MATLAB软件的应用上面我还有许多不足,尤其是对如何编写M文件方面很难掌握,仍有待加强。
五、实验报要求
1、按照实验步骤做实验;
2、记录实验结果,并作出总结;
3、按照实验报告格式写出报告。
附:
有关dftuv()的说明
clearall
closeall
x=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(u>xm/2);%找出U中满足条件的数据的位置
u(idx)=u(idx)-xm;
idy=find(v>xn/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;
[U1,V1]=meshgrid(u1,v1);
D1=sqrt(U1.^2+V1.^2);%由U1,V1构造D1
%---------------------ILPFbydftuv--------------
H=double(D<=50);
F=fft2(x);
G=H.*F;%不需要进行中心平移
g=ifft2(G);
figure%tu1
subplot(121);imshow(real(G));title('D<=50,ILPF,bydftuv')
subplot(122);imshow(uint8(real(g)),[]);title('ifft')
%-------------------ILPFbyu1,v1--------------
H=double(D1<=50);
F=fft2(x);
G=H.*fftshift(F);%需要中心平移
g=ifft2(ifftshift(G));%反中心平移
figure%tu2
subplot(121);imshow(real(G));title('D<=50,ILPF,byu1,v1')
subplot(122);imshow(uint8(real(g)),[]);title('ifft')