首页 > 解决方案 > openCV triangulatePoints在python中表现得很奇怪

问题描述

我正在尝试使用 OpenCV 计算 3D 点以进行多视图重建。我使用 SIFT 执行查找匹配点的标准序列,然后使用已知的相机校准矩阵获取基本矩阵和基本矩阵。在恢复第二个凸轮相对于第一个凸轮的姿势后,我继续尝试对这些点进行三角测量。代码的所有其他部分都运行良好且符合预期。但这一部分会出现故障。我正在使用 OpenCV 4.3.0。有时 triangulatePoints 只是破坏了 IDE (Spyder),有时它给了我分数,有时它给了我一堆 [1,1,1] 的分数。如果点数超过 200,IDE 也会中断。点数越多,似乎得到的故障就越多。这令人沮丧,任何帮助将不胜感激。

这是代码片段。

F, mask = cv.findFundamentalMat(pts1,pts2,cv.FM_LMEDS)
# We select only inlier points
pts1 = pts1[mask.ravel()==1]
pts2 = pts2[mask.ravel()==1]

print(len(pts1),len(pts2))
print(cv.__version__)

E, mask_2 = cv.findEssentialMat(pts1, pts2, focal=f_x, pp=(O_x, O_y), method=cv.FM_LMEDS, prob=0.999, threshold=3.0)
print("Essential Matrix")
print(E)
print(" ")
points, R_1, t_1, mask_2 = cv.recoverPose(E, pts1, pts2,pts2,focal=f_x, pp=(O_x, O_y), mask = mask_2)
print("Rotation Matrix")
print(R_1)
print(" ")
R_M = R.from_matrix(R_1)
R_1_E = R_M.as_euler('zyx', degrees=True)
print("angles (z,y,x) or (alpha, beta, gamma) Z is dir of Principal Ray, Y is Vert and X is horiz")
print(R_1_E)
print("Translation")
print(t_1)

K = np.array([[f_x, 0,O_x],
             [0,f_x, O_y],
             [0,0,1]])

Pr_1 = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0]])
Pr_2 = np.hstack((np.dot(K,R_1),np.dot(K,t_1)))
#Pr_2 = np.hstack((R_1,t_1))

pts1_t = pts1[:200].T
pts2_t = pts2[:200].T
#print(pts1_t)

points4D = cv.triangulatePoints(Pr_1,Pr_2,pts1_t, pts2_t)
#print(points4D.T[:3].T)
coordinate_eucl= cv.convertPointsFromHomogeneous(points4D.T)
coordinate_eucl=coordinate_eucl.reshape(-1,3)
px,py,pz=coordinate_eucl.T
coordP = []
for i in range(len(px)):
    coordP.append([px[i],py[i],pz[i]])
print(coordP[:20])

标签: pythonopencvcomputer-vision

解决方案


OpenCV Python 中的三角点函数不能很好地工作。

我在这里使用了 Eliasvan 创建的函数: https ://github.com/Eliasvan/Multiple-Quadrotor-SLAM/blob/master/Work/python_libs/triangulation.py

我使用了 Linear_LS_Triangulation 函数,对我来说效果很好,而且速度很快。


推荐阅读