PDA

View Full Version : FFTW inQt



Melkman
12th May 2015, 21:38
Hi all! I am very noob in Qt and I'd like to get the fft of an image to do some edge detection with a high pass filter. I'm using Qt Creator 3 on Windows 8, and the code I'm posting crashes with this output:

Starting C:\Users\Appretince\Documents\NWU\4de Jaar\1ste Sem\EERI315(413)- Seinteorie II\Prak1\Qt\build-Part2_Image-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\Part2_Image.exe...

The program has unexpectedly finished.

C:\Users\Appretince\Documents\NWU\4de Jaar\1ste Sem\EERI315(413)- Seinteorie II\Prak1\Qt\build-Part2_Image-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\Part2_Image.exe crashed

It bombs out on the in and out declaration, I think it might be that I'm using the fftw_malloc wrong. This is my code:

QImage gray_image(ui->dspGrayscale->pixmap()->toImage());
ui->dspFourierMag->setPixmap(QPixmap::fromImage(gray_image));
ui->dspFourierPhs->setPixmap(QPixmap::fromImage(gray_image));

fftw_complex *in, *out;
fftw_plan p;
width=gray_image.width();
height=gray_image.height();
QImage fmag_image(gray_image.size(),QImage::Format_RGB32) ;
QImage fphs_image(gray_image.size(),QImage::Format_RGB32) ;
in=(fftw_complex *)fftw_malloc(sizeof(fftw_complex)*width*height);
out=(fftw_complex *)fftw_malloc(sizeof(fftw_complex)*width*height);

p=fftw_plan_dft_2d(width,height,in,out,FFTW_FORWAR D,FFTW_MEASURE);
k=0;

for(x=0;x<width;x++)
{
for(y=0;y<height;y++)
{
in[k][0]=gray_image.pixel(x,y); //Real part of input
in[k++][1]=0; //Imaginary part of input
}
}

fftw_execute(p);
k=0;

for(x=0;x<width;x++)
{
for(y=0;y<height;y++)
{
fpixel=out[k][0];
fmag_image.setPixel(x,y,fpixel);

fpixel=out[k++][1];
fphs_image.setPixel(x,y,fpixel);
}
}

fftw_destroy_plan(p);
ui->dspFourierMag->setPixmap(QPixmap::fromImage(fmag_image));
ui->dspFourierPhs->setPixmap(QPixmap::fromImage(fphs_image));

Any help would be much appreciated. Thank you.

ChrisW67
12th May 2015, 21:58
My guess is that the value of k goes out of bounds. Run your program in a debugger and inspect the backtrace and values actually in variables at the time of the crash.

BTW There is a fftw_malloc_complex() convenience function you could use.

Melkman
13th May 2015, 10:11
If I comment out the k loop it still doesn't work. It crashes whenever I run these commands:

in=(fftw_complex *)fftw_malloc(sizeof(fftw_complex)*width*height);
out=(fftw_complex *)fftw_malloc(sizeof(fftw_complex)*width*height);

I've tried lowering the size but it still crashes. Thanks for the help though.

ChrisW67
13th May 2015, 13:11
Are you certain it is the allocations and not the subsequent accesses?
See the notes about indexing: http://www.fftw.org/doc/Dynamic-Arrays-in-C.html#Dynamic-Arrays-in-C
and look at what you are doing in your nested loops.

d_stranz
16th May 2015, 16:51
Run your program in a debugger and inspect the backtrace and values actually in variables at the time of the crash.

Have you done what ChrisW67 suggested, and debug to see what the values actually are when the crash occurs? Random guesses about what might be going wrong might get you to something that works, but you'll have no idea why. Your code will remain fragile and ready to crash again at the next unhappy combination of parameters.