1
$\begingroup$

I tried to Fourier decompose my image using FFT and reconstruct it back using IFFT. While I did this, I noticed something peculiar:

enter image description here

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
$\endgroup$
8
  • 2
    $\begingroup$ imshow does something different for integer (your g) and floating-point (your f) images. Use imshow(f,[0,255]). $\endgroup$ Sep 29 at 13:31
  • $\begingroup$ @CrisLuengo that completely blackened the image $\endgroup$
    – DatBoi
    Sep 29 at 14:02
  • $\begingroup$ 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. $\endgroup$ Sep 29 at 16:37
  • $\begingroup$ @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. $\endgroup$
    – DatBoi
    Sep 29 at 17:06
  • 1
    $\begingroup$ @DatBoi, I updated my answer to handle the case of indexed image and uint8 image. $\endgroup$
    – Royi
    Sep 29 at 18:54

2 Answers 2

4
$\begingroup$

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]);
$\endgroup$
0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Can you elaborate? $\endgroup$
    – DatBoi
    Sep 30 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.