Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2-D Fourier Transform?

1,027 views
Skip to first unread message

hadi motamedi

unread,
Nov 10, 2010, 6:30:01 AM11/10/10
to
Dear All
Can you please let me know how can I obtain 2-D Fourier transform of a
2-D signal say an saved image ?
Thank you

Nasser M. Abbasi

unread,
Nov 11, 2010, 6:10:09 AM11/11/10
to
On 11/10/2010 9:41 AM, Nasser M. Abbasi wrote:

>
> ------ 2D spectrum of image ------------------------------
> img=Import["ExampleData/lena.tif"];
> ImageDimensions[img] (*look at image*)
>
> ImageChannels[img] (*see how many channels*)
>
> data=ImageData[img]; (*get data*)
> {nRow,nCol,nChannel}=Dimensions[data]
>
> Map[Image[data[[All,All,#]]]&,Range[1,nChannel]](*look at each channel*)
>
> d=data[[All,All,2]]; (*get channel 2*)
> Image[d,ImageSize->400]
>
> (*center Image first*)
> indx=Flatten[Outer[List,Range[1,nRow],Range[1,nCol]],1];
> Map[(d[[ #[[1]],#[[2]] ]]=(-1)^(#[[1]]+#[[2]])*d[[#[[1]],#[[2]] ]])&,indx];
>
> (*make FFT and look at spectrum and phase*)
> fw= Fourier[d,FourierParameters->{1,1}];
>
> fudgeFactor=100; (*adjust for better viewing as needed*)
>
> ArrayPlot[Abs@(fudgeFactor * Log[1+fw]),PlotLabel->"Magnitude spectrum",ImageSize->400]
> ArrayPlot[Arg[fw],PlotLabel->"Phase spectrum",ImageSize->400]
> -----------------------------------------
>
> see my howto note #95
> http://12000.org/my_notes/mma_matlab_control/KERNEL/e95/mma/index.htm
>
> --Nasser


Opps, In the above, I took the Abs of the FFT outside the log.

Instead of

ArrayPlot[Abs@(fudgeFactor * Log[1+fw]),PlotLabel->"Magnitude spectrum",ImageSize->400]

It should be

ArrayPlot[(fudgeFactor * Log[1+Abs@fw]),PlotLabel->"Magnitude spectrum",ImageSize->400]

The fudgefactor is something one changes to get a clearer picture of the spectrum. There is no magic formula for it that I know of.

--Nasser


Nasser M. Abbasi

unread,
Nov 11, 2010, 6:10:54 AM11/11/10
to


There might be a shorter way in the digital image
processing functions in version 7 as many new
ones where added.

But this is one way to do this. If you find
any errors pls let me know:

Matthias Odisio

unread,
Nov 11, 2010, 6:10:43 AM11/11/10
to
Dear Hadi Motamedi,

On 11/10/10 5:29 AM, hadi motamedi wrote:
> Dear All
> Can you please let me know how can I obtain 2-D Fourier transform of a
> 2-D signal say an saved image ?
> Thank you

Fourier and InverseFourier works with 1D, 2D, ... arrays.

When working with images, be aware that the data after Fourier
transform does not have its lower frequencies at the center of the
array, but in the corners. It is usually annoying. You may want to
use the following work-around (Gonzalez and Woods, DIP 3e, Chapter 4):

CenteredFourier[img_] := Module[{data = ImageData[img], dim},
dim = Dimensions[data];
Fourier[data*(-1)^Table[i + j, {i, First[dim]}, {j, Last[dim]}]]
];

CenteredInverseFourier[F_] := Module[{dim},
dim = Dimensions[F];
Image[Re[
InverseFourier[F]*(-1)^
Table[i + j, {i, First[dim]}, {j, Last[dim]}]]]
];

Matthias Odisio
Wolfram Research

Barrie Stokes

unread,
Nov 12, 2010, 5:27:13 AM11/12/10
to
Hi

You could check out the function

FourierSequenceTransform[] (new in v.7), and also go to

http://www.wolfram.com/products/mathematica/newin7/content/EnhancedFourierAnalysis/ComputeA2DDiscreteTimeFourierTransform.html

found by Googling "mathematica 2d fourier transform".

You might also find

http://demonstrations.wolfram.com/2DFourierTransforms/

interesting.

HTH

Barrie

>>> On 10/11/2010 at 10:29 pm, in message <2010111011...@smc.vnet.net>,

Nasser M. Abbasi

unread,
Nov 12, 2010, 5:27:23 AM11/12/10
to
On 11/11/2010 3:10 AM, Matthias Odisio wrote:

>
> CenteredFourier[img_] := Module[{data = ImageData[img], dim},
> dim = Dimensions[data];
> Fourier[data*(-1)^Table[i + j, {i, First[dim]}, {j, Last[dim]}]]
> ];
>

Hello Matthias;

I liked your way to center the image more than the way
I did it. Even though it is the same effect, your's is
little bit more clear, and clear code is important for
me.

So, if you do not mind, I borrowed your line above to
center the image, and use it instead of mine.

I also now use Image to display the spectrum, not ArrayPlot.

Here is the updated complete script to the 2D spectrum of an image
(Mathematica 7)

----------Spectrum of 2D image ---------

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

data=ImageData[img]; (*get data*)
{nRow,nCol,nChannel}=Dimensions[data]

d=data[[All,All,2]]; (*get channel 2 to FFT but center it first*)
d=d*(-1)^Table[i+j,{i,nRow},{j,nCol}];

(*make FFT, and look at spectrum and phase*)

fw= Fourier[d,FourierParameters->{1,1}];
fudgeFactor=100; (*adjust for better viewing as needed*)

abs=fudgeFactor * Log[1+Abs@fw];
Labeled[Image[abs/Max[abs],ImageSize->500],Style["Magnitude spectrum", 18]]

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

-----------------------------------


--Nasser

0 new messages