各类滤波器的MATLAB程序

一、理想低通滤波器
IA=imread('lena.bmp');
[f1,f2]=freqspace(size(IA),'meshgrid');
Hd=ones(size(IA));
r=sqrt(f1.2+f2.2);
Hd(r>0.2)=0;
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.Hd;
Ya=ifftshift(Ya);
Ia=ifft2(Ya);
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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(r<0.2)=0;
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.
Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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;
for i=1:size(IA,1)
for j=1:size(IA,2)
t=r(i,j)/(DD);
Hd(i,j)=1/(t^n+1);
end
end
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.
Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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));
for i=1:size(IA,1)
for j=1:size(IA,2)
t=r(i,j)/(DD);
Hd(i,j)=exp(-t);
end
end
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.
Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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;
for i=1:size(IA,1)
for j=1:size(IA,2)
t=r(i,j)/(DD);
Hd(i,j)=1-exp(-t);
end
end
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.
Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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(r for i=1:size(IA,1)
for j=1:size(IA,2)
if r(i,j)>=D0 & r(i,j)<=D1
Hd(i,j)=(D1-r(i,j))/(D1-D0);
end
end
end
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.*Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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(r for i=1:size(IA,1)
for j=1:size(IA,2)
if r(i,j)>=D0 & r(i,j)<=D1
Hd(i,j)=(D0-r(i,j))/(D0-D1);
end
end
end
Y=fft2(double(IA));
Y=fftshift(Y);
Ya=Y.*Hd;
Ya=ifftshift(Ya);
Ia=real(ifft2(Ya));
figure
subplot(2,2,1),imshow(uint8(IA));
subplot(2,2,2),imshow(uint8(Ia));
figure
surf(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;
for i=1:N1
for j=1:N2
d=sqrt((i-u0)2+(j-v0)2);
if d<=d0
h=1;
else
h=0;
end
y(i,j)=g(i,j)*h;
end
end
y=ifftshift(y);
E1=ifft2(y);
E2=real(E1);
figure
subplot(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;
for i=1:N1
for j=1:N2
d=sqrt((i-u0)2+(j-v0)2);
if d<=d0
h=0;
else h=1;
end
y(i,j)=g(i,j)*h;
end
end
y=ifftshift(y);
E1=ifft2(y);
E2=real(E1);
figure
subplot(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;
for i=1:N1
for j=1:N2
d=sqrt((i-u0)2+(j-v0)2);
h=1/(1+(d/d0)^(2n));
y(i,j)=g(i,j)
h;
end
end
y=ifftshift(y);
E1=ifft2(y);
E2=real(E1);
figure
subplot(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;
for i=1:M
for j=1:N
D(i,j)=((i-M/2)2+(j-N/2)2);
end
end
c=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);
figure
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(uint8(real(Y)));

你可能感兴趣的:(各类滤波器的MATLAB程序)