各类滤波器的MATLAB程序一、理想低通滤波器IA=imread('lena.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');Hd=ones(size(IA));r=sqrt(f1.^2+f2.^2);Hd(r0.2)=0;Y=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=ifft2(Ya);figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');二、理想高通滤波器IA=imread('lena.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');Hd=ones(size(IA));r=sqrt(f1.^2+f2.^2);Hd(r0.2)=0;Y=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');三、Butterworth低通滤波器IA=imread('lena.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');D=0.3;r=f1.^2+f2.^2;n=4;fori=1:size(IA,1)forj=1:size(IA,2)t=r(i,j)/(D*D);Hd(i,j)=1/(t^n+1);endendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');四、Butterworth高通滤波器IA=imread('lena.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');D=0.3;r=f1.^2+f2.^2;n=4;fori=1:size(IA,1)forj=1:size(IA,2)t=(D*D)/r(i,j);Hd(i,j)=1/(t^n+1);endendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');五、高斯低通滤波器IA=imread('lena.bmp');IB=imread('babarra.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');D=100/size(IA,1);r=f1.^2+f2.^2;Hd=ones(size(IA));fori=1:size(IA,1)forj=1:size(IA,2)t=r(i,j)/(D*D);Hd(i,j)=exp(-t);endendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');六、高斯高通滤波器IA=imread('lena.bmp');IB=imread('babarra.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');%D=100/size(IA,1);D=0.3;r=f1.^2+f2.^2;fori=1:size(IA,1)forj=1:size(IA,2)t=r(i,j)/(D*D);Hd(i,j)=1-exp(-t);endendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');七、梯形低通滤波器IA=imread('lena.bmp');IB=imread('babarra.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');%D=100/size(IA,1);D0=0.1;D1=0.4;r=sqrt(f1.^2+f2.^2);Hd=zeros(size(IA));Hd(rD0)=1;fori=1:size(IA,1)forj=1:size(IA,2)ifr(i,j)=D0&r(i,j)=D1Hd(i,j)=(D1-r(i,j))/(D1-D0);endendendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');八、梯形高通滤波器IA=imread('lena.bmp');IB=imread('babarra.bmp');[f1,f2]=freqspace(size(IA),'meshgrid');%D=100/size(IA,1);D0=0.1;D1=0.4;r=sqrt(f1.^2+f2.^2);Hd=ones(size(IA));Hd(rD1)=0;fori=1:size(IA,1)forj=1:size(IA,2)ifr(i,j)=D0&r(i,j)=D1Hd(i,j)=(D0-r(i,j))/(D0-D1);endendendY=fft2(double(IA));Y=fftshift(Y);Ya=Y.*Hd;Ya=ifftshift(Ya);Ia=real(ifft2(Ya));figuresubplot(2,2,1),imshow(uint8(IA));subplot(2,2,2),imshow(uint8(Ia));figuresurf(Hd,'Facecolor','interp','Edgecolor','none','Facelighting','phong');九、用其他方法编写的理想低通、理想高通、Butterworth低通、同态滤波程序1、理想低通i1=imread('lena.bmp');i2=imnoise(i1,'salt&pepper',0.1);f=double(i2);k=fft2(f);g=fftshift(k);[N1,N2]=size(g);d0=50;u0=floor(N1/2)+1;v0=floor(N2/2)+1;fori=1:N1forj=1:N2d=sqrt((i-u0)^2+(j-v0)^2);ifd=d0h=1;elseh=0;endy(i,j)=g(i,j)*h;endendy=ifftshift(y);E1=ifft2(y);E2=real(E1);figuresubplot(2,2,1),imshow(uint8(i1));subplot(2,2,2),imshow(uint8(i2));subplot(2,2,3),imshow(uint8(E2));2、理想高通i1=imread('lena.bmp');i2=imnoise(i1,'salt&pepper',0.1);f=double(i2);k=fft2(f);g=fftshift(k);[N1,N2]=size(g);n=2;d0=10;u0=floor(N1/2)+1;v0=floor(N2/2)+1;fori=1:N1forj=1:N2d=sqrt((i-u0)^2+(j-v0)^2);ifd=d0h=0;elseh=1;endy(i,j)=g(i,j)*h;endendy=ifftshift(y);E1=ifft2(y);E2=real(E1);figuresubplot(2,2,1),imshow(uint8(i1));subplot(2,2,2),imshow(uint8(i2));subplot(2,2,3),imshow(uint8(E2));3、Butterworth低通i1=imread('lena.bmp');i2=imnoise(i1,'salt&pepper',0.1);f=double(i2);k=fft2(f);g=fftshift(k);[N1,N2]=size(g);n=2;d0=50;u0=floor(N1/2)+1;v0=floor(N2/2)+1;fori=1:N1forj=1:N2d=sqrt((i-u0)^2+(j-v0)^2);h=1/(1+(d/d0)^(2*n));y(i,j)=g(i,j)*h;endendy=ifftshift(y);E1=ifft2(y);E2=real(E1);figuresubplot(2,2,1),imshow(uint8(i1));subplot(2,2,2),imshow(uint8(i2));subplot(2,2,3),imshow(uint8(E2));4、同态滤波I=rgb2gray(imread('fabric00.bmp'));[M,N]=size(I);T=double(I);L=log(T);F=fft2(L);A=2;B=0.3;fori=1:Mforj=1:ND(i,j)=((i-M/2)^2+(j-N/2)^2);endendc=1.1;%锐化参数D0=max(M,N);H=(A-B)*(1-exp(c*(-D/(D0^2))))+B;F=F.*H;F=ifft2(F);Y=exp(F);figuresubplot(1,2,1),imshow(I);subplot(1,2,2),imshow(uint8(real(Y)));十、Gabor滤波器