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.