【问题标题】:use cv2.solvePnP from homography result to get camera pose使用来自单应性结果的 cv2.solvePnP 获取相机位姿
【发布时间】:2016-10-08 01:00:59
【问题描述】:

我使用以下代码对图像进行地理配准

有输入

grid    = "for example a utm grid"
img_raw = cv2.imread(filename)
mtx, dist = "intrinsic camera matrix and 
             distortion coefficient from calibration matrix"
src_pts = "camera location of gcp on undistorted image"
dst_pts = "world location of gcp in the grid coordinate"

我校正相机失真并应用单应性

img = cv2.undistort(img_raw, mtx, dist, None, None)

H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
img_geo = cv2.warpPerspective(img,(grid.shape[0],grid.shape[1]),\
                              flags=cv2.INTER_NEAREST,borderValue=0)

然后我想获取相机的位置。我尝试使用在 cv2.solvePnP 中计算的旋转和平移矩阵,例如 here 所示。如果我是对的,我需要至少一组 4 个共面点的相机和世界坐标。

flag, rvec, tvec = cv2.solvePnP(world, cam, mtx, dist)

再次,如果我是对的,在solvePnP中,相机坐标需要来自原始图像帧,而不是像src_pts中的未失真帧。

所以我的问题是,我怎样才能在原始图像帧中获得 src_pts 的像素位置?或者有没有其他方法可以获取 rvec 和 tvec?

【问题讨论】:

    标签: opencv


    【解决方案1】:

    【讨论】:

    • coord_pts_in = cv2.undistortPoints(coord_pts_in_raw, K, D, P=K)
    • 谢谢,虽然projectPoints 需要输入 rvec 和 tvec,我试图从 solvePnP 获得。我在下面回复了我找到的解决方法。
    【解决方案2】:

    这是我找到的解决方案

    grid    = "for example a utm grid"
    img_raw = cv2.imread(filename)
    mtx, dist = "intrinsic camera matrix and 
             distortion coefficient from calibration matrix"
    src_pts = "camera location of gcp on raw image"
    dst_pts = "world location of gcp in the grid coordinate"
    

    注意src_pts 现在是原始扭曲图像中的点

    src_pts_undistorted = cv2.undistortPoints(src_pts, K, D, P=K)
    img = cv2.undistort(img_raw, mtx, dist, None, None)
    
    H, mask = cv2.findHomography(src_pts_undistorted, dst_pts, cv2.RANSAC,5.0)
    img_geo = cv2.warpPerspective(img,(grid.shape[0],grid.shape[1]),\
                              flags=cv2.INTER_NEAREST,borderValue=0)
    

    然后我可以从solvePnP得到姿势

    flag, rvec, tvec = cv2.solvePnP(dst_pts, src_pts, mtx, dist)
    

    【讨论】:

    • 第三个代码片段和第二个有什么关系?如我所见,solvePnP 的所有参数都是来自第一个代码片段的输入。
    猜你喜欢
    • 2017-11-27
    • 2017-10-15
    • 2019-12-17
    • 2013-11-05
    • 2015-05-01
    • 2013-09-09
    • 1970-01-01
    • 2020-05-26
    • 2017-05-30
    相关资源
    最近更新 更多