0
votes

In trying to replicate an optical Fourier transform within Matlab I did the following:

I loaded this 512x512 image as "dot512r":

enter image description here

Then did:


     >> whos dot512r
     Name           Size                Bytes  Class    Attributes

     dot512r      512x512x3            786432  uint8              

     >> bwimg=rgb2gray(dot512r);
     >> whos bwimg

      Name         Size              Bytes  Class    Attributes

      bwimg      512x512            262144  uint8              

     >> ftimg=fft2(bwimg);
     >> figure
     >> imshow(log(fftshift(ftimg))/10.0);

Which results in this image (so far all's good): enter image description here

Then I did the inverse fft and displayed it like this:


    >> iftimg=ifft2(ftimg);
    >> imshow(iftimg);

Which resulted in the following image, in which I've marked the anomalies with red arrows. I can accept the two small pixel "errors" at the upper and lower left of the white bar, but the large anomaly at the upper left of the image has me perplexed. The same anomaly appeared in several other shapes that I did with 512x512 images.

I don't get what I'm doing wrong.

enter image description here

2
Did you confirm that bwimg doesn't have these anomalies? Can you also confirm that imag(iftimg) is zero?Oliver Charlesworth
I looked at bwimg and it has no anomalies. The same output anomalies as above can be seen if you just execute: imshow(ifft2(fft2(bwimg)));EddingtonsMonkey

2 Answers

0
votes

Try dividing the output of ifft by 255, you'll no longer see your anomalies.

You input is uint8, by default displayed as 0 = black, 255 = white.

Your output is doubles, by default displayed as 0 = black, 1 = white.

That is, everything above 1 is shown as pure white. A value of 1 in the input, which you couldn't see, becomes white in the output.

In principle, ifft(fft(x))-x is within numerical accuracy. But only if x is doubles to begin with.

0
votes

Ok I believe I figured out what was happening.

I started to wonder if it was a JPEG compression artifact. Jpeg's 8x8 pixel cells would have a frequency of 512/8 or 64. That's roughly where the anomaly appears.

Originally I created the image in Photoshop, and then used the "save for web" option with quality set to 100%, so I thought there would be no JPEG compression noise. I didn't notice the little check box labeled "Optimized" and it was checked.

I recreated the image from scratch and ensured everything was pure black & white and saved it without the "Optimized" box being checked.

When I reran the forward and inverse fft2 on the new image, the large anomaly was gone. Only the little ones at the corners exist, and those could be from precision loss. Here's the new output:

>>imshow(ifft2(fft2(rgb2gray(dot512f))));

enter image description here