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.
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
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);
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
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);
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:
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
- Middle Right: result using
- Bottom Left: result using
- Bottom Right: correct result using