给定两个两个任意(即不平行)图像(例如,通过SURF找到)的对应点集,我已尝试使用以下方法来提取这些点的3D位置。
def triangulate(pts1,pts2):
cameraMatrix = np.array([[1, 0,0],[0,1,0],[0,0,1]])
F,m1 = cv2.findFundamentalMat(pts1, pts2) # apparently not necessary
# using the essential matrix can get you the rotation/translation bet. cameras, although there are two possible rotations:
E,m2 = cv2.findEssentialMat(pts1, pts2, cameraMatrix, cv2.RANSAC, 0.999, 1.0)
Re1, Re2, t_E = cv2.decomposeEssentialMat(E)
# recoverPose gets you an unambiguous R and t. One of the R's above does agree with the R determined here. RecoverPose can already triangulate, I check by hand below to compare results.
K_l = cameraMatrix
K_r = cameraMatrix
retval, R, t, mask2, triangulatedPoints = cv2.recoverPose(E,pts_l_norm, pts_r_norm, cameraMatrix,distanceThresh=0.5)
# given R,t you can explicitly find 3d locations using projection
M_r = np.concatenate((R,t),axis=1)
M_l = np.concatenate((np.eye(3,3),np.zeros((3,1))),axis=1)
proj_r = np.dot(cameraMatrix,M_r)
proj_l = np.dot(cameraMatrix,M_l)
points_4d_hom = cv2.triangulatePoints(proj_l, proj_r, np.expand_dims(pts1, axis=1), np.expand_dims(pts2, axis=1))
points_4d = points_4d_hom / np.tile(point_s4d_hom[-1, :], (4, 1))
points_3d = points_4d[:3, :].T
return points_3d
我已经假设我的固有相机矩阵在上面近似于I。由两种方法(findEssentialMat-> decomposeEssentialMat与recoverPose)确定的R,t一致,由两种方法(recoverPose与triangulatePoints)确定的三角点也一致。我的问题与我看到的值有关,points_3d的值通常在x,y的范围为0-50,对于z的范围为0-0.03。据我所知,这些值应该以像素为单位;我选择的相机矩阵=我影响了比例吗?
您应该使用经过校准的相机。您可以在OpenCV中查找相机校准教程,它为您提供了代码并说明了如何校准相机。
校准将为您提供用于获取与triangulatePoints一起使用的未失真SURF关键点(又称特征)所必须使用的相机矩阵和失真系数。
通常,所计算的摄像机平移是一个单位矢量,因此您的3D点将使用该位移作为比例单位。假设您的两个图像是用相距1米的相机拍摄的,则3D点将以米为单位。 3D比例尺永远不会以像素为单位。
最后评论:
1-以这种方式对三角剖分不是那么准确。精确度是通过多视图或slam方法实现的,它们都不是简单的。并且总是与经过校准的相机一起使用。
2-如果您不校准相机,则可以假定您的相机矩阵近似(糟糕的结果,但总比没有好),前提是:-cx和cy在图像的中间-fx = fy =半水平分辨率(例如1920/2像素),前提是您的相机的光圈为90度。否则,您必须绘制一个等腰三角形并进行一些几何处理。