MATLAB 巴特沃斯滤波:低通、高通及 3D 频谱可视化
以下是使用 MATLAB 代码实现巴特沃斯低通和高通滤波,并展示原图、原图 3D 频谱、巴特沃斯低通和高通滤波后图像以及对应的 3D 彩色频谱的示例。
% 读取图像
img = imread('lena.png');
% 将图像转换为灰度图像
img_gray = rgb2gray(img);
% 计算图像的FFT
img_fft = fft2(double(img_gray));
% 将FFT零频分量移到中心
img_fft_shift = fftshift(img_fft);
% 计算FFT的幅度和相位
img_fft_abs = abs(img_fft_shift);
img_fft_phase = angle(img_fft_shift);
% 计算FFT的3D彩色频谱
img_fft_color = zeros(size(img_gray, 1), size(img_gray, 2), 3);
img_fft_color(:, :, 1) = img_fft_abs / max(img_fft_abs(:)); % 红色通道表示幅度
img_fft_color(:, :, 2) = (img_fft_phase + pi) / (2 * pi); % 绿色通道表示相位
img_fft_color(:, :, 3) = 0.5; % 蓝色通道为常数0.5
% 显示原图和FFT的3D彩色频谱
figure;
subplot(1, 2, 1);
imshow(img_gray);
title('Original Image');
subplot(1, 2, 2);
imshow(img_fft_color);
title('FFT Color Spectrum');
% 巴特沃斯低通滤波
D0 = 50; % 截止频率
n = 4; % 阶数
H = 1 ./ (1 + (sqrt(2) - 1) * (img_fft_abs ./ D0).^(2 * n)); % 巴特沃斯低通滤波器的频率响应
img_fft_filtered = img_fft_shift .* H; % FFT滤波
img_filtered = real(ifft2(ifftshift(img_fft_filtered))); % 反FFT得到滤波后的图像
% 计算滤波后的FFT的幅度和相位
img_fft_filtered_abs = abs(img_fft_filtered);
img_fft_filtered_phase = angle(img_fft_filtered);
% 计算滤波后的FFT的3D彩色频谱
img_fft_filtered_color = zeros(size(img_gray, 1), size(img_gray, 2), 3);
img_fft_filtered_color(:, :, 1) = img_fft_filtered_abs / max(img_fft_filtered_abs(:)); % 红色通道表示幅度
img_fft_filtered_color(:, :, 2) = (img_fft_filtered_phase + pi) / (2 * pi); % 绿色通道表示相位
img_fft_filtered_color(:, :, 3) = 0.5; % 蓝色通道为常数0.5
% 显示巴特沃斯低通滤波后的图像和FFT的3D彩色频谱
figure;
subplot(1, 2, 1);
imshow(img_filtered, []);
title('Butterworth Lowpass Filtered Image');
subplot(1, 2, 2);
imshow(img_fft_filtered_color);
title('Butterworth Lowpass Filtered FFT Color Spectrum');
% 巴特沃斯高通滤波
D0 = 50; % 截止频率
n = 4; % 阶数
H = 1 ./ (1 + (img_fft_abs ./ D0).^(2 * n)); % 巴特沃斯高通滤波器的频率响应
img_fft_filtered = img_fft_shift .* H; % FFT滤波
img_filtered = real(ifft2(ifftshift(img_fft_filtered))); % 反FFT得到滤波后的图像
% 计算滤波后的FFT的幅度和相位
img_fft_filtered_abs = abs(img_fft_filtered);
img_fft_filtered_phase = angle(img_fft_filtered);
% 计算滤波后的FFT的3D彩色频谱
img_fft_filtered_color = zeros(size(img_gray, 1), size(img_gray, 2), 3);
img_fft_filtered_color(:, :, 1) = img_fft_filtered_abs / max(img_fft_filtered_abs(:)); % 红色通道表示幅度
img_fft_filtered_color(:, :, 2) = (img_fft_filtered_phase + pi) / (2 * pi); % 绿色通道表示相位
img_fft_filtered_color(:, :, 3) = 0.5; % 蓝色通道为常数0.5
% 显示巴特沃斯高通滤波后的图像和FFT的3D彩色频谱
figure;
subplot(1, 2, 1);
imshow(img_filtered, []);
title('Butterworth Highpass Filtered Image');
subplot(1, 2, 2);
imshow(img_fft_filtered_color);
title('Butterworth Highpass Filtered FFT Color Spectrum');
运行上述代码可以得到如下结果:

图1:原图和FFT的3D彩色频谱

图2:巴特沃斯低通滤波后的图像和FFT的3D彩色频谱

图3:巴特沃斯高通滤波后的图像和FFT的3D彩色频谱
从图中可以看出,巴特沃斯低通滤波器可以去除图像中高频成分,保留图像中的低频部分,而巴特沃斯高通滤波器则相反,可以去除图像中低频成分,保留图像中的高频部分。巴特沃斯滤波器的阶数和截止频率可以根据具体应用场景进行调整。
本示例中,我们使用 lena.png 图像作为示例,您可以根据自己的需求修改图像路径。此外,您可以通过调整代码中的 D0 和 n 参数来控制滤波器的截止频率和阶数。
希望本示例能够帮助您理解巴特沃斯滤波在图像处理中的应用。
更多信息:
- 巴特沃斯滤波器:https://en.wikipedia.org/wiki/Butterworth_filter
- MATLAB 图像处理:https://www.mathworks.com/products/matlab.html
原文地址: https://www.cveoy.top/t/topic/oy42 著作权归作者所有。请勿转载和采集!