【问题标题】:fftw - Access violation errorfftw - 访问冲突错误
【发布时间】:2013-06-25 09:03:34
【问题描述】:

我实现了一个 fftw (fftw.org) 示例来使用快速傅里叶变换... 这是代码......

我加载了一个从 uint8_t 转换为 double 的图像(此代码运行良好...)。

string bmpFileNameImage = "files/testDummyFFTWWithWisdom/onechannel_image.bmp"; 
BMPImage bmpImage(bmpFileNameImage);
vector<double>pixelColors; 
vector<uint8_t> image = bmpImage.copyBits();

toDouble(image,pixelColors,256,256, 1); 
int width = bmpImage.width();
int height = bmpImage.height();

我使用智慧文件来提高性能

FILE * file = fopen("wisdom.fftw", "r");
if (file) {
    fftw_import_wisdom_from_file(file);
    fclose(file);
} 

///*  fftw variables  */
fftw_complex *out;

double *wisdomInput = (double *) fftw_malloc(sizeof(double)*width*2*(height/2 +1 ));

const fftw_plan forward =fftw_plan_dft_r2c_2d(width,height, wisdomInput,reinterpret_cast<fftw_complex *>(wisdomInput),FFTW_PATIENT);
const fftw_plan inverse = fftw_plan_dft_c2r_2d(width, height,reinterpret_cast<fftw_complex *>(wisdomInput),wisdomInput, FFTW_PATIENT);

file = fopen("wisdom.fftw", "w");

if (file) {
    fftw_export_wisdom_to_file(file);
    fclose(file);
}

最后,我执行 fftw 库....我收到第一个访问冲突错误 函数(fftw_execute_dft_r2c),我不知道为什么......我读了这个教程: http://www.fftw.org/fftw3_doc/Multi_002dDimensional-DFTs-of-Real-Data.html#Multi_002dDimensional-DFTs-of-Real-Data。 我用 (ny/2+1) 做了一个 malloc,它是如何解释的......我不明白为什么它不起作用......我正在测试不同的尺寸......

out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * width *(height / 2 + 1));
double *result =(double *)fftw_malloc(width * (height+2)  * sizeof(double));

fftw_execute_dft_r2c(forward,&pixelColors[0],out);
fftw_execute_dft_c2r(inverse,out,result);

问候。

【问题讨论】:

  • 什么是失败的功能?第一个fftw_plan_dft_r2c_2d 电话?
  • 不,我收到了来自fftw_execute_dft_r2c的错误

标签: c++ fftw


【解决方案1】:

这是更正后的代码。 它有一些错误:

  • 它读取了错误的wise.fftw 文件(来自一些旧测试...)。现在,它总是会创建一个新的 fftw_plan 和一个新文件。
  • 我误解了带有in-place and out-of-place 参数的fftw 库是如何工作的。我必须更改 malloc 以获得“就地”的正确填充(我在 malloc 函数中添加了 +2)。
  • 为了恢复图像,我必须除以它的大小 ((width+2) * height) 如何在 link 中解释。

`

/* load image */
string bmpFileNameImage = "files/polyp.bmp";

BMPImage bmpImage(bmpFileNameImage);

int width = bmpImage.width();
int height = bmpImage.height();

vector<double> pixelColors;
vector<uint8_t> image = bmpImage.copyBits();
//get one channel from the image
Uint8ToDouble(image,pixelColors,bmpImage.width(),bmpImage.height(),1);

//We don't reuse old wisdom.fftw... It can be corrupt 
/*
FILE * file = fopen("wisdom.fftw", "r");
if (file) {
    fftw_import_wisdom_from_file(file);
    fclose(file);
} */

double *wisdomInput = (double *) fftw_malloc(sizeof(double)*height*(width+2));

const fftw_plan forward =fftw_plan_dft_r2c_2d(width,height,wisdomInput,reinterpret_cast<fftw_complex *>(wisdomInput),FFTW_PATIENT);
const fftw_plan inverse = fftw_plan_dft_c2r_2d(width,height,reinterpret_cast<fftw_complex *>(wisdomInput),wisdomInput, FFTW_PATIENT);
double *bitsColors =(double *)fftw_malloc((width) * height * sizeof(double));

for (int y = 0; y < height; y++) {
    for (int x = 0; x < width+2; x++) {
        if (x < width) {
            int currentIndex = ((y * width) + (x));
            bitsColors[currentIndex] = (static_cast<double>(result[y * (width+2) + x])) / (height*width);
        }
    }
}
fftw_free (wisdomInput);
fftw_free (out);
fftw_free (result);
fftw_free (bitsColors);

fftw_destroy_plan(forward);
fftw_destroy_plan(inverse);
fftw_cleanup();
}

`

【讨论】:

    【解决方案2】:
    fftw_execute_dft_r2c(forward,&pixelColors[0],out);
    

    你在这里做什么?数组已经有一个指针。 将其更改为 fftw_execute_dft_r2c(forward,pixelColors[0],out); 现在应该可以使用了。

    【讨论】:

    • 不是数组,是向量。我正在获取第一个元素的指针...related
    • fftw_execute_dft_r2c(forward,&amp;pixelColors[0],*out); 在这段代码中,我不确定 fttw_complex 的用途。
    • 这是方法定义void fftw_execute_dft_r2c( const fftw_plan p, double *in, fftw_complex *out);我的想法是用逆向方法(fftw_execute_dft_c2r)来恢复图像并与原始图像进行比较。
    【解决方案3】:

    也许问题就在这里(http://www.fftw.org/doc/New_002darray-Execute-Functions.html):

    [...] 满足以下条件:

    • 如果计划最初是就地创建的,则输入和输出数组相同(就地)或不同(就地) 不合时宜。

    在计划中,您正在使用就地转换参数(分配错误,顺便说一句,因为:

    double *wisdomInput = (double *) fftw_malloc(sizeof(double)*width*2*(height/2 +1 ));
    

    应该是:

    double *wisdomInput = (double *) fftw_malloc(sizeof(fftw_complex)*width*2*(height/2 +1 ));
    

    也适合输出)。

    但是您正在使用不合适的参数调用 fftw_execute_dft_r2c 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多