在试图计算2幅图像的FFT(快速傅里叶变换)时发生访问冲突

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

本文关键字:变换 傅里叶 访问冲突 计算 2幅 图像 FFT      更新时间:2023-10-16

我正在尝试对两个原始图像进行FFT。但是我得到了unhandled exception (access violation)——我不知道为什么。我正在使用fftw库。首先我读取两个图像,然后我计算FFT。但是在开始计算之前,会产生访问冲突错误。

#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;
}

fft1fft2仅足够容纳一个输出行-您需要Height行。你可能也想动态分配它们,因为一旦你有了正确的大小,它们很可能对堆栈来说太大了,例如

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

注意:以后不要忘记调用delete []来释放这些