我想使用Python 2.7和OpenCV 3.3中的triangulatePoints
从立体相机中得到3D点。为此,我已经对立体相机进行了校准,并将矩阵存储在文件夹中。我还使用cv2.stereoRectify
矫正了我的图像,并通过使用cv2.initUndistortRectifyMap
去畸变图像。然后我保存了这些图像以及投影矩阵P1和P2,并在两个图像中寻找相应的点。左图像中的点是ptl = np.array([304,277])
,右图像中的对应点是ptr = np.array([255,277])
。之后,我尝试了points = cv2.triangulatePoints(P1,P2,ptl,ptr)
。代码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt
cameraMatrixL = np.load('mtx_left.npy')
distCoeffsL = np.load('dist_left.npy')
cameraMatrixR = np.load('mtx_right.npy')
distCoeffsR = np.load('dist_right.npy')
R = np.load('R.npy')
T = np.load('T.npy')
# following matrices I saved which i got from stereoRectify
R1 = np.load('R1.npy')
R2 = np.load('R2.npy')
P1 = np.load('P1.npy')
P2 = np.load('P2.npy')
Q = np.load('Q.npy')
# upload alreday distorted and rectified images
imgL = cv2.imread('D:\python/triangulate in 3 D\left.png',0)
imgR = cv2.imread('D:\python/triangulate in 3 D/right.png',0)
ptl = np.array([304,277]) # point in left image
ptr = np.array([255,277]) # Corresponding point in right image
points = cv2.triangulatePoints(P1,P2,ptl,ptr)
print points
但是每次我运行这段代码时,我的结果都改变了(而且所有的结果都是错误的)。有一次结果看起来像:
[[ 518845863]
[ 1667138857]
[-1189385102]
[ -661713]]
另一次结果看起来像这样:
[[-1766436066]
[ 0]
[ 0]
[-1299735447]]
有时候看起来像是:
[[ 0]
[ 0]
[697559541]
[ 0]]
我不知道为什么即使所有参数都相同,结果也会改变?此外,这些3D点是不正确的。如何解决这些问题?
编辑:我在这段代码中观察到一件事情,在运行后它没有完成。它既不显示“Process finished with exit code 0”,也不显示“Process finished with exit code 1”。当我按下红色停止按钮时,它以“Process finished with exit code 1”结束。为什么?我认为正是由于这个原因才出现了上述错误。为什么这段代码不能以“Process finished with exit code 0”运行?