用matlab导入一张图像,为其叠加周期噪声,然后通过频域滤波的方法对其进行去噪,对比滤波前后的效果。
时间: 2023-12-16 18:05:28 浏览: 47
以下是一种可能的实现方式:
```matlab
% 导入图像
img = imread('image.png');
% 叠加周期噪声
noise_freq = 0.05; % 噪声的频率
noise_amp = 50; % 噪声的振幅
[row, col] = size(img);
[X, Y] = meshgrid(1:col, 1:row);
noise = noise_amp * sin(2*pi*noise_freq*X);
noisy_img = img + uint8(noise);
% 显示原始图像和叠加噪声后的图像
figure;
subplot(1,2,1); imshow(img); title('原始图像');
subplot(1,2,2); imshow(noisy_img); title('加噪声后的图像');
% 对叠加噪声后的图像进行频域滤波
fft_img = fft2(double(noisy_img));
fft_img_shift = fftshift(fft_img);
[row, col] = size(fft_img_shift);
[X, Y] = meshgrid(1:col, 1:row);
center_x = floor(col/2) + 1;
center_y = floor(row/2) + 1;
radius = 50; % 滤波器半径
mask = sqrt((X-center_x).^2 + (Y-center_y).^2) < radius;
fft_img_shift_filtered = fft_img_shift .* mask;
fft_img_filtered = ifftshift(fft_img_shift_filtered);
img_filtered = uint8(real(ifft2(fft_img_filtered)));
% 显示滤波前后的图像
figure;
subplot(1,2,1); imshow(noisy_img); title('加噪声后的图像');
subplot(1,2,2); imshow(img_filtered); title('滤波后的图像');
```
这段代码首先导入一张图像,然后叠加了一个频率为0.05的周期噪声,并显示了原始图像和叠加噪声后的图像。接着,使用`fft2`函数将图像转换到频域,并使用`fftshift`函数将零频分量移到频谱中心。然后,定义了一个半径为50的圆形滤波器,将其应用于频域图像,并使用`ifft2`函数将滤波后的频域图像转换回空域。最后,将滤波后的图像显示出来,与叠加噪声后的图像进行对比。