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

发布于 2024-11-05 20:43:45 字数 1422 浏览 0 评论 0原文

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

I'm trying to convert simple code for cvProjectPoints2 to C++, so I'm using cv::ProjectPoints. I'm using the cv namespace to avoid prefixing everything with 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
   );
}

This simply will not compile. What is wrong with the parameters to projectPoints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-11-12 20:43:45

我发现的文档给出了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& >。

希望有帮助。

The documentation I've found gives the following declarations for 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);

In all cases, you're passing pointers to these objects, not the objects themselves.

Questions aside as to why you're using dynamic allocation here — it's almost certainly not necessary, and you probably have memory leaks — you need to dereference the pointers before passing anything to projectPoints:

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

You then need to find something to pass for the distCoeffs parameter (perhaps an empty Mat object?) because 0 is not a const Mat&.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文