简体   繁体   English

尝试计算2张图像的FFT(快速傅立叶变换)时发生访问冲突

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

I am trying to take FFT of two raw images. 我正在尝试对两个原始图像进行FFT。 But I am getting unhandled exception (access violation) - I could not figure out why. 但是我遇到了unhandled exception (access violation) -我不知道为什么。 I am using fftw library. 我正在使用fftw库。 First I am reading two images, then I calculate FFT. 首先,我读取两个图像,然后计算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;
}

fft1 and fft2 are only large enough to hold one output row - you need Height rows. fft1fft2仅足够容纳一个输出行-您需要Height行。 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, eg 您可能还希望动态分配它们,因为一旦您具有正确的大小,它们对于堆栈来说就太大了,例如

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. 注意:不要忘记调用delete []以便稍后释放它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM