0

我想使用 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]
4

2 回答 2

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;
于 2015-03-10T14:25:42.253 回答
2

我之前也遇到过同样的问题,不是相机矩阵输入错误。
更改声明

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

toCV_32FC1 并且不给 and 初始Mat tvec大小Mat d

于 2015-06-21T01:44:17.057 回答