cv :: solvePnPRansac返回单位矩阵

时间:2015-02-23 21:49:57

标签: opencv camera-calibration extrinsic-parameters

我想使用cv :: solvePnPRansac找到给定3D到2D对应关系的对象姿势。我的通信可能有异常值,所以我不想使用cv :: solvePnP。我使用了从opengl中的摄像头拍摄的快照的3D到2D对应关系。因此,在反投影后,我发现使用solvePnP后返回的旋转矩阵是正确的。但是,当我使用solvePnPRansac时,我得到一个单位矩阵和零转换向量。请帮忙!我使用了以下代码。提前谢谢!

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

运行代码后,我得到以下输出。

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]

2 个答案:

答案 0 :(得分:3)

您的相机矩阵错误。 您的代码应为:

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;

答案 1 :(得分:2)

之前我遇到过同样的问题,并不是因为相机矩阵输入错误 更改

的声明
Mat camera_mat(3, 3, CV_64FC1);
Mat distCoeffs(4, 1, CV_64FC1);

CV_32FC1 并且不要将初始尺寸设为Mat tvecMat d