【问题标题】:Output 3D points change everytime in triangulatePoints using in Python 2.7 with OpenCV 3.3在 Python 2.7 和 OpenCV 3.3 中,输出 3D 点每次在 triangulatePoints 中发生变化
【发布时间】:2018-02-20 04:12:26
【问题描述】:

我想在 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 先生,我刚刚解决了这个问题。但是我在输出点上遇到错误。你能给我一些建议吗,如何减少错误?

标签: python python-2.7 opencv stereo-3d


【解决方案1】:

最后,经过这么多尝试,我发现自己犯了什么错误。实际上,我以错误的方式在代码中定义了我的观点。根据triangulatePoints document,积分应该是

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 毫米,但我解决了上述问题,所以我回答了这个问题,现在我必须找出最小化错误的方法。如果有人知道如何减少错误,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2014-04-29
    • 2014-02-14
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多