我在世界坐标中有一组3d点,并且在图像中有2d点的相应对应关系。我想找到一个矩阵,它可以让我在这些点之间进行转换。我怎么能在OpenCV中做到这一点?
答案 0 :(得分:2)
cv::solvePnP()
正是您正在寻找的,它会从3D-2D点对应中找到一个物体姿势并产生一个旋转矢量(rvec
),它与翻译矢量({{1})一起}),将模型坐标系中的点带到摄像机坐标系。
答案 1 :(得分:0)
您可以使用solvePnP:
// camMatrix based on img size
int max_d = std::max(img.rows,img.cols);
Mat camMatrix = (Mat_<double>(3,3) <<
max_d, 0, img.cols/2.0,
0, max_d, img.rows/2.0,
0, 0, 1.0);
// 2d -> 3d correspondence
vector<Point2d> pts2d = ...
vector<Point3d> pts3d = ...
Mat rvec,tvec;
solvePnP(pts3d, pts2d, camMatrix, Mat(1,4,CV_64F,0.0), rvec, tvec, false, SOLVEPNP_EPNP);
// get 3d rot mat
Mat rotM(3, 3, CV_64F);
Rodrigues(rvec, rotM);
// push tvec to transposed Mat
Mat rotMT = rotM.t();
rotMT.push_back(tvec.reshape(1, 1));
// transpose back, and multiply
return camMatrix * rotMT.t();