Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

image processing - Getting Fourier Transform from Phase and Magnitude - Matlab

The magnitude and phase of a fourier transform F are defined as:

Mag = sqrt(Real(F)^2 + Imaginary(F)^2)

and

Phase = arctan(Imaginary(F)/Real(F))

Ive tried to write matlab code that takes in a grayscale image matrix, performs fft2() on the matrix and then calculates the magnitude and phase from the transform. I then wish to calculate the imaginary and real parts of the fourier transform. This is done by rearranging the first two equations into:

Real = Mag/sqrt(1 + tan(Phase)^2)

and

Imaginary = Real*tan(Phase)

and finally combining and inverse fft2:

F = Real + i*Imaginary
image = ifft2(F)

I'd expect to see the same image as the input, but i get garbage. Is my maths wrong? My matlab mfile code is as follows:

function y = forwardBackwardFFT(image)

F = fft2(image);
mag = sqrt(real(F).^2 + imag(F).^2);
phase = atan(imag(F)./real(F));

re = sqrt((mag.^2)./(1 + tan(phase).^2));
im = re.*tan(phase);
F = re + i*im;
f = ifft2(F);

subplot(1,2,1);
imshow(image);
Title('Original Image');

subplot(1,2,2);
imshow(f);
Title('Image after forward and backward FFT');
y = f;

thanks a lot :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your function is trying to test two things at once: (1) FFT and inverse FFT an image, and (2) disassemble a complex number into real and imaginary parts, transform to amplitude and phase, and then put it back together again. Instead of trying the whole thing at once and wondering why it doesn't work, you should test each of these two functions separately.

To test whether ifft(fft(image)) gives back the original image, you can just remove or comment out all the complex number manipulations:

function y = forwardBackwardFFT(image)

F = fft2(image);
%# stuff removed
f = ifft2(F);

subplot(1,2,1);
imshow(image);
title('Original Image');

subplot(1,2,2);
imshow(f, []);
title('Image after forward and backward FFT');
y = f;

This works. So the problem is with your complex number manipulations. Consider what happens when phase=0 or phase=pi/2. The tangent of 0 is 0, leading to a division by zero; and tan(pi/2) is infinite.

Here is some code that works:

mag =  sqrt(real(F).^2 + imag(F).^2);
phase = atan2(imag(F),real(F));

re = mag .* cos(phase);
im = mag .* sin(phase);
F = re + 1i*im;

You will have to do imagesc(abs(f)) in order to show the resulting inverse-transformed image, to get rid of a (nearly zero) imaginary component.

A more idiomatic way to get the magnitude and phase of a complex number is to simply do:

mag = abs(F);
phase = angle(F);

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...