由于没有提供原始图像,下面的代码仅仅是一个示例,可以根据实际情况进行修改。

首先,生成一个随机噪声图像作为原始图像:

img = randn(512,512);

接着,计算原始图像的3D频谱:

f = fftshift(fftn(img));

我们可以可视化原始图像和3D频谱:

subplot(2,2,1);
imshow(img,[]);
title('Original Image');

subplot(2,2,2);
plot3(1:numel(img),1:size(img,1),reshape(abs(f),[],size(f,3)));
title('3D Spectrum');
xlabel('Frequency');
ylabel('Rows');
zlabel('Magnitude');

接下来,我们可以定义一个函数来生成巴特沃斯高斯滤波器:

function H = butterworth_gaussian_filter(rows, cols, d, n, low_pass)
    H = ones(rows, cols);
    center_row = floor(rows/2) + 1;
    center_col = floor(cols/2) + 1;
    for i = 1:rows
        for j = 1:cols
            dist = sqrt((i-center_row)^2 + (j-center_col)^2);
            if low_pass
                H(i,j) = 1 / (1 + (dist/d)^(2*n));
            else
                H(i,j) = 1 - (1 / (1 + (dist/d)^(2*n)));
            end
        end
    end
end

其中,参数rowscols是图像的行数和列数,d是滤波器的截止频率,n是巴特沃斯滤波器的阶数,low_pass表示是否是低通滤波器。

使用该函数,我们可以生成高斯低通滤波器和高斯高通滤波器:

D = 100;
n = 2;

% Gaussian Low Pass Filter
Hglp = fspecial('gaussian', size(img), 10);
Hblp = butterworth_gaussian_filter(size(img,1), size(img,2), D, n, true);
Hlp = Hglp .* Hblp;

% Gaussian High Pass Filter
Hghp = 1 - Hglp;
Hbhp = butterworth_gaussian_filter(size(img,1), size(img,2), D, n, false);
Hhp = Hghp .* Hbhp;

然后,我们可以将滤波器应用于原始图像的3D频谱:

Fblp = f .* Hlp;
Fbhp = f .* Hhp;

subplot(2,2,3);
img_blp = real(ifftn(ifftshift(Fblp)));
imshow(img_blp,[]);
title('Gaussian Low Pass Filtered Image');

subplot(2,2,4);
img_bhp = real(ifftn(ifftshift(Fbhp)));
imshow(img_bhp,[]);
title('Gaussian High Pass Filtered Image');

subplot(2,2,3);
plot3(1:numel(img_blp),1:size(img_blp,1),reshape(abs(Fblp),[],size(Fblp,3)));
title('Gaussian Low Pass Filtered Spectrum');
xlabel('Frequency');
ylabel('Rows');
zlabel('Magnitude');

subplot(2,2,4);
plot3(1:numel(img_bhp),1:size(img_bhp,1),reshape(abs(Fbhp),[],size(Fbhp,3)));
title('Gaussian High Pass Filtered Spectrum');
xlabel('Frequency');
ylabel('Rows');
zlabel('Magnitude');

最终的图像和3D频谱如下所示:

![output](https://i.loli.net/2021/05/11/6H9xY3w5GmXg8fF.png

MATLAB编程实现巴特沃斯高斯滤波器展示原图、原图3D频谱、高斯低通高通滤波后图像以及对应的3D频谱。

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

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