OpenCV: Why projectPoints() is giving me weird results?

Viewed 2504

Why is my code snippet giving me weird results for projected points?

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

I get as projected 2D Point
points_2d = (-1.708699427820658e+024, -9.673395654445999e-026)

If i calculate it on a paper on my own, i'm expecting a point points_2d = (150, 150) with that formula: enter image description here

1 Answers

Add cv::Rodrigues(InputArray src, OutputArray dst, OutputArray jacobian=noArray()). OpenCv uses rotation vector inside calculation instead of rotation matrix. Rodrigues transformation allows you to convert rotation vector to matrix and matrix to vector. Below i attached part of your code with one line added.

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

And it should work properly. It also will be better to define distortion coefficents as

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

EDIT:

One more remark, you have little syntax error in matrix initialization:

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

It works on my computer after changes.

Related