本关任务:编写matlab程序实现:分别构造截止距离为d=15,80,300的理想低通、高斯低通、巴特沃斯低通滤波器,分别实现对图像asquares.tif进行低通滤波处理,并将滤波处理结果边同源图像以3×4的子图进行显示。
我可以解决该问题。
下面是用Matlab解决题目中提出问题的步骤和代码: 步骤1:读入图片并显示
img = imread('asquares.tif');
figure(1); imshow(img); title('Original Image');
步骤2:构造理想低通滤波器
% 设置截止频率
d1 = 15;
d2 = 80;
d3 = 300;
% 图像傅里叶变换
img_fft = fft2(img);
% 构造理想低通滤波器
h1 = zeros(size(img));
h2 = zeros(size(img));
h3 = zeros(size(img));
[x, y] = meshgrid(1:size(img,2), 1:size(img,1));
dist = sqrt((x-round(size(img,2)/2)).^2 + (y-round(size(img,1)/2)).^2);
h1(dist<=d1) = 1;
h2(dist<=d2) = 1;
h3(dist<=d3) = 1;
% 应用低通滤波器
img_filter1 = real(ifft2(img_fft.*h1));
img_filter2 = real(ifft2(img_fft.*h2));
img_filter3 = real(ifft2(img_fft.*h3));
% 显示
figure(2);
subplot(3, 4, 1); imshow(img); title('Original Image');
subplot(3, 4, 2); imshow(h1); title(['Ideal Lowpass, D=' num2str(d1)]);
subplot(3, 4, 3); imshow(img_filter1); title(['Filtered Image, D=' num2str(d1)]);
subplot(3, 4, 6); imshow(h2); title(['Ideal Lowpass, D=' num2str(d2)]);
subplot(3, 4, 7); imshow(img_filter2); title(['Filtered Image, D=' num2str(d2)]);
subplot(3, 4, 10); imshow(h3); title(['Ideal Lowpass, D=' num2str(d3)]);
subplot(3, 4, 11); imshow(img_filter3); title(['Filtered Image, D=' num2str(d3)]);
步骤3:构造高斯低通滤波器
% 构造高斯低通滤波器
h1 = fspecial('gaussian', size(img), d1);
h2 = fspecial('gaussian', size(img), d2);
h3 = fspecial('gaussian', size(img), d3);
% 应用低通滤波器
img_filter1 = imfilter(img, h1);
img_filter2 = imfilter(img, h2);
img_filter3 = imfilter(img, h3);
% 显示
subplot(3, 4, 4); imshow(h1); title(['Gaussian Lowpass, D=' num2str(d1)]);
subplot(3, 4, 5); imshow(img_filter1); title(['Filtered Image, D=' num2str(d1)]);
subplot(3, 4, 8); imshow(h2); title(['Gaussian Lowpass, D=' num2str(d2)]);
subplot(3, 4, 9); imshow(img_filter2); title(['Filtered Image, D=' num2str(d2)]);
subplot(3, 4, 12); imshow(h3); title(['Gaussian Lowpass, D=' num2str(d3)]);
subplot(3, 4, 12); imshow(img_filter3); title(['Filtered Image, D=' num2str(d3)]);
步骤4:构造巴特沃斯低通滤波器
% 构造巴特沃斯低通滤波器
n = 2;
[b1, a1] = butter(n, d1/(size(img,1)/2));
[b2, a2] = butter(n, d2/(size(img,1)/2));
[b3, a3] = butter(n, d3/(size(img,1)/2));
% 应用滤波器
img_filter1 = imfilter(img, b1, a1);
img_filter2 = imfilter(img, b2, a2);
img_filter3 = imfilter(img, b3, a3);
% 显示
figure(3);
subplot(3, 4, 1); imshow(img); title('Original Image');
subplot(3, 4, 2); freqz(b1, a1); title(['Butterworth Lowpass, D=' num2str(d1)]);
subplot(3, 4, 3); imshow(img_filter1); title(['Filtered Image, D=' num2str(d1)]);
subplot(3, 4, 6); freqz(b2, a2); title(['Butterworth Lowpass, D=' num2str(d2)]);
subplot(3, 4, 7); imshow(img_filter2); title(['Filtered Image, D=' num2str(d2)]);
subplot(3, 4, 10); freqz(b3, a3); title(['Butterworth Lowpass, D=' num2str(d3)]);
subplot(3, 4, 11); imshow(img_filter3); title(['Filtered Image, D=' num2str(d3)]);
最后,将滤波后的结果添加到同源图像的3x4子图中并显示 ``` % 添加到同源图像的3x4子图中 figure(4); subplot(3,4,1); imshow(img); title('Original Image'); subplot(3,4,2); imshow(img_filter1);title(['Ideal Lowpass, D=' num2str(d1)]); subplot(3,4,3); imshow(img_filter2);title(['Ideal Lowpass, D