Problem
In MATLAB, a small-sized Gaussian low-pass filter is applied to a larger image in the frequency domain by performing the following operations: First, the image and the filter are padded to the same size, and then the filtering is carried out in the frequency domain. However, the result of the filtering is erroneous, resulting in an image that appears completely white.Would like to know how to modify the code or how to process in order to obtain the correct filtering results.
%processing with a Gaussian LP filter in the frequency domain.
for k = 3:2:33 %filter size ranges from 3 to 33.
%%The image and the Gaussian filter are padded with zeros to the same size, and then apply fft
pad_img = padarray(origin,[k-1 k-1],0,'post');%image padding and fft
F_img = fft2(double(pad_img));
F_img = fftshift(F_img);
spain_gaus = fspecial('gaussian',[k k],sigma);%produce gaussian LP filter in spatial domain and then apply fft
pad_spain_gaus = padarray(spain_gaus,[1079 1079],0,'both');
pad_spain_gaus = padarray(pad_spain_gaus,[1 1],0,'pre');
fft_gaus = fft2(double(pad_spain_gaus));
shift_fft_gaus = fftshift(fft_gaus);
figure;imshow(1+log10(abs(shift_fft_gaus)));%display the filter spectrum.
%%perform filtering on the image and display the result.
fft_start_time = tic;
img_filtered = F_img.*shift_fft_gaus;
img_filtered = ifftshift(img_filtered);
fft_result = real(ifft2(img_filtered));
finalResult = fft_result(1:rows, 1:cols);
fft_end_time = toc(fft_start_time);
fprintf('运行时间: %f s\n',fft_end_time);
str = [num2str(k),'×',num2str(k),'滤波结果'];
figure;imshow(finalResult);
title(str);
end
`
filter spectrum during the process:
2.matlab codes ` %reading image img = imread('C:\Users\86155\Desktop\yinghua.jpg'); origin = rgb2gray(img); [rows, cols] = size(origin); sigma = 5; %standard deviation of filter
%processing with a Gaussian LP filter in the frequency domain. for k = 3:2:33 %filter size ranges from 3 to 33. %%The image and the Gaussian filter are padded with zeros to the same size, and then apply fft pad_img = padarray(origin,[k-1 k-1],0,'post');%image padding and fft F_img = fft2(double(pad_img)); F_img = fftshift(F_img); spain_gaus = fspecial('gaussian',[k k],sigma);%produce gaussian LP filter in spatial domain and then apply fft pad_spain_gaus = padarray(spain_gaus,[1079 1079],0,'both'); pad_spain_gaus = padarray(pad_spain_gaus,[1 1],0,'pre'); fft_gaus = fft2(double(pad_spain_gaus)); shift_fft_gaus = fftshift(fft_gaus); figure;imshow(1+log10(abs(shift_fft_gaus)));%display the filter spectrum.
%%perform filtering on the image and display the result. fft_start_time = tic; img_filtered = F_img.*shift_fft_gaus; img_filtered = ifftshift(img_filtered); fft_result = real(ifft2(img_filtered)); finalResult = fft_result(1:rows, 1:cols); fft_end_time = toc(fft_start_time); fprintf('运行时间: %f s\n',fft_end_time); str = [num2str(k),'×',num2str(k),'滤波结果']; figure;imshow(finalResult); title(str); end ` filter spectrum during the process:
image used for the codes:
3.Proposed
sorry,none currently