将项目点从 C 转换为 C++

Converting projectPoints from C to C++

本文关键字:转换 C++ 项目      更新时间:2023-10-16

我正在尝试将cvProjectPoints2的简单代码转换为C++,所以我正在使用cv::P rojectPoints。 我using cv命名空间以避免在所有内容前面加上cv::

Mat_<double>*    object_points      = new Mat_<double>(10, 3, CV_64FC1);
Mat_<double>*    rotation_vector    = new Mat_<double>(3,3, CV_64FC1);
Mat_<double>*    translation_vector = new Mat_<double>(Size(3,1), CV_64FC1);
Mat_<double>*    intrinsic_matrix   = new Mat_<double>(Size(3, 3), CV_64FC1);
vector<Point2f>* image_points       = new vector<Point2f>;
double t[] = {
   70, 95, 120
};
double object[] = {
   150, 200, 400,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0,
   0,0,0
};
double rotation[] = {
    0, 1, 0,
   -1, 0, 0,
    0, 0, 1
};
double intrinsic[] = {
   -500, 0,   320,
    0,  -500, 240,
    0,   0,   1
};
int main() {
   for (int i = 0; i < 30; i++) {
      (*object_points)[i/3][i%3] = object[i];
   }
   for (int i = 0; i < 9; i++) {
      (*rotation_vector)[i/3][i%3] = rotation[i];
      (*intrinsic_matrix)[i/3][i%3] = intrinsic[i];
   }
   for (int i = 0; i < 3; i++) {
      (*translation_vector)[0][i] = t[i];
   }
   projectPoints(
      object_points,
      rotation_vector,
      translation_vector,
      intrinsic_matrix,
      0,
      image_points
   );
}

这根本无法编译。要projectPoints的参数有什么问题?

我找到的文档给出了以下projectPoints声明:

void projectPoints(const Mat& objectPoints, const Mat& rvec, const Mat& tvec, const Mat& cameraMatrix, const Mat& distCoeffs, vector<Point2f>& imagePoints);
void projectPoints(const Mat& objectPoints, const Mat& rvec, const Mat& tvec, const Mat& cameraMatrix, const Mat& distCoeffs, vector<Point2f>& imagePoints, Mat& dpdrot, Mat& dpdt, Mat& dpdf, Mat& dpdc, Mat& dpddist, double aspectRatio=0);

在所有情况下,您传递的是指向这些对象的指针,而不是对象本身。

除了为什么在这里使用动态分配的问题——几乎可以肯定没有必要,而且你可能有内存泄漏——你需要在将任何东西传递给projectPoints之前取消引用指针:

projectPoints(
   *object_points,
   *rotation_vector,
   *translation_vector,
   *intrinsic_matrix,
   0,
   *image_points
);

然后,您需要找到要为 distCoeffs 参数传递的内容(也许是一个空的 Mat 对象?),因为0不是const Mat&

希望有帮助。