views:

343

answers:

1
+1  Q: 

FFT of an image

Hi. I have an assignment about fftw and I was trying to write a small program to create an fft of an image. I am using CImg to read and write images. But all I get is a dark image with a single white dot :(

I'm most likely doing this the wrong way and I would appreciate if someone could explain how this should be done. I don't need the code, I just need to know what is the right way to do this.

Here is my code:

CImg<double> input("test3.bmp");
CImg<double> image_fft(input, false);

unsigned int nx = input.dimx(), ny = input.dimy();
size_t align = sizeof(Complex);

array2<Complex> in (nx, ny, align);

fft2d Forward(-1, in);

for (int i = 0; i < input.dimx(); ++i) {
    for (int j = 0; j < input.dimy(); ++j) {
        in(i,j) = input(i,j);
    }
}

Forward.fft(in);

for (int i = 0; i < input.dimx(); ++i) {
    for (int j = 0; j < input.dimy(); ++j) {
        image_fft(i,j,0) = image_fft(i,j,1) = image_fft(i,j,2) = std::abs(in(i,j));
    }
}

image_fft.normalize(0, 255);
image_fft.save("test.bmp");
+4  A: 

You need to take the log of the magnitude. The single white dot is the base value (0 Hz, DC, whatever you want to call it), so it will almost ALWAYS be by far the largest component of any image you take (Since pixel values cannot be negative, the DC value will always be positive and large).

What you need to do is calculate the log (ln, whatever, some type of logarithmic calculation) of the magnitude (so after you've converted from complex to magnitude/phase form (phasor notation iirc?)) on each point before you normalize it.

Please note that the values are there, they are just REALLY small compared to the DC value, taking the log (Which makes smaller values bigger by a lot, and bigger values only slightly larger) will make the other frequencies visible.

CookieOfFortune
Thank you very much, it works now :)
jbradaric