MATLAB中图片的傅里叶频谱布特沃斯低通滤波的实现
时间: 2023-09-12 18:03:39 浏览: 57
Matlab实现图像低通滤波
4星 · 用户满意度95%
以下是MATLAB中图片的傅里叶频谱布特沃斯低通滤波的实现步骤:
1. 读取需要处理的图片,转为灰度图像。
```matlab
img = imread('image.jpg');
gray_img = rgb2gray(img);
```
2. 对灰度图像进行二维傅里叶变换,得到频谱图像。
```matlab
fft_img = fft2(double(gray_img));
freq_img = fftshift(fft_img);
freq_img_abs = abs(freq_img);
freq_img_log = log(1 + freq_img_abs);
```
3. 构造布特沃斯低通滤波器,设置截止频率和n值。
```matlab
[M, N] = size(freq_img);
D0 = 50; % 截止频率
n = 4; % n值
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).^(2*n));
```
4. 将频谱图像和低通滤波器进行卷积,得到滤波后的频谱图像。
```matlab
freq_img_filtered = H .* freq_img;
freq_img_filtered_abs = abs(freq_img_filtered);
freq_img_filtered_log = log(1 + freq_img_filtered_abs);
```
5. 对滤波后的频谱图像进行逆傅里叶变换,得到滤波后的图像。
```matlab
ifft_img_filtered = ifft2(ifftshift(freq_img_filtered));
ifft_img_filtered = uint8(real(ifft_img_filtered));
```
6. 显示原始图像、频谱图像、低通滤波器、滤波后的频谱图像和滤波后的图像。
```matlab
subplot(2, 3, 1);
imshow(gray_img);
title('Original Image');
subplot(2, 3, 2);
imshow(freq_img_log, []);
title('Frequency Spectrum');
subplot(2, 3, 3);
imshow(H, []);
title('Butterworth Lowpass Filter');
subplot(2, 3, 4);
imshow(freq_img_filtered_log, []);
title('Filtered Frequency Spectrum');
subplot(2, 3, 5);
imshow(ifft_img_filtered);
title('Filtered Image');
```
阅读全文