gpt4 book ai didi

python - 在 Python 2.7 和 OpenCV 3.3 中使用 triangulatePoints 每次输出 3D 点都会发生变化

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:04 27 4
gpt4 key购买 nike

我想在 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

最佳答案

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

projPoints1 – 2xN array of feature points in the first image.

但是在我写的代码中

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

关于python - 在 Python 2.7 和 OpenCV 3.3 中使用 triangulatePoints 每次输出 3D 点都会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46163831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com