triangulatePoints()方法的奇怪行为

时间:2019-08-01 11:03:21

标签: python opencv computer-vision triangulation

我正在尝试使用OpenCV中的triangulatePoints()方法来重建3d对象

我为两个摄像机生成了投影矩阵,它们看起来很正常。 但是三角剖分方法针对不同的输入2D坐标返回相同的3d值。有什么问题吗?

#code
print('Projection matrices:')
print(P0,'\n',P1, '\n')


points = np.array([
    [[381,198],[433,418]],
    [[393,231],[435,453]],
    [[415,225],[465,454]],
    [[406,195],[169,420]]
])


for p in points:
    print('2D coordinates: {}, {}'.format(p[0], p[1]))
    s = np.array(cv2.triangulatePoints(P0, P1, 
                              p[0], 
                              p[1])).T


    print('3D coordinates: {}'.format(s[0][:-1]/np.max(s[0][-1])))
    print()


#Output:
#Projection matrices:
[[587.42947475   0.         223.06652927   0.        ]
 [  0.         587.42947475 236.78123179   0.        ]
 [  0.           0.           1.           0.        ]] 
 [[ 5.87429475e+02  0.00000000e+00  2.23066529e+02 -1.09198390e+04]
 [ 0.00000000e+00  5.87429475e+02  2.36781232e+02  0.00000000e+00]
 [ 0.00000000e+00  0.00000000e+00  1.00000000e+00  0.00000000e+00]] 

2D coordinates: [381 198], [433 418]
3D coordinates: [-1.00049629  0.          0.        ]

2D coordinates: [393 231], [435 453]
3D coordinates: [-1.00049629  0.          0.        ]

2D coordinates: [415 225], [465 454]
3D coordinates: [-1.00049629  0.          0.        ]

2D coordinates: [406 195], [169 420]
3D coordinates: [-1.00049629  0.          0.        ]

1 个答案:

答案 0 :(得分:1)

我找到了答案:有必要在图像点初始化中将dtype指定为np.float32:


points = np.array([
    [[381,198],[433,418]],
    [[393,231],[435,453]],
    [[415,225],[465,454]],
    [[406,195],[169,420]]
], dtype=np.float32)

此输出正确后:

2D coordinates: [381. 198.], [433. 418.]
3D coordinates: [ -76.17712   -33.213867 -265.16254 ]

2D coordinates: [393. 231.], [435. 453.]
3D coordinates: [-110.72584  -66.3086  -358.42117]

2D coordinates: [415. 225.], [465. 454.]
3D coordinates: [-105.021255  -54.279873 -300.12518 ]

2D coordinates: [406. 195.], [169. 420.]
3D coordinates: [14.564291   5.4381375 43.69199  ]