gpt4 book ai didi

OpenCV:为什么 projectPoints() 给我奇怪的结果?

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:39 27 4
gpt4 key购买 nike

为什么我的代码片段给我的投影点结果很奇怪?

//Generate the one 3D Point which i want to project onto 2D plane
vector<Point3d> points_3d;
points_3d.push_back(Point3d(10, 10, 100));
Mat points3d = Mat(points_3d);

//Generate the identity matrix and zero vector for rotation matrix and translation vector
Mat rvec = (Mat_<double>(3, 3) << (1, 0, 0, 0, 1, 0, 0, 0, 1));
Mat tvec = (Mat_<double>(3, 1) << (0, 0, 0));

//Generate a camera intrinsic matrix
Mat K = (Mat_<double>(3,3)
<< (1000, 0, 50,
0, 1000, 50,
0, 0, 1));

//Project the 3D Point onto 2D plane
Mat points_2d;

projectPoints(points_3d, rvec, tvec, K, Mat(), points_2d);

//Output
cout << points_2d;

我得到了投影的 2D 点
points_2d = (-1.708699427820658e+024, -9.673395654445999e-026)

如果我自己在纸上计算它,我期望点 points_2d = (150, 150) 使用该公式:enter image description here

最佳答案

添加 cv::Rodrigues(InputArray src, OutputArray dst, OutputArray jacobian=noArray())。 OpenCv 在计算中使用旋转向量而不是旋转矩阵。 Rodrigues 变换允许您将旋转矢量转换为矩阵以及将矩阵转换为矢量。下面我附上了你的部分代码,添加了一行。

//Generate the identity matrix and zero vector for rotation matrix and translation vector
Mat rvec,rMat = (Mat_<double>(3, 3) << (1, 0, 0, 0, 1, 0, 0, 0, 1));
Rodrigues(rMat,rvec); //here
Mat tvec = (Mat_<double>(3, 1) << (0, 0, 0));

它应该可以正常工作。最好将失真系数定义为

 Mat dist = Mat::zeros(8,1,CV_32f);

编辑:

再说一句,你在矩阵初始化中几乎没有语法错误:

cv::Mat rvec,rMat = (cv::Mat_<double>(3, 3) << /* ( */1, 0, 0, 0, 1, 0, 0, 0, 1); //you had error here
cv::Rodrigues(rMat, rvec);
cv::Mat tvec = (cv::Mat_<double>(3, 1) <</* ( */ 0, 0, 0); //and here

修改后它可以在我的电脑上运行。

关于OpenCV:为什么 projectPoints() 给我奇怪的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46524955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com