显示包含复数的cvMatrix (CV_64FC2)

4 投票
2 回答
3580 浏览
提问于 2025-04-15 17:55

我刚接触OpenCV,想把我的Python程序的结果和OpenCV的计算结果进行比较。我的矩阵里包含复数,因为这是cvDFT的结果。Python对复数处理得很好,并且用科学计数法显示出来。但我的C++程序在使用std::cout时效果不太好。

我尝试把我的数字数组存储在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

这是在Python中的相同代码,以及输出:

>>> a = np.mgrid[:5, :5][0]
>>> a
array([[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]])
>>> np.fft.fft2(a)
array([[ 50.0 +0.j        ,   0.0 +0.j        ,   0.0 +0.j        ,
          0.0 +0.j        ,   0.0 +0.j        ],
       [-12.5+17.20477401j,   0.0 +0.j        ,   0.0 +0.j        ,
          0.0 +0.j        ,   0.0 +0.j        ],
       [-12.5 +4.0614962j ,   0.0 +0.j        ,   0.0 +0.j        ,
          0.0 +0.j        ,   0.0 +0.j        ],
       [-12.5 -4.0614962j ,   0.0 +0.j        ,   0.0 +0.j        ,
          0.0 +0.j        ,   0.0 +0.j        ],
       [-12.5-17.20477401j,   0.0 +0.j        ,   0.0 +0.j        ,
          0.0 +0.j        ,   0.0 +0.j        ]])
>>>

问题显然出在第二个cout上,它对这种数据类型(CV_64FC2,表示复数)处理得不够好。

我的问题是:我该如何输出结果,以便检查我的Python代码和我的C++/OpenCV代码是否做了一样的事情?

谢谢!

2 个回答

0

你试过用Python来操作OpenCV吗?http://www.exothermia.net/monkeys_and_robots/2009/12/11/working-opencv-python-bindings/

通过这些绑定,你可以在Python中调用OpenCV的功能,并把结果以numpy数组的形式得到。这样你就可以把这些结果和你用纯Python写的代码得到的结果进行比较。如果你愿意动手的话,还可以把自己的C代码包装起来,让它也能在Python中使用。

不过,如果你只是想保存数据的话,可能可以把实部和虚部保存成图片,然后在Python中读取它们(我对OpenCV不太熟悉,你需要查看它和Python对浮点图片的支持情况)。

4

在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)

撰写回答