如何估算OpenCV中两台摄像机的位置?

时间:2015-11-24 23:52:23

标签: python opencv structure-from-motion

我有两组来自两个图像的对应点。我估计了编码摄像机之间转换的基本矩阵:

E, mask = cv2.findEssentialMat(points1, points2, 1.0)

然后我提取了旋转和翻译组件:

points, R, t, mask = cv2.recoverPose(E, points1, points2)

但是如何实际获取两个摄像头的摄像机矩阵,以便我可以使用cv2.triangulatePoints生成一个小点云?

1 个答案:

答案 0 :(得分:5)

这是我做的:

输入:

pts_l - set of n 2d points in left image. nx2 numpy float array
pts_r - set of n 2d points in right image. nx2 numpy float array

K_l - Left Camera matrix. 3x3 numpy float array
K_r - Right Camera matrix. 3x3 numpy float array

代码:

# Normalize for Esential Matrix calaculation
pts_l_norm = cv2.undistortPoints(np.expand_dims(pts_l, axis=1), cameraMatrix=K_l, distCoeffs=None)
pts_r_norm = cv2.undistortPoints(np.expand_dims(pts_r, axis=1), cameraMatrix=K_r, distCoeffs=None)

E, mask = cv2.findEssentialMat(pts_l_norm, pts_r_norm, focal=1.0, pp=(0., 0.), method=cv2.RANSAC, prob=0.999, threshold=3.0)
points, R, t, mask = cv2.recoverPose(E, pts_l_norm, pts_r_norm)

M_r = np.hstack((R, t))
M_l = np.hstack((np.eye(3, 3), np.zeros((3, 1))))

P_l = np.dot(K_l,  M_l)
P_r = np.dot(K_r,  M_r)
point_4d_hom = cv2.triangulatePoints(P_l, P_r, np.expand_dims(pts_l, axis=1), np.expand_dims(pts_r, axis=1))
point_4d = point_4d_hom / np.tile(point_4d_hom[-1, :], (4, 1))
point_3d = point_4d[:3, :].T

输出:

point_3d - nx3 numpy array