2
$\begingroup$

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!

$\endgroup$
1
  • 2
    $\begingroup$ 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. $\endgroup$
    – user484
    Mar 7, 2014 at 12:11

2 Answers 2

4
$\begingroup$

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)
    ]
$\endgroup$
1
  • $\begingroup$ RotateLeft can rotate rows and columns in one go: RotateLeft[ft, {hshift, wshift}] $\endgroup$ Mar 7, 2014 at 17:17
2
$\begingroup$

You schould always share some code :)

pic = Import["https://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

$\endgroup$
1
  • $\begingroup$ @Matt so, is this what you are after? $\endgroup$
    – Kuba
    Mar 7, 2014 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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