# Calculate the 2D Fourier transform of an Image

I am new to Mathematica, and using version 8.0.

I would like to calculate the 2D Fourier Transform of an Image with Mathematica and plot the magnitude and phase spectrum, as well as reconstruct the image with the inverse transform.

• Hello. You do not have to sign in post, you only need to update your profile data and we can see it on miniature. Also, it is good to show some research effort and minimal sample of data (image) you are working with. – Kuba Jul 26 '13 at 7:23
• How about reading the documentation, like that on the Fourier function? – Sjoerd C. de Vries Jul 26 '13 at 8:52
• Coming from another language its not clear at all that you need to convert your imagedata to (?image-)data (other than just loading it) before doing DFT. I got stuck for hours on this as well. The question and its answer are useful to me, thanks. – Leo Sep 26 '13 at 19:03

## 3 Answers

img = Import["ExampleData/lena.tif"];
Image[img, ImageSize -> 300] data = ImageData[img];(*get data*)
{nRow, nCol, nChannel} = Dimensions[data];
d = data[[All, All, 2]];
d = d*(-1)^Table[i + j, {i, nRow}, {j, nCol}];
fw = Fourier[d, FourierParameters -> {1, 1}];

(*adjust for better viewing as needed*)
fudgeFactor = 100;
abs = fudgeFactor*Log[1 + Abs@fw];
Labeled[Image[abs/Max[abs], ImageSize -> 300],Style["Magnitude spectrum", 18]] arg = Arg@fw;
Labeled[Image[arg/Max[arg], ImageSize -> 300],Style["Phase spectrum", 18]] ps. I do think Ms. Lena looks prettier in the spatial domain than in the frequency domain.

• I think OP wants to reconstruct the image from spectrum. – Kuba Jul 26 '13 at 7:18
• Thank You very much for the help.... Is there not a direct command of 2D DFT in MMA as 1D DFT in MMA???? – user8727 Jul 26 '13 at 10:28
• Nice answer. Very basic question that requires knowledge of some differences between Mathematica and other languages. Im glad you gave him such a complete answer, it helped me too. And plus one for the PS! – Leo Sep 26 '13 at 19:05
• I have post a question about your solution's extend.If you are available,help me please. – yode Mar 26 '16 at 9:44
• In your answer, I did not understand the need for line d = d*(-1)^Table[i + j, {i, nRow}, {j, nCol}];d = d*(-1)^Table[i + j, {i, nRow}, {j, nCol}]; Could you comment on that? Also, are you computing the DFT only for the green color? Why did you pick this one? – FACamargo Sep 15 '16 at 12:17

Taking the Fourier transform is easy and fun!

Let's strip away some of the complexities. First, remove the color from the image, since this just complicates things (you can always take the transform of each color channel separately).

img = ColorConvert[Import["ExampleData/lena.tif"], "Grayscale"];


Here are the magnitude and phase of the Fourier transform:

(abs = Abs[Fourier[ImageData[img]]]) // Image
(arg = Arg[Fourier[ImageData[img]]]) // Image  To invert these, use:

Chop[InverseFourier[(abs E^(I arg))]] // Image which is the same as we started with. The Chop is needed because spurious (almost zero) complex numbers remain after the Inverse).

Notice how clear and concise this is... it's just taking Fourier of the image, taking the Abs and Arg to get the magnitude and phase images, and then reconstructing using InverseFourier on Abs*Exp[I Arg].

This is a bit simpler than usual. For one thing, the zero frequency is in the upper left hand corner (instead of the center, where one usually plots it). You can move this to the center using RotateLeft on the transformed image, or by multiplying in the spatial domain by +/-1^(i,j) as in Nasser's answer. Of course, then, to reconstruct, you have to rotate back. This also uses the default FourierParameters, which may or may not be exactly what you want.

• +1 for stripping all superfluous distraction away, and zeroing in on the essence of the answer. – Paul_A Dec 23 '17 at 19:52

Mathematica also has a builtin function called ImagePeriodogram that returns an Image representing the power spectrum of the image. This won't help with phase spectra or reconstructing images, however.