确定相机姿势

Determine camera pose?

本文关键字:相机      更新时间:2023-10-16

我正试图根据场景中的基准标记来确定相机姿势。

受托人:http://tinypic.com/view.php?pic=4r6k3q&s=8#.VNLWTVVK1E

当前流程:

  1. 使用SIFT进行特征检测
  2. 使用SIFT提取描述符
  3. 使用FLANN进行匹配
  4. 使用CV_RANSAC查找单应性
  5. 识别基准的角
  6. 使用透视变换()识别场景中基准点的角点
  7. 在角落周围画线(即证明它在场景中找到了基准
  8. 运行摄像机校准
  9. 负载校准结果(cameraMatrix和distortionCoefficients)

现在我正试着弄清楚相机的姿势。我尝试使用:

void solvePnP(const Mat&objectPoints,const Matamp;imagePoints,常量Mat&cameraMatrix,const Mat&distCoeffs,垫子&rvec,Mat&tvec,bool useExtrinicGuess=false)

其中:

  • obectPoints是基准角
  • imagePoint是场景中的基准角
  • cameraMatrix来自校准
  • distCoeffs来自校准
  • rvec和tvec应该从这个函数返回给我

然而,当我运行这个程序时,我会得到一个核心转储错误,所以我不确定我做错了什么。

我还没有找到关于solvePNP()的很好的文档-我误解了函数或输入参数吗?

感谢您的帮助

更新 这是我的流程:

OrbFeatureDetector detector; //Orb seems more accurate than SIFT
vector<KeyPoint> keypoints1, keypoints2; 
detector.detect(marker_im, keypoints1);
detector.detect(scene_im, keypoints2);
Mat display_marker_im, display_scene_im;
drawKeypoints(marker_im, keypoints1, display_marker_im, Scalar(0,0,255));
drawKeypoints(scene_im, keypoints2, display_scene_im, Scalar(0,0,255));
SiftDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute( marker_im, keypoints1, descriptors1 );
extractor.compute( scene_im, keypoints2, descriptors2 );
BFMatcher matcher; //BF seems to match better than FLANN
vector< DMatch > matches;
matcher.match( descriptors1, descriptors2, matches );
Mat img_matches;
drawMatches( marker_im, keypoints1, scene_im, keypoints2,
    matches, img_matches, Scalar::all(-1), Scalar::all(-1),
    vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
vector<Point2f> obj, scene;
for (int i = 0; i < matches.size(); i++) {
    obj.push_back(keypoints1[matches[i].queryIdx].pt);
    scene.push_back(keypoints2[matches[i].trainIdx].pt);
}
Mat H;
H = findHomography(obj, scene, CV_RANSAC);
//Get corners of fiducial
vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0);
obj_corners[1] = cvPoint(marker_im.cols, 0);
obj_corners[2] = cvPoint(marker_im.cols, marker_im.rows);
obj_corners[3] = cvPoint(0, marker_im.rows);
vector<Point2f> scene_corners(4);
perspectiveTransform(obj_corners, scene_corners, H);
FileStorage fs2("cal.xml", FileStorage::READ);
Mat cameraMatrix, distCoeffs;
fs2["Camera_Matrix"] >> cameraMatrix;
fs2["Distortion_Coefficients"] >> distCoeffs;
Mat rvec, tvec;
//same points as object_corners, just adding z-axis (0)
vector<Point3f> objp(4);
objp[0] = cvPoint3D32f(0,0,0);
objp[1] = cvPoint3D32f(gray.cols, 0, 0);
objp[2] = cvPoint3D32f(gray.cols, gray.rows, 0);
objp[3] = cvPoint3D32f(0, gray.rows, 0);
solvePnPRansac(objp, scene_corners, cameraMatrix, distCoeffs, rvec, tvec );
Mat rotation, viewMatrix(4, 4, CV_64F);
Rodrigues(rvec, rotation);
for(int row=0; row<3; ++row)
{
   for(int col=0; col<3; ++col)
   {
      viewMatrix.at<double>(row, col) = rotation.at<double>(row, col);
   }
   viewMatrix.at<double>(row, 3) = tvec.at<double>(row, 0);
}
viewMatrix.at<double>(3, 3) = 1.0f;
cout << "rotation: " << rotation << endl;
cout << "viewMatrix: " << viewMatrix << endl;

好的,solvePnP()给你从模型的帧(即立方体)到相机的帧的传递矩阵(称为视图矩阵)。

输入参数:

  • objectPoints–对象坐标空间中的对象点阵列,3xN/Nx3 1通道或1xN/Nx1 3通道,其中N是点的数量。CCD_ 3也可以在这里传递。这些点是3D的,但由于它们在(基准标记的)图案坐标系中,因此装备是平面的,使得每个输入对象点的Z坐标为0
  • imagePoints–对应图像点的阵列,2xN/Nx2 1通道或1xN/Nx1 2通道,其中N为点数。CCD_ 5也可以通过这里
  • intrinsics:摄像机矩阵(焦距、主点)
  • distortion:失真系数,如果为空则假设为零失真系数
  • rvec:输出旋转矢量
  • tvec:输出平移矢量

视图矩阵的构建是这样的:

cv::Mat rvec, tvec;
cv::solvePnP(objectPoints, imagePoints, intrinsics, distortion, rvec, tvec);
cv::Mat rotation, viewMatrix(4, 4, CV_64F);
cv::Rodrigues(rvec, rotation);
for(int row=0; row<3; ++row)
{
   for(int col=0; col<3; ++col)
   {
      viewMatrix.at<double>(row, col) = rotation.at<double>(row, col);
   }
   viewMatrix.at<double>(row, 3) = tvec.at<double>(row, 0);
}
viewMatrix.at<double>(3, 3) = 1.0f;

此外,你能分享你的代码和错误信息吗?