2
$\begingroup$

Ok, so we have an image that is a Fourier inverse of the original picture. We want to get the original picture back. We use Matlab to get that job done. We import the image and then we invert it with the help of ifft(), this gives us a matrix with complex numbers. But to get the original picture we need to do some operation on the complex numbers to get it. But what is that operations. I tried the magnitude, real and imaginary part but this doesn't create the picture we want.

$\endgroup$
2
$\begingroup$

To apply IFFT you need back the signal do complex numbers, you need use magnitude and phase information to rebuild correctly.

The real part is = magnitude * cos(phase)

The imaginary part is = magnitude * sin(phase)

You can use square roots of −1 (sqrt(-1)) to get Imaginary unit.

Now multiply imaginary unit with imaginary part and sum with real part, OK now are you done to apply IFFT !

At the end I apply a mat2gray function to convert the matrix to the intensity!

here how it is really done in matlab:

x=imread('C:\Users\Eder\Pictures\download.jpg');
figure(1);imshow(x);
%Make FFT
y=fft(x);
%Amplitude of the FFT
mx=abs(y);
%get Phase Information
ma=angle(y);
%back the signal to complex
y2= mx .* ( cos(ma) + sqrt(-1) * sin(ma) );
%Apply Inverse FFT
x2=real(ifft(y2));
result=mat2gray(x2);
figure(2);imshow(result);
$\endgroup$
  • $\begingroup$ So I need to use the real part of the matrix I get from the function ifft(). But that image looks like shit. $\endgroup$ – user1200276 Sep 17 '14 at 20:16
  • $\begingroup$ My code show how do, here working nice :-) $\endgroup$ – ederwander Sep 17 '14 at 20:21
  • $\begingroup$ But the image I use is already inverted so how can I find the phase and amplitude if the matrix only consist of values of 1-256 $\endgroup$ – user1200276 Sep 17 '14 at 21:51
  • $\begingroup$ @user1200276: No, the answer specifically states that you need the real and the imaginary component of the ifft output. Do you know what a complex number is? $\endgroup$ – MSalters Sep 22 '14 at 11:23
0
$\begingroup$

I know this is an old question but I had a similar problem and figured I would share the results.

Below is a typical fft image pipeline. Couple things to note:

  • When dealing with images use ifft2() instead of ifft() to perform a 2D transform.
  • The frequency domain image is usually shifted to put low frequencies in the center. Use ifftshift() to reverse this.
  • Only the real part of the inverse transform is needed. The imaginary parts should be close to zero.
  • The magnitudes of the frequency domain image are usually scaled logarithmically for viewing purposes. If you already have the complex values then this probably isn't an issue.

DFT MATLAB pipeline:

%% Original Image
img = imread('lenna.png');
figure(1); imshow(img); % show image

%% Frequency Domain (FFT)
freqDomain = fft2(img);
% swap quadrants
shifted = fftshift(freqDomain);

%% Adjust scaling for viewing (not necessary)
% get magnitude
mag = abs(shifted);
% find the largest value
largest = max(mag(:));
% set the scale factor relative to the largest magnitude
c = (1.0 / log(1.0 + largest));
% scale magnitudes logarithmically
scaled = arrayfun(@(x) c * log(1.0 + x), mag);
% show scaled image
figure(2); imshow(scaled);

%% Spectral Domain (IFFT)
% swap quadrants back
shifted = ifftshift(shifted);
% get real part of ifft
specDouble = real(ifft2(shifted));
% convert result and show image
specDomain = uint8(specDouble);
figure(3); imshow(specDomain);

Results:

If you scale the magnitudes of your frequency image and get something resembling the left image you probably need to use ifftshift() to get it looking like the right image:

fft shift example

The following are some potentially incorrect results (and a correct one) using the traditional Lenna image. The frequency domain is shown above.

  • Top: original image
  • Middle Left: result of ifft2() without using ifftshift() first
  • Middle Right: result using ifft() instead of ifft2()
  • Bottom Left: result using ifft() without ifftshift() first
  • Bottom Right: correct result using ifftshift() then ifft2()

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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