我如何估计两个摄像机在OpenCV中的位置? [英] How do I estimate positions of two cameras in OpenCV?

查看:638
本文介绍了我如何估计两个摄像机在OpenCV中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从两个图像中获得了两组相应的点.我估计了编码相机之间转换的基本矩阵:

I have two sets of corresponding points from two images. I have estimated the Essential matrix which encodes the transformation between the cameras:

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

然后我提取了旋转和平移分量:

I've then extracted the rotation and translation components:

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

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

But how do I actually get the cameras matrices of the two cameras so I can use cv2.triangulatePoints to generate a little point cloud?

推荐答案

这就是我所做的:

输入:

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

这篇关于我如何估计两个摄像机在OpenCV中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆