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

时间:2017-10-02 11:53:16

标签: opencv 3d projection camera-calibration

为什么我的代码段会给出奇怪的预测结果?

//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)

如果我自己在纸上计算,我希望得到一个点point_2d =(150,150),其公式为:enter image description here

1 个答案:

答案 0 :(得分:0)

添加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

更改后,它可以在我的计算机上运行。