12
$\begingroup$

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.

$\endgroup$
  • 4
    $\begingroup$ 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. $\endgroup$ – Kuba Jul 26 '13 at 7:23
  • $\begingroup$ How about reading the documentation, like that on the Fourier function? $\endgroup$ – Sjoerd C. de Vries Jul 26 '13 at 8:52
  • 2
    $\begingroup$ 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. $\endgroup$ – Leo Sep 26 '13 at 19:03
32
$\begingroup$
img = Import["ExampleData/lena.tif"];
Image[img, ImageSize -> 300]

Mathematica graphics

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]]

Mathematica graphics

arg = Arg@fw;
Labeled[Image[arg/Max[arg], ImageSize -> 300],Style["Phase spectrum", 18]]

Mathematica graphics

ps. I do think Ms. Lena looks prettier in the spatial domain than in the frequency domain.

$\endgroup$
  • $\begingroup$ I think OP wants to reconstruct the image from spectrum. $\endgroup$ – Kuba Jul 26 '13 at 7:18
  • $\begingroup$ Thank You very much for the help.... Is there not a direct command of 2D DFT in MMA as 1D DFT in MMA???? $\endgroup$ – user8727 Jul 26 '13 at 10:28
  • $\begingroup$ 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! $\endgroup$ – Leo Sep 26 '13 at 19:05
  • $\begingroup$ I have post a question about your solution's extend.If you are available,help me please. $\endgroup$ – yode Mar 26 '16 at 9:44
  • $\begingroup$ 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? $\endgroup$ – FACamargo Sep 15 '16 at 12:17
11
$\begingroup$

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

enter image description here

enter image description here

To invert these, use:

Chop[InverseFourier[(abs E^(I arg))]] // Image

enter image description here

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.

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

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.

$\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.