stereo calibrate function cv2.stereoCalibrate doesn't work

asked 2014-03-29 10:20:44 -0600

liurw1 gravatar image

updated 2014-03-29 17:13:29 -0600

I am trying to do stereo calibration in opencv using python, the opencv version is 2.4.8. For my code below, the cv2.calibrateCamera for left-side and right-side camera works well. However, for the result of cv2.stereoCalibrate function is wrong. And I could not figure out the reason.

retval
10.76430684138752
cameraMatrix1
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
cameraMatrix2
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
distCoeffs1
array([[ 0.,  0.,  0.,  0.,  0.]])
distCoeffs2
array([[ 0.,  0.,  0.,  0.,  0.]])
R
array([[  9.99916455e-01,   1.29260319e-02,   4.98160739e-06],
       [ -1.29260319e-02,   9.99916455e-01,   2.82448153e-06],
       [ -4.94468186e-06,  -2.88863798e-06,   1.00000000e+00]])
T
array([[ -1.85429627e-01],
       [  4.20439537e-01],
       [  2.31196471e-04]])
E
array([[ -3.47380139e-01,   8.87598385e+01,  -1.60582985e+05],
       [ -8.79457745e+01,  -9.36829534e-01,  -7.08231282e+04],
       [  1.59654107e+05,   7.28929116e+04,   1.00000000e+00]])
F
array([[ -3.47380139e-01,   8.87598385e+01,  -1.60582985e+05],
       [ -8.79457745e+01,  -9.36829534e-01,  -7.08231282e+04],
       [  1.59654107e+05,   7.28929116e+04,   1.00000000e+00]])

Following is my code for stereo calibrate:

import numpy as np
import cv2
import glob

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

objp = np.zeros((14*14,3), np.float32)
objp[:,:2] = np.mgrid[0:14,0:14].T.reshape(-1,2)

objpoints_left = [] # 3d point in real world space
objpoints_right = []
imgpoints_left = [] # 2d points in image plane.
imgpoints_right = [] 

images_left = glob.glob('ImageL*.jpg')

for fname in images_left:

    img_left = cv2.imread(fname)
    gray_left = cv2.cvtColor(img_left,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret_left, corners_left = cv2.findChessboardCorners(gray_left, (14,14),None)
    #print ret_left

    # If found, add object points, image points (after refining them)
    if ret_left == True:
        objpoints_left.append(objp)

        cv2.cornerSubPix(gray_left,corners_left,(20,20),(-1,-1),criteria)
        imgpoints_left.append(corners_left)

        # Draw and display the corners
        cv2.drawChessboardCorners(img_left, (14,14), corners_left, ret_left)
        img2_left=cv2.resize(img_left, (0,0), fx=0.4, fy=0.4) 
        cv2.imshow('img',img2_left)
        cv2.waitKey(500)

cv2.destroyAllWindows()

ret_left, mtx_left, dist_left, rvecs_left, tvecs_left = cv2.calibrateCamera(objpoints_left, imgpoints_left, gray_left.shape[::-1],None,None)

images_right = glob.glob('ImageR*.jpg')

for fname in images_right:
    img_right = cv2.imread(fname)
    gray_right = cv2.cvtColor(img_right,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret_right, corners_right = cv2.findChessboardCorners(gray_right, (14,14),None)
    #print ret_right
    # If found, add object points, image points (after refining them)
    if ret_right == True:
        objpoints_right.append(objp)

        cv2.cornerSubPix(gray_right,corners_right,(20,20),(-1,-1),criteria)
        imgpoints_right.append(corners_right)

        # Draw and display the corners
        cv2.drawChessboardCorners(img_right, (14,14), corners_right, ret_right)
        img2_right=cv2.resize(img_right, (0,0), fx=0.4, fy=0.4) 
        cv2.imshow('img',img2_right)
        cv2.waitKey(500)

cv2.destroyAllWindows()

ret_right, mtx_right, dist_right, rvecs_right, tvecs_right = cv2.calibrateCamera(objpoints_right, imgpoints_right, gray_right.shape[::-1], None, None)

retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F=cv2.stereoCalibrate(objpoints_left, imgpoints_left, imgpoints_right,  gray_left.shape[::-1])
edit retag flag offensive close merge delete