返回

时间:2016-07-27 13:10:20

标签: c++ opencv

我使用OpenCv将euler角度转换为rotationmatrix

cv::Mat transformEulerToRotation(const cv::Mat &euler) {
  float rot[9]
  //transforming things
  //...
  cv::Mat resMat = cv::Mat(3, 3, CV_32F, rot);
  //here can I check the result
  std::cout << "resMat " << resMat << std::endl;
  return resMat;
}

void otherFunction() {
  //...
  std::cout << "rotMat function " << transformEulerToRotation(euler) << std::endl;
  cv::Mat rotMat = transformEulerToRotation(euler).clone;
  std::cout << "rotMat variable " << rotMat << std::endl;
}

结果我得到了例如:

resMat [0.99965221, -0.024546526, -0.009639469;
-0.017124595, 0.99955207, 0.024376612;
0.010061417, 0.017124617, 0.99980277]

rotMat function [6.4592681e-31, 0, 2.6510468e-36;
0, 4.291036e-38, 0;
6.4569209e-31, 0, 0.21559119]

rotMat variable [0.99965221, -0.024546526, 1.5537966e-32;
-3.7597382e+19, 0.99955207, 0.024376612;
9.3246211e-39, 0, 0.21557593]

当我刚刚返回cv :: Mat时,我不知道会发生什么变化。我该怎么做才能用相同的值取回Matrix。

2 个答案:

答案 0 :(得分:2)

我确信clone()方法有效,但如果您关心性能,则应尽量避免使用该副本。我们的想法是先创建cv::Mat,然后直接对其数据进行操作。在你的情况下

cv::Mat transformEulerToRotation(const cv::Mat &euler)
{
  cv::Mat rotMat(3, 3, CV_32F);
  float* rot = rotMat.ptr<float>();
  // You can operate on rot now
  // transforming things
  // ...
  // Check the result
  std::cout << "rotMat" << rotMat<< std::endl;
  return rotMat;
}

答案 1 :(得分:0)

退出函数后,缓冲区rot将包含垃圾值。您需要clone()矩阵来创建内部数据的深层副本,因为简单的副本只会创建指向相同内部数据的另一个Mat标头(这将被破坏)。

所以你可以:

return resMat.clone();

或避免创建中间矩阵:

return cv::Mat(3, 3, CV_32F, rot).clone();