Hello everyone,
I'm currently trying to create a program (although "create" is a wrong word as my code is based on what I find in the web) which will load an image, compute the FFT and will show the spectrum as an image.
Until now I have:
And there is a problem with the "creating spectrum" lines. Instead of a spectrum I have some "noise". What am I doing wrong? I would be very grateful for some help.Code:#include <stdio.h> #include "cv.h" #include "highgui.h" #include "fftw3.h" void iplimage_dft(IplImage*); int main(int argc, char** argv) { IplImage* img; img = cvLoadImage(argc == 2 ? argv[1] : "mustang.jpg", 0); iplimage_dft(img); return 0; } void iplimage_dft(IplImage* img) { IplImage* img1, * img2; fftw_complex* in, * dft; fftw_plan plan_f; int i, j, k, w, h, N; /* Copy input image */ img1 = cvCloneImage(img); w = img1->width; h = img1->height; N = w * h; /* Create output image */ img2 = cvCreateImage(cvSize(w, h), 8, 1); /* Allocate input data for FFTW */ in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); dft = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); /* Create plan */ plan_f = fftw_plan_dft_2d(w, h, in, dft, FFTW_FORWARD, FFTW_ESTIMATE); /* Populate input data in row-major order */ for (i = 0, k = 0; i < h; i++) { for (j = 0; j < w; j++, k++) { in[k][0] = ((uchar*)(img1->imageData + i * img1->widthStep))[j]; in[k][1] = 0.; } } /* forward DFT */ fftw_execute(plan_f); /* creating spectrum */ for (i = 0, k = 0; i < h; i++) { for (j = 0; j < w; j++, k++) ((uchar*)(img2->imageData + i * img2->widthStep))[j] = sqrt(pow(dft[k][0],2) + pow(dft[k][1],2)); } cvShowImage("iplimage_dft(): original", img1); cvShowImage("iplimage_dft(): result", img2); cvWaitKey(0); /* Free memory */ fftw_destroy_plan(plan_f); fftw_free(in); fftw_free(dft); cvReleaseImage(&img1); cvReleaseImage(&img2); }
Thanks in advance,
Narren


Reply With Quote

Bookmarks