首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示包含复数的cvMatrix (CV_64FC2)

显示包含复数的cvMatrix (CV_64FC2)
EN

Stack Overflow用户
提问于 2010-01-12 11:41:04
回答 2查看 3.5K关注 0票数 4

我是OpenCV新手,我想将python程序的结果与我在OpenCV中的计算结果进行比较。我的矩阵包含复数,因为它是cvDFT的结果。Python可以很好地处理复数,并用科学记数法显示它。我的C++程序在尝试使用std::cout时无效。

我试图将我的numbers数组存储在std::complex[]中,而不是double[]中,但是它不能编译。

下面是我的代码,以及它的结果:

代码语言:javascript
运行
AI代码解释
复制
    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中的相同内容,输出如下:

代码语言:javascript
运行
AI代码解释
复制
>>> 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,它对date类型(复数的CV_64FC2)的效率很低。

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

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-14 18:13:58

在OpenCV 2.0代码中有一个dft示例,我现在也在研究它。这里有一个拷贝粘贴给你,它可能会给你一个想法。正如您所看到的,它使用cvSplit将其扩展为实部和虚部。希望这能有所帮助:

代码语言:javascript
运行
AI代码解释
复制
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)
票数 4
EN

Stack Overflow用户

发布于 2010-01-22 16:19:46

你有没有尝试过OpenCV的python绑定?http://www.exothermia.net/monkeys_and_robots/2009/12/11/working-opencv-python-bindings/

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

但是如果你只想转储数据,你可以将真实的和虚构的部分保存为图像并在python中读取它们(我对OpenCV不是很熟悉,你必须检查它对浮动图像的支持-以及python的支持)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2048577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档