我有兴趣找到一个场景的视差图 . 首先,我使用以下代码进行了立体声校准(我自己写了一些来自Google的帮助,因为没有找到任何有用的教程,因为在Python中为OpenCV 2.4.10编写了相同的教程) .
我在两台摄像机上同时拍摄了棋盘图像,并将它们保存为左* .jpg和右* .jpg .
import numpy as np
import cv2
import glob
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpointsL = [] # 3d point in real world space
imgpointsL = [] # 2d points in image plane.
objpointsR = []
imgpointsR = []
images = glob.glob('left*.jpg')
for fname in images:
img = cv2.imread(fname)
grayL = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, cornersL = cv2.findChessboardCorners(grayL, (9,6),None)
# If found, add object points, image points (after refining them)
if ret == True:
objpointsL.append(objp)
cv2.cornerSubPix(grayL,cornersL,(11,11),(-1,-1),criteria)
imgpointsL.append(cornersL)
images = glob.glob('right*.jpg')
for fname in images:
img = cv2.imread(fname)
grayR = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, cornersR = cv2.findChessboardCorners(grayR, (9,6),None)
# If found, add object points, image points (after refining them)
if ret == True:
objpointsR.append(objp)
cv2.cornerSubPix(grayR,cornersR,(11,11),(-1,-1),criteria)
imgpointsR.append(cornersR)
retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (320,240))
如何纠正图像?在继续查找视差图之前,我还应该采取哪些其他步骤?我在某处读到,在计算视差图时,两帧上检测到的特征应位于同一水平线上 . 请帮帮我 . 任何帮助将非常感激 .
3 回答
你需要
cameraMatrix1
,distCoeffs1
,cameraMatrix2
,distCoeffs2
和"newCameraMatrix" for cv2.undistort()你可以使用cv2.getOptimalNewCameraMatrix()获得"newCameraMatrix"
所以在你的脚本的其余部分粘贴这个:
希望能让你走向可能正在计算“视差图”的下一件事;)
Reference:
http://www.janeriksolem.net/2014/05/how-to-calibrate-camera-with-opencv-and.html
试试这段代码,我已经能够解决这个错误:
首先,使用opencv校准应用程序或matlab calibration toolbox来计算相机参数 . 使用参数,您可以纠正您的图像 .
在整改之后,请参考opencv的代码库中的python示例(samples / python / stereo_match.py)来计算视差图 .