MATLAB 巴特沃斯滤波:低通和高通滤波器实现及图像频谱分析

本文使用 MATLAB 代码实现巴特沃斯低通和高通滤波,并展示滤波前后图像及对应频谱。详细解释滤波器原理,并展示原图、低通/高通滤波后图像以及对应的 3D 频谱。

代码实现

% 读取图像
img = imread('lena.png');
% 转换为灰度图像
img = rgb2gray(img);

% 计算图像的傅里叶变换
img_fft = fft2(double(img));
% 将低频分量移到中心位置
img_fft = fftshift(img_fft);
% 计算频谱
img_fft_abs = abs(img_fft);
img_fft_abs_log = log(img_fft_abs+1);

% 显示原图
figure;
subplot(2,2,1);
imshow(img);
title('Original Image');
% 显示原图频谱
subplot(2,2,2);
mesh(img_fft_abs_log);
title('Original Image Spectrum');

% 定义巴特沃斯滤波器参数
n = 3; % 阶数
D0_low = 50; % 低通截止频率
D0_high = 50; % 高通截止频率

% 计算巴特沃斯低通滤波器
[m,n] = size(img);
u = 0:(m-1);
v = 0:(n-1);
idx = find(u>m/2);
u(idx) = u(idx)-m;
idy = find(v>n/2);
v(idy) = v(idy)-n;
[V,U] = meshgrid(v,u);
D = sqrt(U.^2+V.^2);
H = 1./(1+(D./D0_low).^(2*n));

% 计算巴特沃斯高通滤波器
H_high = 1-H;

% 进行巴特沃斯低通滤波
img_filtered_low = ifft2(ifftshift(img_fft.*H));
img_filtered_low = real(img_filtered_low);

% 计算巴特沃斯低通滤波后的频谱
img_filtered_low_fft = fft2(double(img_filtered_low));
img_filtered_low_fft = fftshift(img_filtered_low_fft);
img_filtered_low_fft_abs = abs(img_filtered_low_fft);
img_filtered_low_fft_abs_log = log(img_filtered_low_fft_abs+1);

% 显示巴特沃斯低通滤波后的图像
subplot(2,2,3);
imshow(uint8(img_filtered_low));
title('Butterworth Lowpass Filtered Image');
% 显示巴特沃斯低通滤波后的频谱
subplot(2,2,4);
mesh(img_filtered_low_fft_abs_log);
title('Butterworth Lowpass Filtered Image Spectrum');

% 进行巴特沃斯高通滤波
img_filtered_high = ifft2(ifftshift(img_fft.*H_high));
img_filtered_high = real(img_filtered_high);

% 计算巴特沃斯高通滤波后的频谱
img_filtered_high_fft = fft2(double(img_filtered_high));
img_filtered_high_fft = fftshift(img_filtered_high_fft);
img_filtered_high_fft_abs = abs(img_filtered_high_fft);
img_filtered_high_fft_abs_log = log(img_filtered_high_fft_abs+1);

% 显示巴特沃斯高通滤波后的图像
figure;
subplot(2,2,1);
imshow(uint8(img_filtered_high));
title('Butterworth Highpass Filtered Image');
% 显示巴特沃斯高通滤波后的频谱
subplot(2,2,2);
mesh(img_filtered_high_fft_abs_log);
title('Butterworth Highpass Filtered Image Spectrum');

% 对比原图和巴特沃斯低通、高通滤波后的图像
subplot(2,2,3);
imshowpair(img, img_filtered_low, 'montage');
title('Original vs Butterworth Lowpass Filtered Image');
subplot(2,2,4);
imshowpair(img, img_filtered_high, 'montage');
title('Original vs Butterworth Highpass Filtered Image');

滤波结果展示

运行上述代码,可以得到如下图像:

  • 原图: Original Image

  • 原图频谱: Original Image Spectrum

  • 巴特沃斯低通滤波后的图像: Butterworth Lowpass Filtered Image

  • 巴特沃斯低通滤波后的频谱: Butterworth Lowpass Filtered Image Spectrum

  • 巴特沃斯高通滤波后的图像: Butterworth Highpass Filtered Image

  • 巴特沃斯高通滤波后的频谱: Butterworth Highpass Filtered Image Spectrum

  • 原图和巴特沃斯低通、高通滤波后的图像对比: Original vs Butterworth Lowpass Filtered Image Original vs Butterworth Highpass Filtered Image

结论

从上述结果可以看出,巴特沃斯低通滤波器可以实现图像的平滑处理,去除高频噪声,而巴特沃斯高通滤波器则可以实现图像的锐化处理,增强图像的边缘信息。

MATLAB 巴特沃斯滤波:低通和高通滤波器实现及图像频谱分析

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

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