实验5 图像频域增强一、实验目的通过本实验使学生掌握使用MATLAB的二维傅里叶变换进行频域增强的方法。
二、实验原理本实验是基于数字图像处理课程中的图像频域增强理论来设计的。
本实验的准备知识:第四章频域图像增强中的一维傅里叶变换和二维傅里叶变换,频域图像增强的步骤,频域滤波器。
根据教材285页到320页的内容,开展本实验。
可能用到的函数:1、延拓函数 padarray例:A=[1,2;3,4];B=padarray(A,[2,3],’post’);则结果为B =1 2 0 0 03 4 0 0 00 0 0 0 00 0 0 0 0使用该函数实现图像的0延拓。
Padarray还有其它用法,请用help查询。
2、低通滤波器生成函数首先编写dftuv函数,如下function [U,V]=dftuv(M,N)%DFTUV Computes meshgrid frequency matrices.% [U,V]=DFTUV(M,N] computes meshgrid frequency matrices U and V. Uand V are useful for computing frequency-domain filter functions thatcan be used with DFTFILT. U and V are both M-by-N.% Set up range of variables.u=0:(M-1);v=0:(N-1);% Compute the indices for use in meshgrid.idx=find(u>M/2);u(idx)=u(idx)-M;idy=find(v>N/2);v(idy)=v(idy)-N;%Compute the meshgrid arrays.[V,U]=meshgrid(v,u);然后编写低通滤波器函数function [H,D]=lpfilter(type,M,N,D0,n)% LPFILTER computers frequency domain lowpass filters.% H=lpfilter(TYPE,M,N,D0,n) creates the transfer function of a lowpassfilter, H, of the specified TYPE and size(M-by-N). To view the filter as an image or mesh plot, it should be centered using H=fftshift(H).% valid values for TYPE, D0, and n are:% 'ideal' Ideal lowpass filter with cutoff frequency D0. n need not be supplied. D0 must be positive.% 'btw' Butterworth lowpass filter of ordern, and cutoff D0. The default value for n is 1. D0 must be positive.% 'gaussian' Gaussian lowpass filter with cutoff (standard deviation)D0.n need not be supplied. D0 must be positive.%Use function dftuv to set up the meshgrid arrays needed for computing the required distances.[U,V]=dftuv(M,N); %D=sqrt(U.^2+V.^2); % Compute the distances D(U,V)% Begin filter computations.switch typecase 'ideal'H=double(D<=D0);case 'btw'if nargin==4n=1;endH=1./(1+(D./D0).^(2*n));case 'gaussian'H=exp(-(D.^2)./(2*(D0^2)));otherwiseerror('Unknown filter type')end通过调用函数lpfilter可生成相应的滤波器掩膜矩阵。
参考该函数可相应的生成高通滤波器函数。
3、频域滤波F=fft2(f,size(H,1),size(H,2)); % 对延拓的 f 计算 FFT。
注意,这里隐含着对 f 的延拓。
G=real(ifft2(H.*F)); % 滤波Gf=G(1:size(f,1),1:size(f,2)); %裁剪后的图像三、实验内容(一)图像频域增强的步骤参考教材286页的Figure 4.36,重复该图像中的步骤,并将相应的结果显示出来。
(二)频域低通滤波产生实验四中的白条图像。
设计不同截止频率的理想低通滤波器、Butterworth低通滤波器,对其进行频域增强。
观察频域滤波效果,并解释之。
设计不同截止频率的理想低通滤波器、Butterworth低通滤波器,对含高斯噪声的lena 图像进行频域增强。
观察频域滤波效果,并解释之。
(三)频域高通滤波设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对上述白条图像进行频域增强。
观察频域滤波效果,并解释之。
设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对含高斯噪声的lena 图像进行频域增强。
观察频域滤波效果,并解释之。
四、实验步骤(二)频域低通滤波理想低通滤波器1. D0=5程序:A=zeros(64,64);A(32-20:32+20,32-8:32+8)=255;subplot(1,2,1)imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1*exp(-1/2*(d^2/d0^2)); %GLPF滤波函数s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);运行结果:2.D0=50程序:A=zeros(64,64);A(32-20:32+20,32-8:32+8)=255;subplot(1,2,1)imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=50;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1*exp(-1/2*(d^2/d0^2)); %GLPF滤波函数s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);运行结果:Butterworth 低通滤波器1.程序A=zeros(64,64);A(32-20:32+20,32-8:32+8)=255;subplot(1,2,1)imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1/(1+0.414*(d/d0)^(2*n));s(i,j)=h*s(i,j); %GLPF滤波后的频域表示 endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);运行结果:含高斯噪声的lena图像进行频域增强clear all;A=imread('D:\pic\lena.bmp')subplot(2,3,1),imshow(A,[]);title('原图') %把图像显示出来B=imnoise(A,'gauss',0.02)subplot(2,3,3),imshow(B,[]); %添加高斯噪声后的图像title('添加高斯噪声后的图像')f=double(B); %图像存储类型转换g=fft2(f); %傅立叶变换g=fftshift(g); %转换数据矩阵[N1,N2]=size(g); %测量图像尺寸参数n=2;d0=50;n1=fix(N1/2);n2=fix(N2/2);for i=1:N1for j=1:N2d=sqrt((i-n1)^+(j-n2)^2)c=double(d<=d0); %result(i,j)=c*g(i,j);endendresul=ifftshift(result); %傅立叶逆变换X2=ifft2(result);X3=uint8(real(X2));subplot(2,3,5)imshow(X3) %显示频域增强后的图像D0=20,butterworth 滤波器D0=50,低通滤波器D0=50,butterworth 滤波器title('D0=50,低通滤波器')运行结果:原图添加高斯噪声后的图像D0=20,低通滤波器(三)频域高通滤波理想高通滤波器 A=zeros(64,64);A(32-20:32+20,32-8:32+8)=255;subplot(1,2,1);imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2);%对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1-(1*exp(-1/2*(d^2/d0^2)));s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);Butterworth 高通滤波器A=zeros(64,64);A(32-20:32+20,32-8:32+8)=255;subplot(1,2,1);imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1-(1/(1+0.414*(d/d0)^(2*n)));s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);对含高斯噪声的lena图像理想高通滤波器A1= imread('D:\pic\lena.bmp')A=imnoise(A1, 'gauss', 0.02);subplot(1,2,1);imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1-(1*exp(-1/2*(d^2/d0^2)));s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);运行结果:Butterworth 高通滤波器A1= imread('D:\pic\lena.bmp')A=imnoise(A1, 'gauss', 0.02);subplot(1,2,1)imshow(A);s=fftshift(fft2(A));[M,N]=size(s);n1=floor(M/2); %对M/2进行取整n2=floor(N/2);d0=5;for i=1:Mfor j=1:Nd=sqrt((i-n1)^2+(j-n2)^2); %点(i,j)到傅立叶变换中心的距离h=1-(1/(1+0.414*(d/d0)^(2*n)));s(i,j)=h*s(i,j); %GLPF滤波后的频域表示endends=ifftshift(s);s=uint8(real(ifft2(s)));subplot(1,2,2); %创建图形图像对象imshow(s);五、实验心得通过MATLAB软件实现了原理程序及仿真图像。