下面是用Matlab实现的Butterworth高通、低通滤波器。

clc;clear all;close all;

I=imread(‘cameraman.tif’);

subplot(3,2,1);imshow(I);

title(‘原始图’);

f=double(I); % 数据类型转换,MATLAB不支持图像的无符号整型的计算

g=fft2(f); % 傅立叶变换

g=fftshift(g); % 转换数据矩阵

subplot(3,2,2);imshow(log(1+abs(g)),[]);

title(‘傅里叶频谱’);

%低通滤波***************************************************************

[M,N]=size(g);

nn=2; % 二阶巴特沃斯(Butterworth)低通滤波器

d0=10;

m=floor(M/2); n=floor(N/2);

for i=1:M

for j=1:N

d=sqrt((i-m)^2+(j-n)^2);

h=1/(1+0.414*(d/d0)^(2*nn)); % 计算低通滤波器传递函数

result(i,j)=h*g(i,j);

end

end

subplot(3,2,3);imshow(log(1+abs(result)),[]);

title(‘butterworth低通滤波’);

result=ifftshift(result);

J2=ifft2(result);

J3=uint8(real(J2));

subplot(3,2,4),imshow(J3); % 显示滤波处理后的图像

title(‘低通滤波图’);

%高通滤波

for i=1:M

for j=1:N

d=sqrt((i-m)^2+(j-n)^2);

h=1/(1+0.414*(d0/d)^(2*nn));% 计算传递函数

result(i,j)=h*g(i,j);

end

end

subplot(3,2,5);imshow(log(1+abs(result)),[]);

title(‘butterworth高通滤波’);

result=ifftshift(result);

J2=ifft2(result);

J3=uint8(real(J2));

subplot(3,2,6),imshow(J3); % 显示滤波处理后的图像

title(‘高通滤波图’);

效果图:

0818b9ca8b590ca3270a3433284dd417.png0818b9ca8b590ca3270a3433284dd417.png