Why image is shifted when using ifft2

30 ビュー (過去 30 日間)
Khang Truong
Khang Truong 2019 年 10 月 2 日
コメント済み: Yulin WANG 2020 年 11 月 15 日
Hi, I'm implementing 2-D convolution by using FFT. Here is my code:
img = im2single(imread('dog.bmp'));
filter = fspecial('gaussian', 53, 3);
F = fft2(img);
mask = fft2(filter, size(img, 1), size(img, 2));
filtered_img = ifft2(F .* mask);
imshow(real(filtered_img));
Here is the original image: dog.bmp
And the result:
untitled.jpg
Why this happens? How can I fix it? Please help me.
Many thanks.
  1 件のコメント
Yulin WANG
Yulin WANG 2020 年 11 月 15 日
Well, you are applying a Gaussian Filter on an image in frequency domain and convert it back to the spatial domain, and that's why the image becomes a bit blurry than the orginal one.
I did not see anything wrong with this.

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2019 年 10 月 3 日
編集済み: Cris LaPierre 2019 年 10 月 3 日
The shift is related to your hsize value in fspecial (shifted ~0.5*hsize in both X and Y).
I'm not sure I can do any better explaining than what you can find googling, but you need to center your frequency domain in the center of the image. You can do this using fftshift and ifftshift. See here, and here.
Also note that, in the comment of the second link, there is a recommendation to use psf2otf to take the FFT of a point spread function.
After some playing around with a built in image, I came up with this code:
img = im2single(imread('autumn.tif'));
imshow(img)
filter = fspecial('gaussian', 53, 2);
F = fftshift(fft2(img));
mask = fftshift(psf2otf(filter,[size(img, 1), size(img, 2)]));
filtered_img = ifft2(ifftshift(F .* mask));
imshow(abs(filtered_img));
  1 件のコメント
Cris LaPierre
Cris LaPierre 2019 年 10 月 3 日
A little more playing suggests that you really only need to replace
mask = fft2(filter, size(img, 1), size(img, 2));
with
mask = psf2otf(filter, [size(img, 1), size(img, 2)]);
in your code.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by