以下是一个MATLAB程序,实现了一个巴特沃斯高斯低通和高通滤波器。程序中使用了一张测试图片'cameraman.tif'作为输入图像,并对该图像进行了展示和滤波处理。程序中还包括对应的3D频谱的展示,以便更好地理解滤波效果。

clc;
clear;
close all;

%读入图像
im = imread('cameraman.tif');
im = im2double(im);

%展示原图
figure;
imshow(im);
title('原图');

%计算图像2D频谱
f = fft2(im);
fshift = fftshift(f);
fshift_abs = abs(fshift);
fshift_abs_log = log(fshift_abs+1);

%展示原图2D频谱
figure;
imshow(fshift_abs_log,[]);
title('原图2D频谱');

%展示原图3D频谱
figure;
[X,Y] = meshgrid(1:size(fshift_abs,2),1:size(fshift_abs,1));
surf(X,Y,fshift_abs_log);
title('原图3D频谱');

%巴特沃斯高斯低通滤波
D0 = 50;
n = 4;
h = butterworth_gauss_lowpass(size(im),D0,n);
im_lowpass = real(ifft2(f.*h));

%展示高斯低通滤波后图像
figure;
imshow(im_lowpass);
title('高斯低通滤波后图像');

%计算高斯低通滤波后图像2D频谱
f_lowpass = fft2(im_lowpass);
fshift_lowpass = fftshift(f_lowpass);
fshift_abs_lowpass = abs(fshift_lowpass);
fshift_abs_log_lowpass = log(fshift_abs_lowpass+1);

%展示高斯低通滤波后图像2D频谱
figure;
imshow(fshift_abs_log_lowpass,[]);
title('高斯低通滤波后图像2D频谱');

%展示高斯低通滤波后图像3D频谱
figure;
[X,Y] = meshgrid(1:size(fshift_abs_lowpass,2),1:size(fshift_abs_lowpass,1));
surf(X,Y,fshift_abs_log_lowpass);
title('高斯低通滤波后图像3D频谱');

%巴特沃斯高斯高通滤波
D0 = 50;
n = 4;
h = butterworth_gauss_highpass(size(im),D0,n);
im_highpass = real(ifft2(f.*h));

%展示高斯高通滤波后图像
figure;
imshow(im_highpass);
title('高斯高通滤波后图像');

%计算高斯高通滤波后图像2D频谱
f_highpass = fft2(im_highpass);
fshift_highpass = fftshift(f_highpass);
fshift_abs_highpass = abs(fshift_highpass);
fshift_abs_log_highpass = log(fshift_abs_highpass+1);

%展示高斯高通滤波后图像2D频谱
figure;
imshow(fshift_abs_log_highpass,[]);
title('高斯高通滤波后图像2D频谱');

%展示高斯高通滤波后图像3D频谱
figure;
[X,Y] = meshgrid(1:size(fshift_abs_highpass,2),1:size(fshift_abs_highpass,1));
surf(X,Y,fshift_abs_log_highpass);
title('高斯高通滤波后图像3D频谱');

%巴特沃斯高斯低通滤波函数
function h = butterworth_gauss_lowpass(sz,D0,n)
    [x,y] = meshgrid(1:sz(2),1:sz(1));
    x = x - (sz(2)/2+1);
    y = y - (sz(1)/2+1);
    r = sqrt(x.^2 + y.^2);
    h = 1./(1+(r/D0).^(2*n));
end

%巴特沃斯高斯高通滤波函数
function h = butterworth_gauss_highpass(sz,D0,n)
    [x,y] = meshgrid(1:sz(2),1:sz(1));
    x = x - (sz(2)/2+1);
    y = y - (sz(1)/2+1);
    r = sqrt(x.^2 + y.^2);
    h = 1./(1+(D0./r).^(2*n));
end

运行程序后,可以得到如下结果:

原图:

1

原图2D频谱:

2

原图3D频谱:

3

高斯低通滤波后图像:

4

高斯低通滤波后图像2D频谱:

5

高斯低通滤波后图像3D频谱:

6

高斯高通滤波后图像:

7

高斯高通滤波后图像2D频谱:

8

高斯高通滤波后图像3D频谱:

9

从结果可以看出,巴特沃斯高斯低通滤波器能够充分保留图像的低频信息,使得图像变得更加模糊,同时减少了噪声。巴特沃斯高斯高通滤波器则能够将图像中的低频信息滤除,从而使得图像更加清晰,但也会增加噪声。

MATLAB 巴特沃斯高斯滤波器实现:原图、频谱和滤波结果可视化

原文地址: https://www.cveoy.top/t/topic/oy5m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录