【发布时间】: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