【问题标题】:Using a Queue to keep Translation and Rotational Matrices in OpenCV使用队列在 OpenCV 中保留平移和旋转矩阵
【发布时间】:2011-11-23 07:30:14
【问题描述】:

我需要使用队列来保持为每个轮廓生成旋转和平移矩阵(使用cvFindExtrinsicCameraParams2())。

但是当我从队列中弹出元素时,我只能得到同一对旋转和平移的副本。

我怀疑它与指针有关,但是当我试图避免 struct 中的指针时,cvFindExtrinsicCameraParams2() 抛出了异常。如何解决这个问题?

//------------A.h------------------
struct RotMat{
    CvMat *rotation_;
    CvMat *translation_;
};


//-----------B.h-------------------
class B {

private:
        CvMat *rotation;
        CvMat *translation;
} 


//-----------B.cpp-----------------
#include "A.h"
#include "B.h"

void functionx(){

        queue<RotMat> rtq;

        // start loop

                // cvFindExtrinsicCameraParams2(&object_points,&image_points,
                //                               intrinsic,distortion,
                //                                rotation,translation);

                RotMat rt = {rotation, translation};
                rtq.push(rt);

        // end loop

        while(!rtq.empty()) { //assume rtq has n elements

                RotMat rt_ = rtq.front();
                rtq.pop();
                cout<< rt_.translation_->data.fl[1]; // the same value pair is 
                cout<< rt_.rotation->data.fl[1];     // printed in all n iterations

        } 
}

测试结果

Total markers detected = 2

Marker 1: Translation: -249.227
          Rotation: -0.0124926

Marker 2: Translation: -249.227
          Rotation: -0.0124926

【问题讨论】:

    标签: c++ pointers opencv struct


    【解决方案1】:

    您正在将指针推入队列,但您正在重用相同的内存位置。因此,所有队列项都指向内存中的同一位置。

    在调用cvFindExtrinsicCameraParams2之前创建新的旋转和平移矩阵。然后将新创建的矩阵指针推送到队列中。

    您可能想考虑使用智能指针,这样就不会出现内存泄漏问题。或者,只需使用 Mat 类,让它为您管理数据。

    【讨论】:

    • 我发现了这一点并使用 CvClone 来避免它。
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 2013-04-29
    • 2014-06-10
    • 2012-09-09
    • 2016-06-26
    • 1970-01-01
    • 2013-12-06
    • 2014-08-24
    相关资源
    最近更新 更多