Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Recently, I have encountered an issue with ArrayPlot after performing a Fourier transform of a table.

ArrayPlot of a 2D table with precalculated values

Picture presented above is an ArrayPlot of a 2D table. Using Fourier on this 2D table I obtain the following ArrayPlot:

Fourier transform of the first image.

After this procedure one ends up with the result which consists of data at four vertices of the square. The thing is that I would like the result to be in this form:

enter image description here

I have tried different Fourier transform parameters but none of them had delivered the demanded image.

Thank you in advance for your reply!

share|improve this question
2  
I guess what you're looking for is an analogue of Matlab's fftshift. It's not really an issue with ArrayPlot, it's an question of visualizing the result of the Fourier transform. –  Rahul Narain Mar 7 at 12:11

2 Answers 2

You schould always share some code :)

pic = Import["http://i.stack.imgur.com/y1IuJ.png"]
dat = ImageData@ColorConvert[pic, "GrayScale"];
Dimensions@dat

(img = ArrayPad[
        Abs[Fourier[dat]][[2 ;; 154, 2 ;; 154]],
       {{153, 0}, {153, 0}},
       "Reversed"]) // Image // ImageCrop // ColorNegate

enter image description here

img // Image // ImageCrop[#, {60, 30}] & // ColorNegate
% // ImageAdjust

enter image description here

enter image description here

share|improve this answer
    
@Matt so, is this what you are after? –  Kuba Mar 7 at 11:51

This is really ancient code that should do what you need. It was written on MMA version 3 or 4 (or 5??) - I don't remember - and I have not tested it on newer versions. If it does not work you'll probably have to find a newer procedure to extract the dimensions from an image.

So, here's the code, it works on a matrix of values and gives you a matrix that represents the (generally complex) FFT you are after:

FFT2D[mat_] := Module[{ft, hshift, wshift},
    ft = Fourier[mat];
    {hshift, wshift} = IntegerPart[(# + .5)/2] & /@ Dimensions[ft];
    Transpose[RotateLeft[
        Transpose[RotateLeft[
            ft,
            hshift]],
        wshift]]
]

This is how I used it back then (again, you might have to adjust the code, possibly by changing ListDensityPlot with ArrayPlot)

mat = GrayScaleMatrix["Image1.bmp"];
(* this was a silly conversion from color to grayscale, see later *)
{hshift, wshift} = IntegerPart[(# + .5)/2] & /@ Dimensions[mat]
(* this is just extracting the dimensions of the image*)
ListDensityPlot[mat, MeshRange -> {{-wshift, wshift}, {-hshift, hshift}}]
(* and this is plotting it to show the gray scaled image *)

Instead of Fourier, I would use FFT2D. That's it.

fft = FFT2D[mat];

Plotting might require the previously extracted information about the image's dimensions.

ListDensityPlot[Abs[fft], MeshRange -> {{-wshift, wshift}, {-hshift, hshift}}]

Addendum - And this is the procedure I used to convert color bitmaps into gray scale ones (I wanted to experiment with actual pictures and I did not have the time to figure out how to properly create a gray scaled version). I include it here just in case you want to try with the actual data it has worked for me

GrayScaleMatrix[filename_String] := Module[{pic},
    pic = Import[filename];
    pic[[1, 1]] /. {r_, g_, b_} :> (.3r + .6g + .1b)
    ]
share|improve this answer
    
RotateLeft can rotate rows and columns in one go: RotateLeft[ft, {hshift, wshift}] –  Simon Woods Mar 7 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.