I’m posting my code and data set (a small one for now, having trouble getting both cameras working at once) below. It attempts to perform a stereo calibration followed by undistortion, where it runs into the error.
Data set: Stereo_Dataset - Album on Imgur
Code:
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((9*14,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:14].T.reshape(-1,2)
objp *= (16.2, 16.2, 0)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints1 = [] # 2d points in image plane.
imgpoints2 = [] # 2d points in image plane.
images1 = sorted(glob.glob('L*.png'))
images2 = sorted(glob.glob('R*.png'))
for fname1, fname2 in zip(images1, images2):
img1 = cv2.imread(fname1)
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
img2 = cv2.imread(fname2)
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret1, corners1 = cv2.findChessboardCorners(gray1, (9,14), cv2.CALIB_CB_FAST_CHECK | cv2.CALIB_CB_ADAPTIVE_THRESH)
ret2, corners2 = cv2.findChessboardCorners(gray2, (9,14), cv2.CALIB_CB_FAST_CHECK | cv2.CALIB_CB_ADAPTIVE_THRESH)
# If found, add object points, image points (after refining them)
if ret1 and ret2:
objpoints.append(objp)
corners1 = cv2.cornerSubPix(gray1,corners1,(11,11),(-1,-1),criteria)
corners2 = cv2.cornerSubPix(gray2,corners2,(11,11),(-1,-1),criteria)
imgpoints1.append(corners1)
imgpoints2.append(corners2)
cv2.destroyAllWindows()
ret1, mtx1, dist1, rvecs1, tvecs1 = cv2.calibrateCamera(objpoints, imgpoints1, gray1.shape[::-1], None, None)
ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera(objpoints, imgpoints2, gray2.shape[::-1], None, None)
h, w = gray1.shape[:2]
newcameramtx1, roi1 = cv2.getOptimalNewCameraMatrix(mtx1, dist1, (w, h), 1, (w, h))
newcameramtx2, roi2 = cv2.getOptimalNewCameraMatrix(mtx2, dist2, (w, h), 1, (w, h))
ret, _, _, _, _, rmtx, tvec, _, _ = cv2.stereoCalibrate(objpoints, imgpoints1, imgpoints2, mtx1, dist1, mtx2, dist2,
gray1.shape[::-1], None, None, None, None,
cv2.CALIB_FIX_INTRINSIC, criteria)
size = (h, w)
recL, recR, projL, projR, dispToDepthMap, leftROI, rightROI = cv2.stereoRectify(mtx1, dist1, mtx2, dist2, size, rmtx, tvec, None, None, None, None, None, cv2.CALIB_ZERO_DISPARITY)
leftMapX, leftMapY = cv2.initUndistortRectifyMap(mtx1, dist1, recL, projL, size, cv2.CV_32FC1)
rightMapX, rightMapY = cv2.initUndistortRectifyMap(mtx2, dist2, recR, projR, size, cv2.CV_32FC1)
img1 = cv2.imread('LImage0.png')
img2 = cv2.imread('RImage0.png')
dst = cv2.undistortPoints(img1, mtx1, dist1, recL, projL)
#dst = cv2.remap(img1, leftMapX, leftMapY, cv2.INTER_LINEAR)
x, y, w, h = leftROI
dst = dst[y:y + h, x:x + w]
cv2.imwrite('ImageL_undistort.png', dst)
dst = cv2.undistortPoints(img2, mtx2, dist2, recR, projR)
#dst = cv2.remap(img2, rightMapX, rightMapY, cv2.INTER_LINEAR)
x, y, w, h = rightROI
dst = dst[y:y + h, x:x + w]
cv2.imwrite('ImageR_undistort.png', dst)