时间编译中的违规访问(0xC0000005)

时间:2011-07-18 09:57:42

标签: c++ access-violation fftw itk

我想要做的过程是对图像(存储在“imagen”中)进行FFT,然后将其与滤波器“H”相乘,之后,也将进行逆FFT。 代码如下所示:

int ancho;
int alto;
ancho=ui.imageframe->imagereader->GetBufferedRegion().GetSize()[0];     //ancho=widht of the image
alto=ui.imageframe->imagereader->GetBufferedRegion().GetSize()[1];      //alto=height of the image

double *H ;
H =matrix2D_H(ancho,alto,eta,sigma); // H is calculated

// We want to get: F= fft(f) ; H*F ; f'=ifft(H*F)
// Inicialization of the neccesary elements for the calculation of the fft
fftw_complex *out;
fftw_plan p;

int N= (ancho/2+1)*alto; //number of points of the image
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);

double *in = (double*) imagen.GetPointer(); // conversion of itk.smartpointer --> double*
p = fftw_plan_dft_r2c_2d(ancho, alto, in, out, FFTW_ESTIMATE); // FFT planning
fftw_execute(p); // FFT calculation

/* Multiplication of the Output of the FFT with the Filter H*/ 
int a = alto;
int b = ancho/2 +1; // The reason for the second dimension to have this value is that when the FFT calculation of a real image is performed only the non-redundants outputs are calculated, that’s the reason for the output of the FFT and the filter ‘H’ to be equal. 

// Matrix point-by-point multiplicaction: [axb]*[axb]
fftw_complex* res ; // result will be stored here
res = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*a*b);
res = multiply_matrix_2D(out,H, a, b);

问题位于函数'multiply_matrix_2D'的循环中:

 fftw_complex*  prueba_r01::multiply_matrix_2D(fftw_complex* out, double* H, int M ,int N){
/* The matrix out[MxN] or [n0x(n1/2)+1] is the image after the FFT , and the out_H[MxN] is the filter in the frequency domain,
both are multiplied POINT TO POINT, it has to be called  twice, one for the imaginary part and another for the normal part
*/
fftw_complex *H_cast;
H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);
H_cast= reinterpret_cast<fftw_complex*> (H); // casting from double* to fftw_complex*

fftw_complex *res; // the result of the multiplication will be stored here
res = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);

//Loop for calculating the matrix point-to-point multiplication
for (int x = 0; x<M ; x++){
    for (int y = 0; y<N ; y++){
            res[x*N+y][0] = out[x*N+y][0]*(H_cast[x*N+y][0]+H_cast[x*N+y][1]); 
            res[x*N+y][1] = out[x*N+y][1]*(H_cast[x*N+y][0]+H_cast[x*N+y][1]); 
        }
}
fftw_free(H_cast);
return res;
}

x = 95且y = 93的值为M = 191且N = 96; prueba_r01.exe中0x004273ab处的未控制异常:0xC0000005访问违规读数0x01274000。

imagen http://img846.imageshack.us/img846/4585/accessviolationproblem.png

如果变量的很多值都是红色的,并且对于翻译问题:H_cast [] [1]在值框中有:“Error30CXX0000:无法评估表达式”。

我将非常感谢您的任何帮助!

安东尼奥

2 个答案:

答案 0 :(得分:1)

这部分代码

H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);
H_cast= reinterpret_cast<fftw_complex*> (H); // casting from double* to fftw_complex*

首先为H_cast分配一个新缓冲区,然后立即将其设置为指向原始H。它不会复制数据,只会复制指针。

在函数结束时,一些缓冲区是空闲的

fftw_free(H_cast);

似乎释放H指向的数据而不释放函数中分配的缓冲区。

当回到来电者时,H就丢失了!

答案 1 :(得分:1)

ITK内部有一个FFT类,可以使用cmake的fftw(USE_FFTW)进行配置。该类描述了如何从fftw引用ITK原始缓冲区内存。

PS:即将推出的ITKv4大大提高了fftw的兼容性。

相关问题