# Inverse fft does not give back the same image

I tried to Fourier decompose my image using FFT and reconstruct it back using IFFT. While I did this, I noticed something peculiar: The first image is the regular one, and the second is the image I constructed by re-combining the DFT:

[g, map]=imread("skull.png");

gdft=fft2(g);
f=ifft2(gdft);

subplot(1,2,1)
imshow(g,map)
subplot(1,2,2)
imshow(f)


As you can see, I applied the function and the inverse successively which means there should technically be no change in the image. Why is there such a noticeable change then? I think there is a loss of information on the relative magnitudes of gray values to some extent.

Maybe relevant details:

• The program was written in Matlab
• The image is 16x16 grayscale in nature
• imshow does something different for integer (your g) and floating-point (your f) images. Use imshow(f,[0,255]). Sep 29 at 13:31
• @CrisLuengo that completely blackened the image Sep 29 at 14:02
• Oh, g is an index into the color map... you should do g = ind2rgb(g,map); before processing it. This will likely give you an RGB image rather than a gray-scale image, so then do g = rgb2gray(g);. You can now display imshow(g), and compute f=ifft2(fft2(g)), and see that f-g is small everywhere. Sep 29 at 16:37
• @CrisLuengo somehow that did end up working! Why exactly do we have to go back and forth between rgb and grayscale? I remember doing some image processing in Julia and it was very straightforward. Sep 29 at 17:06
• @DatBoi, I updated my answer to handle the case of indexed image and uint8 image.
– Royi
Sep 29 at 18:54

When you use imread() on the image, the output is array of uint8.
When you apply the fft2() it converts it to double hence also the output of ifft2() is double.

When you plot using imshow() image of class double it uses [0, 1] range while the data is in the range [0, 255].

The other issue is having an image with mapped colors.

#### Handle Indexed / Mapped Image

Credit to @CrisLuengo who spotted this.

[mI, mG] = imread('https://i.imgur.com/FhCgvnX.png');
mI = ind2rgb(mI, mG); %<! Scales into [0, 1]

mII = ifft2(fft2(mI), 'symmetric');

figure();
subplot(1, 2, 1);
imshow(mI);
subplot(1, 2, 2);
imshow(mII);


#### Handle General RGB Image

I saved the RGB image (Output of ind2rgb(mI, mG)) as a PNG then this will work:

mI = imread('https://i.imgur.com/HhtNo4L.png'); %<! UINT8

mII = ifft2(fft2(mI), 'symmetric'); %<! Double in the range [0, 255]

figure();
subplot(1, 2, 1);
imshow(mI);
subplot(1, 2, 2);
imshow(mII, [0, 255]);


Then obviously the function doesnt use the identity kernel for the convolution.You can write your own code to do the convolution by using the identity kernel.

• Can you elaborate? Sep 30 at 13:21