将projectPoints从C转换为C ++

时间:2011-05-07 22:46:47

标签: c++ opencv

我正在尝试将cvProjectPoints2的简单代码转换为C ++,所以我使用的是cv :: ProjectPoints。我是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的参数有什么问题?

1 个答案:

答案 0 :(得分:0)

我发现的documentationprojectPoints提供了以下声明:

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&

希望有所帮助。