Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

stereo calibrate function cv2.stereoCalibrate doesn't work

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. 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([[ 1.00000000e+00, 1.94439107e-12, -1.43540965e-16], [ -1.94439107e-12, 1.00000000e+00, -1.73569059e-15], [ 1.43540965e-16, 1.73569059e-15, 1.00000000e+00]]) T: array([[ -8.72592313e-11], [ 2.32321701e-11], [ -6.12694770e-14]]) E: array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]]) F: array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]])

Following is my code for stereo calibrate:

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((14*14,3), np.float32) objp[:,:2] = np.mgrid[0:14,0:14].T.reshape(-1,2)

Arrays to store object points and image points from all the images.

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_left, gray_left.shape[::-1])

stereo calibrate function cv2.stereoCalibrate doesn't work

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. reason.\n 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([[ 1.00000000e+00, 1.94439107e-12, -1.43540965e-16], [ -1.94439107e-12, 1.00000000e+00, -1.73569059e-15], [ 1.43540965e-16, 1.73569059e-15, 1.00000000e+00]]) T: array([[ -8.72592313e-11], [ 2.32321701e-11], [ -6.12694770e-14]]) E: array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]]) F: array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]])

Following is my code for stereo calibrate:

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((14*14,3), np.float32) objp[:,:2] = np.mgrid[0:14,0:14].T.reshape(-1,2)

Arrays to store object points and image points from all the images.

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_left, gray_left.shape[::-1])

stereo calibrate function cv2.stereoCalibrate doesn't work

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.\n cameraMatrix1: reason.

cameraMatrix1:

array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) cameraMatrix2: 1.]])

cameraMatrix2:

array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) distCoeffs1: 1.]])

distCoeffs1:

array([[ 0., 0., 0., 0., 0.]]) distCoeffs2: 0.]])

distCoeffs2:

array([[ 0., 0., 0., 0., 0.]]) R: 0.]])

R:

array([[ 1.00000000e+00, 1.94439107e-12, -1.43540965e-16], [ -1.94439107e-12, 1.00000000e+00, -1.73569059e-15], [ 1.43540965e-16, 1.73569059e-15, 1.00000000e+00]]) T: T:

array([[ -8.72592313e-11], [ 2.32321701e-11], [ -6.12694770e-14]]) E: E:

array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]]) F: F:

array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]])

Following is my code for stereo calibrate:

import numpy as np
import cv2
import glob

termination criteria

glob 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)

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

Arrays to store object points and image points from all the images.

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')

glob.glob('ImageL*.jpg') for fname in images_left: img_left = cv2.imread(fname) gray_left = cv2.cvtColor(img_left,cv2.COLOR_BGR2GRAY)

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()

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

gray_left.shape[::-1],None,None) images_right = glob.glob('ImageR*.jpg')

glob.glob('ImageR*.jpg') for fname in images_right: img_right = cv2.imread(fname) gray_right = cv2.cvtColor(img_right,cv2.COLOR_BGR2GRAY)

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()

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

None) retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F=cv2.stereoCalibrate(objpoints_left, imgpoints_left, imgpoints_left, gray_left.shape[::-1])

gray_left.shape[::-1])

stereo calibrate function cv2.stereoCalibrate doesn't work

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.

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([[ 1.00000000e+00, 1.94439107e-12, -1.43540965e-16], [ -1.94439107e-12, 1.00000000e+00, -1.73569059e-15], [ 1.43540965e-16, 1.73569059e-15, 1.00000000e+00]]) T:

array([[ -8.72592313e-11], [ 2.32321701e-11], [ -6.12694770e-14]]) E:

array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]]) F:

array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 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_left, imgpoints_right,  gray_left.shape[::-1])

stereo calibrate function cv2.stereoCalibrate doesn't work

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.

cameraMatrix1:

retval
10.76430684138752
cameraMatrix1
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])

cameraMatrix2:

1.]]) cameraMatrix2 array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])

distCoeffs1:

1.]]) distCoeffs1 array([[ 0., 0., 0., 0., 0.]])

distCoeffs2:

0.]]) distCoeffs2 array([[ 0., 0., 0., 0., 0.]])

R:

0.]]) R array([[ 1.00000000e+00, 1.94439107e-12, -1.43540965e-16], [ -1.94439107e-12, 1.00000000e+00, -1.73569059e-15], [ 1.43540965e-16, 1.73569059e-15, 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:

T array([[ -8.72592313e-11], [ 2.32321701e-11], [ -6.12694770e-14]]) E:

-1.85429627e-01], [ 4.20439537e-01], [ 2.31196471e-04]]) E array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, -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:

F array([[ -7.48092314e-01, 3.95823749e+11, 1.50088513e+14], [ -3.95823749e+11, 2.08819986e-01, 5.63727287e+14], [ -1.50088513e+14, -5.63727287e+14, 1.00000000e+00]])

-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])