以下是 MATLAB 代码实现巴特沃斯高斯滤波器:

% 读取原图像
img = imread('lena.jpg');
% 转换为灰度图像
if size(img, 3) == 3
    img = rgb2gray(img);
end

% 原图像频谱
img_fft = fft2(img);
img_fft_shift = fftshift(img_fft);
img_fft_abs = abs(img_fft_shift);

% 设置滤波器参数
D0 = 50; % 截止频率
n = 2; % 阶数
type = 'low'; % 低通滤波器
% type = 'high'; % 高通滤波器

% 构造巴特沃斯滤波器
[M, N] = size(img);
[U, V] = meshgrid(1:N, 1:M);
D = sqrt((U - N/2).^2 + (V - M/2).^2);
if strcmp(type, 'low')
    H = 1 ./ (1 + (D ./ D0).^(2*n));
elseif strcmp(type, 'high')
    H = 1 ./ (1 + (D0 ./ D).^(2*n));
end

% 构造高斯滤波器
sigma = 10; % 标准差
hsize = 2*ceil(3*sigma) + 1; % 滤波器大小
h = fspecial('gaussian', hsize, sigma);

% 滤波
img_filtered = filter2(h, img);
img_filtered_fft = fft2(img_filtered);
img_filtered_fft_shift = fftshift(img_filtered_fft);
img_filtered_fft_abs = abs(img_filtered_fft_shift);

% 滤波后频谱
img_filtered_fft_shift_filtered = img_filtered_fft_shift .* H;
img_filtered_fft_filtered_abs = abs(img_filtered_fft_shift_filtered);

% 显示原图
figure;
subplot(2, 2, 1);
imshow(img);
title('Original Image');

% 显示原图频谱
subplot(2, 2, 2);
mesh(img_fft_abs);
title('Original Image Spectrum');
zlim([0 50000]);

% 显示滤波后图像
subplot(2, 2, 3);
imshow(img_filtered);
if strcmp(type, 'low')
    title('Gaussian Lowpass Filtered Image');
elseif strcmp(type, 'high')
    title('Gaussian Highpass Filtered Image');
end

% 显示滤波后频谱
subplot(2, 2, 4);
mesh(img_filtered_fft_filtered_abs);
if strcmp(type, 'low')
    title('Gaussian Lowpass Filtered Image Spectrum');
elseif strcmp(type, 'high')
    title('Gaussian Highpass Filtered Image Spectrum');
end
zlim([0 50000]);

运行上述代码,可以得到以下结果:

巴特沃斯高斯滤波器结果

在结果中,左上角为原图,右上角为原图频谱,左下角为滤波后图像,右下角为滤波后频谱。可以通过修改滤波器参数和滤波器类型来实现不同的滤波效果。

MATLAB 实现巴特沃斯高斯滤波器:图像处理与频谱分析

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

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