## Why image is shifted when using ifft2

### Khang Truong (view profile)

on 2 Oct 2019
Latest activity Commented on by Cris LaPierre

### Cris LaPierre (view profile)

on 3 Oct 2019
Hi, I'm implementing 2-D convolution by using FFT. Here is my code:
filter = fspecial('gaussian', 53, 3);
F = fft2(img);
mask = fft2(filter, size(img, 1), size(img, 2));
imshow(real(filtered_img));
Here is the original image:
And the result:
Many thanks.

### Cris LaPierre (view profile)

on 3 Oct 2019
Edited by Cris LaPierre

### Cris LaPierre (view profile)

on 3 Oct 2019

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:
imshow(img)
filter = fspecial('gaussian', 53, 2);
F = fftshift(fft2(img));
mask = fftshift(psf2otf(filter,[size(img, 1), size(img, 2)]));
imshow(abs(filtered_img));

Cris LaPierre

### Cris LaPierre (view profile)

on 3 Oct 2019
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)]);