c++ 我可以在OpenCV中使用remap获取点位置吗

hpcdzsge  于 2024-01-09  发布在  其他
关注(0)|答案(4)|浏览(146)

我用RGB相机拍摄了一张照片A。我知道照片A中一个点g的位置。相机需要做相机校准。现在我想知道校准后点g的位置。我使用的代码如下,但我想得到点的位置,而不是图像。我怎么才能做到这一点?你能给予我一些建议吗?

  1. initUndistortRectifyMap(
  2. cameraMatrix,
  3. distCoeffs,
  4. Mat(),
  5. Mat(),
  6. Size(640, 480),
  7. CV_32FC1,
  8. map1, map2);
  9. remap(A, B, map1, map2, cv::INTER_LINEAR);
  10. Point2f g = Point2f(...,...);//i want to get the new position of the point not image B

字符串

pepwfjgg

pepwfjgg1#

使用Map获取坐标:
x,y -之后的坐标(不是之前),正如pasbi在评论中正确注意到的那样,Map。
(map 1(y,x),map 2(y,x))-Map前的坐标
换句话说:

  • map1.at<float>(y,x)包含每个目的地点p(x,y)的源x坐标。
  • map2.at<float>(y,x)包含每个目的地点p(x,y)的源y坐标。

请参阅有关remap函数的文档。

f4t66c6m

f4t66c6m2#

undistortPoints()是您的需求。

  1. // src_pts are points in raw(distort) img, rectify_pt_vec are in rectifyImageL
  2. // RL, PL are from stereoRectify()
  3. cv::undistortPoints(src_pts, rectify_pt_vec, cameraMatrixL, distCoeffL, RL, PL);

字符串
如何获得点srcimg从dstimg就像pasbi评论如下.

2jcobegt

2jcobegt3#

我发现最好的方法是重新创建一个相机矩阵,用反转的参数。在一定程度上工作,像基本的图像修改

af7jpaap

af7jpaap4#

如果您使用initUndistortRectifyMap中的map1map2来查找校准后点g的新位置,则需要直接使用Map来调整点。然而,如前所述,有一种更有效的方法可以使用undistortPoints函数来获取点g的新位置。但让我们先关注更新您的原始答案。
使用Map查找新位置的代码如下所示:

  1. // Original point
  2. cv::Point2f g_orig(x, y); // Replace x, y with the coordinates of g
  3. // Convert maps to the correct type if they are not already in CV_16SC2 format
  4. cv::Mat map1_fixed, map2_fixed;
  5. cv::convertMaps(map1, map2, map1_fixed, map2_fixed, CV_16SC2, false);
  6. // Now, you can use the maps to find the new position
  7. cv::Point g_new = map1_fixed.at<cv::Vec2s>(g_orig.y, g_orig.x);
  8. // g_new now contains the new position of point g

字符串
但是,我必须指出,上面代码中的最后一行可能无法按预期工作,因为map1包含定点坐标,它应该与remap一起使用以重新Map整个图像。如果您想要Map单个点,则需要手动插值,这并不简单。
因此,我建议使用undistortPoints,它是为此目的而设计的:

  1. // Original point's coordinates
  2. cv::Point2f g_orig(x, y);
  3. // Make a list containing just your point
  4. std::vector<cv::Point2f> distortedPoints = { g_orig };
  5. std::vector<cv::Point2f> undistortedPoints;
  6. // Undistort the point
  7. cv::undistortPoints(distortedPoints, undistortedPoints, cameraMatrix, distCoeffs, cv::noArray(), cameraMatrix);
  8. // Your new, undistorted point
  9. cv::Point2f g_undistorted = undistortedPoints[0];


现在,有了g_undistorted,你就有了点g的正确位置,而不必处理整个图像。这比试图使用Map矩阵来调整单个点的位置更不容易出错,也更有效。

展开查看全部

相关问题