bluechill
bluechill

Reputation: 437

cv::solvePnPRansac returns identity matrix

I want to find the object pose given 3D-to-2D correspondences using cv::solvePnPRansac. My correspondences might have outliers, so I don't want to use cv::solvePnP. I have used 3D-to-2D correspondences of a snapshot taken from a camera in opengl. So after backprojecting, I find that the rotation matrix returned after using solvePnP is correct. But, when I use solvePnPRansac, I get a identity matrix, and zero translation vector. Please help! I have used the following code. Thanks in advance !

// Parameters to solvePnP
Mat camera_mat(3, 3, CV_64FC1);
Mat distCoeffs(4, 1, CV_64FC1);
Mat rvec(3, 1, CV_64FC1);
Mat tvec(3, 1, CV_64FC1);
Mat d(3, 3, CV_64FC1);

vector<Point3f> list_points3d_model_match;
vector<Point2f> list_points2d_scene_match;

while(map >> u >> v >> x >> y >> z)
{
    Point2f ip = Point2f(u, v);
    Point3f sp = Point3f(x, y, z);

    // cout << x << " " << y << " " << z << endl;
    list_points3d_model_match.push_back(sp);
    list_points2d_scene_match.push_back(ip);
}

camera_mat.at<double>(0, 0) = 600;
camera_mat.at<double>(1, 1) = 600;
camera_mat.at<double>(0, 2) = WIDTH / 2;
camera_mat.at<double>(1, 0) = HEIGHT / 2;
camera_mat.at<double>(3, 3) = 1;

// solvePnP( list_points3d_model_match, list_points2d_scene_match, camera_mat, distCoeffs, rvec, tvec,
   //                 useExtrinsicGuess,  CV_ITERATIVE);

tvec.at<float>(0) = 0;
tvec.at<float>(1) = 0;
tvec.at<float>(2) = 2;

d.at<float>(0, 0) = 1;
d.at<float>(1, 2) = -1;
d.at<float>(2, 1) = 1;

Rodrigues(d, rvec);

solvePnPRansac(list_points3d_model_match, list_points2d_scene_match, camera_mat, distCoeffs, rvec, tvec, false, CV_ITERATIVE);

Rodrigues(rvec, d);

double* _r = d.ptr<double>();
printf("rotation mat: \n %.3f %.3f %.3f\n%.3f %.3f %.3f\n%.3f %.3f %.3f\n",
    _r[0],_r[1],_r[2],_r[3],_r[4],_r[5],_r[6],_r[7],_r[8]);
}

cout << "translation vec:\n" << tvec << endl;

I get the following output after running the code.

rotation mat: 
1.000 0.000 0.000
0.000 1.000 0.000
0.000 0.000 1.000

translation vec:
[0; 0; 0]

Upvotes: 0

Views: 2879

Answers (2)

user5032372
user5032372

Reputation: 21

I had the same problem before, and it's not about the wrong typing of the camera matrix.
Change the declaration of

Mat camera_mat(3, 3, CV_64FC1);
Mat distCoeffs(4, 1, CV_64FC1);

to CV_32FC1 and don't give the initial size to Mat tvec and Mat d.

Upvotes: 2

Daniel Moodie
Daniel Moodie

Reputation: 367

Your camera matrix is wrong. Your code should read:

camera_mat.at<double>(0, 0) = 600;
camera_mat.at<double>(1, 1) = 600;
camera_mat.at<double>(0, 2) = WIDTH / 2;
camera_mat.at<double>(1, 2) = HEIGHT / 2;
camera_mat.at<double>(3, 3) = 1;

Upvotes: 3

Related Questions