triangulatePoints() 方法的奇怪行为

Posted

技术标签:

【中文标题】triangulatePoints() 方法的奇怪行为【英文标题】:Strange behaivor of triangulatePoints() method 【发布时间】:2019-08-01 11:03:21 【问题描述】:

我正在尝试使用 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】:

我找到了答案:有必要在图像点初始化中指定 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  ]

【讨论】:

以上是关于triangulatePoints() 方法的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV triangulatePoints 惯用手

OpenCV undistortPoints 和 triangulatePoint 给出奇怪的结果(立体声)

OpenCV-Triangulation

在spark中遇到了奇怪的错误,找到了奇怪的解决方法

不允许使用 Django 奇怪的方法

UITableView 方法“indexPathForRowAtPoint:”的奇怪行为