在 Python 2.7 和 OpenCV 3.3 中使用 triangulatePoints 时,输出 3D 点每次都会改变

Nas*_*ill 3 python opencv stereo-3d python-2.7

我想知道triangulatePoints在 Python 2.7 和 OpenCV 3.3 中使用的立体相机的 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
Run Code Online (Sandbox Code Playgroud)

但是当我运行这段代码时,我的结果发生了变化(所有结果都是错误的)。一次结果看起来像

[[  518845863]
 [ 1667138857]
 [-1189385102]
 [    -661713]]
Run Code Online (Sandbox Code Playgroud)

另一次结果看起来像

[[-1766436066]
 [          0]
 [          0]
 [-1299735447]]
Run Code Online (Sandbox Code Playgroud)

有时看起来像

[[        0]
 [        0]
 [697559541]
 [        0]]
Run Code Online (Sandbox Code Playgroud)

我不知道为什么即使我的所有参数都相同,结果却在改变?此外,这些 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

Nas*_*ill 5

最后,经过这么多次尝试,我发现了我在做什么错误。实际上,我在代码中以错误的方式定义了我的观点。根据triangulatePoints 文件,点应该是

projPoints1 – 第一张图像中的 2xN 特征点阵列。

但在我写的代码中

ptl = np.array([304,277]) # point in left image
ptr = np.array([255,277]) # Corresponding point in right image
Run Code Online (Sandbox Code Playgroud)

意味着我正在定义1x2数组,而我应该2x1为单点定义数组。同样对于 5 点数组应该是2x5. 对于N点offcourse 2xN。最初,我没有注意到这一点,因为我曾经在 Matlab 中进行训练,并且将相应的点用作 Nx2 数组。现在我把我的点像

l = np.array([[ 304],[ 277]],dtype=np.float)
r = np.array([[ 255 ],[ 277]],dtype=np.float)
Run Code Online (Sandbox Code Playgroud)

我得到了上面的代码工作。

dtype=np.float在定义此点数组以避免错误结果时,还有一点很重要。

结果我得到的结果不是很准确,显示的误差接近 20-25 毫米,但我解决了上述问题,所以我回答了这个问题,现在我必须找出最小化误差的方法。如果有人知道如何减少错误,请告诉我。