在使用Python 2.7和OpenCV 3.3中的triangulatePoints函数时,输出的3D点每次都会发生变化。

3

我想使用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”运行?

你确定图片已经被加载了吗?通常在Python(以及很多其他语言)中,\是一个转义字符,所以它将\p这样的特殊字符作为输入...应该是\,而且地址中不要混用/和\。 - api55
谢谢,@api55先生,我刚刚解决了这个问题。但是我的输出点有错误。您能否给我一些建议,如何减少错误? - Naseeb Gill
1个回答

5

经过多次尝试,我终于找到了我的错误。其实,在代码中,我定义我的点的方式是错误的。根据三角测量点文档,点应该是:

projPoints1-第一张图像中的特征点的2xN数组。

但在代码中,我写成了:

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

意思是我定义的是1x2数组,但实际上应该定义单点的2x1数组。同样地,对于5个点的数组应该是2x5。对于N个点,当然应该是2xN。最初我没有注意到这个问题,因为我在Matlab中做三角化时使用的是相应点作为Nx2数组。现在我把我的点放在了……

l = np.array([[ 304],[ 277]],dtype=np.float)
r = np.array([[ 255 ],[ 277]],dtype=np.float)

我已经使上述代码工作起来。

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接