C++ 显示包含复数的CVF矩阵(CV_64FC2)

C++ 显示包含复数的CVF矩阵(CV_64FC2),c++,python,opencv,numpy,C++,Python,Opencv,Numpy,我是OpenCV新手,我想将python程序的结果与我在OpenCV中的计算结果进行比较。我的矩阵包含复数,因为它是cvDFT的结果。Python可以很好地处理复杂的数字,并用科学的表示法来显示它。当使用STD::CU../P>时,我的C++程序不起作用。 我试图将数字数组存储在std::complex[]而不是double[]中,但它没有编译 以下是我的代码及其结果: CvMat *dft_A; dft_A = cvCreateMat(5, 5, CV_64FC2); //

我是OpenCV新手,我想将python程序的结果与我在OpenCV中的计算结果进行比较。我的矩阵包含复数,因为它是cvDFT的结果。Python可以很好地处理复杂的数字,并用科学的表示法来显示它。当使用STD::CU../P>时,我的C++程序不起作用。 我试图将数字数组存储在std::complex[]而不是double[]中,但它没有编译

以下是我的代码及其结果:

    CvMat *dft_A;

    dft_A = cvCreateMat(5, 5, CV_64FC2); // complex matrix
    double a[] = {
        0, 0, 0, 0, 0,
            1, 1, 1, 1, 1,
            2, 2, 2, 2, 2,
            3, 3, 3, 3, 3,
            4, 4, 4, 4, 4
           };
    dft_A->data.db = a;
    std::cout << "before : " << a[0] << std::endl;
    cvDFT( dft_A, dft_A, CV_DXT_FORWARD);  // DFT !
    std::cout << "after : " << a[0] << std::endl;


        >> before : 0
问题显然来自于第二个cout,它的日期类型效率低下(复数为CV_64FC2)

我的问题是:如何转储结果,以便检查python代码是否与cpp/opencv代码相同


谢谢

您尝试过OpenCV的python绑定吗?

通过绑定,您可以从python调用OpenCV函数,并将结果作为numpy数组,然后将它们与纯python代码中的结果进行比较。通过一些修补,您可以包装自己的C代码,并使其在python中也可用


但是,如果您只想转储数据,您可能可以将实部和虚部保存为图像,并用python读取它们(我对OpenCV不太熟悉,您必须检查其对浮点图像的支持,以及python对浮点图像的支持)。

OpenCV 2.0代码中有一个dft示例,我现在也在学习。这里有一个复制粘贴给你,可能会给你一个想法。正如您所看到的,它使用cvSplit来分割实部和虚部。希望这有助于:

im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !im )
    return -1;

realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);

cvScale(im, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);

dft_M = cvGetOptimalDFTSize( im->height - 1 );
dft_N = cvGetOptimalDFTSize( im->width - 1 );

dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);

// copy A to dft_A and pad dft_A with zeros
cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
cvCopy( complexInput, &tmp, NULL );
if( dft_A->cols > im->width )
{
    cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
    cvZero( &tmp );
}

// no need to pad bottom part of dft_A with zeros because of
// use nonzero_rows parameter in cvDFT() call below

cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );

cvNamedWindow("win", 0);
cvNamedWindow("magnitude", 0);
cvShowImage("win", im);

// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );

// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re, NULL);
cvPow( image_Re, image_Re, 0.5 );

// Compute log(1 + Mag)
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
cvLog( image_Re, image_Re ); // log(1 + Mag)
im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !im )
    return -1;

realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);

cvScale(im, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);

dft_M = cvGetOptimalDFTSize( im->height - 1 );
dft_N = cvGetOptimalDFTSize( im->width - 1 );

dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);

// copy A to dft_A and pad dft_A with zeros
cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
cvCopy( complexInput, &tmp, NULL );
if( dft_A->cols > im->width )
{
    cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
    cvZero( &tmp );
}

// no need to pad bottom part of dft_A with zeros because of
// use nonzero_rows parameter in cvDFT() call below

cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );

cvNamedWindow("win", 0);
cvNamedWindow("magnitude", 0);
cvShowImage("win", im);

// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );

// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re, NULL);
cvPow( image_Re, image_Re, 0.5 );

// Compute log(1 + Mag)
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
cvLog( image_Re, image_Re ); // log(1 + Mag)