使用队列在OpenCV中保留翻译和旋转矩阵 [英] Using a Queue to keep Translation and Rotational Matrices in OpenCV

查看:349
本文介绍了使用队列在OpenCV中保留翻译和旋转矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但是当我从队列中弹出元素时,我可以得到的是同一对旋转和翻译的副本。 b
$ b

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

  // ------------ Ah ----------- ------- 
struct RotMat {
CvMat * rotation_;
CvMat * translation_;
};


// ----------- Bh -------------------
类B {

private:
CvMat * rotate;
CvMat * translation;
}


//-----------B.cpp-----------------
#includeAh
#includeBh

void functionx(){

queue< RotMat> rtq;

//开始循环

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

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

// end loop

while(!rtq.empty()){//假设rtq有n个元素

RotMat rt_ = rtq。面前();
rtq.pop();
cout<< rt_.translation _-> data.fl [1]; //相同的值对是
cout<< rt_.rotation-> data.fl [1]; // print in all n iterings

}
}

测试结果



 检测到的总标记= 2 

标记1:翻译:-249.227
旋转:-0.0124926

标记2:翻译:-249.227
旋转:-0.0124926


解决方案

您正在将指针推入队列,但是您正在重用相同的内存位置。



之前创建新的旋转和翻译矩阵 cvFindExtrinsicCameraParams2 。然后将新创建的矩阵指针推入队列。



您可能需要查看使用智能指针,因此您没有内存泄漏问题。或者,只需使用 Mat 类,并让它管理你的数据。


I need to use a Queue to keep rotation and translation matrices generated (using cvFindExtrinsicCameraParams2()) for each contour.

But when I pop elements from the queue all I can get is copies of the same pair of rotation and translation.

I doubt it has something to do with pointers, but when I tried to avoid pointers in the struct, the cvFindExtrinsicCameraParams2() threw an Exception. How to fix this?

//------------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

        } 
}

Test Results

Total markers detected = 2

Marker 1: Translation: -249.227
          Rotation: -0.0124926

Marker 2: Translation: -249.227
          Rotation: -0.0124926

解决方案

You are pushing pointers into the queue, but you are reusing the same memory locations. So, all of your queue items are pointing to the same location in memory.

Create new rotation and translation matrices before calling cvFindExtrinsicCameraParams2. Then push the newly created matrix pointers onto the queue.

You may want to look at using smart pointers, so you don't have memory leak issues. Or, just use the Mat class, and let it manage the data for you.

这篇关于使用队列在OpenCV中保留翻译和旋转矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆