我想要做的过程是对图像进行FFT(存储在“ imagen”中),然后将其与滤波器“ 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:无法计算表达式”。

我将非常感谢对此提供的任何帮助!!

安东尼奥

最佳答案

这部分代码

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丢失了!

关于c++ - 时间编译中的违规访问(0xC0000005),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6731204/

10-12 15:53