MShah
MShah

Reputation: 11

Access violation while trying to compute FFT (fast Fourier transform) of 2 images

I am trying to take FFT of two raw images. But I am getting unhandled exception (access violation) - I could not figure out why. I am using fftw library. First I am reading two images, then I calculate FFT. But before it starts calculating, it produces access violation error.

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "fftw3.h"

#define Width 2280
#define Height 170

unsigned char im2[170*2280];
unsigned char im1[170*2280];
float image1[170*2280];
float image2[170*2280];

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    FILE* fp1, *fp2;

    //Read two images

    fp1 = fopen ("image1.raw" , "r");
    fread(im1, sizeof(unsigned char), Width* Height, fp1);

    fp2 = fopen ("image2.raw" , "r");
    fread(im2, sizeof(unsigned char), Width* Height, fp2);


    fclose(fp2);
    fclose(fp1);

    //Typecasting two images into float

    for (int i = 0; i < Width* Height; i++)
    {       
        image1[i]= (float)im1[i];
        image2[i] = (float)im2[i];

    }

    fftwf_plan fplan1, fplan2;
    fftwf_complex fft1[((Width/2)+1)*2];
    fftwf_complex fft2[((Width/2)+1)*2];

    fplan1 = fftwf_plan_dft_r2c_2d(Height, Width, (float*)image1, fft1, FFTW_ESTIMATE);
    fftwf_execute(fplan1);
    fftwf_destroy_plan(fplan1);

    fplan2 = fftwf_plan_dft_r2c_2d(Height,Width, image2, (fftwf_complex*)fft2, FFTW_ESTIMATE);
    fftwf_execute(fplan2);
    fftwf_destroy_plan(fplan2);

    _getch();
    return 0;
}

Upvotes: 0

Views: 330

Answers (1)

Paul R
Paul R

Reputation: 213080

fft1 and fft2 are only large enough to hold one output row - you need Height rows. You'll probably want to allocate them dynamically too, as they will most likely be too large for the stack once you have the correct size, e.g.

fftwf_complex *fft1 = new fftwf_complex[((Width/2)+1)*2*Height];
fftwf_complex *fft2 = new fftwf_complex[((Width/2)+1)*2*Height];

NB: don't forget to call delete [] to free these later.

Upvotes: 4

Related Questions