1
$\begingroup$

I am trying to calibrate stereocamera using left and right chessboard images available at github.com->opencv/opencv/tree/master/samples/data

After undistorting them, left images seem to look right however right images that were generated were very small.

Left Image after undistort: left image after undistort

I then tried removing newCameraMatrix obtained from getOptimalNewCameraMatrix but undistorted images were not correct

Right Image After undistort: right image after undistort

How do I correct this? Source code:

DATA DEFINED:

NUM_IMAGES = 14
CURRENT_IMAGE = 1
PATH_TO_CALIBRATION_IMAGES = "calibrationImages\\"
PATH_TO_UNDISTORTED_IMAGES = PATH_TO_CALIBRATION_IMAGES + "undistortedImages\\"
PATTERN_SIZE = (7, 6)

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

#object points / real World coordinates like  (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

#Arrays to store these data for each test image
obj_points = [] #real world coordinates of each image
img_left_points = [] #left image coordinates of corners of each image
img_right_points = [] #right image coordinates of corners of each image

CALIBRATION: GATHERING IMAGE COORDINATES

while CURRENT_IMAGE <= NUM_IMAGES:
    left_img = cv2.imread(PATH_TO_CALIBRATION_IMAGES + "left" + str(CURRENT_IMAGE) + ".jpg")
    left_img = cv2.cvtColor(left_img,cv2.COLOR_BGR2GRAY)
    right_img = cv2.imread(PATH_TO_CALIBRATION_IMAGES + "right" + str(CURRENT_IMAGE) + ".jpg")
    right_img = cv2.cvtColor(right_img,cv2.COLOR_BGR2GRAY)
    CURRENT_IMAGE = CURRENT_IMAGE + 1
    image_size = left_img.shape[::-1]
    print("shape", image_size)
    find_chessboard_flags = cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE | cv2.CALIB_CB_FAST_CHECK

    left_found, left_corners = cv2.findChessboardCorners(left_img, PATTERN_SIZE, None)
    right_found, right_corners = cv2.findChessboardCorners(right_img, PATTERN_SIZE, None)

    if left_found and right_found:
        print("DISPLAYING CURRENT IMAGE: ", CURRENT_IMAGE - 1)
        obj_points.append(objp)
        left_corners2 = cv2.cornerSubPix(left_img, left_corners, (11,11), (-1,-1), criteria)
        img_left_points.append(left_corners2)
        right_corners2 = cv2.cornerSubPix(right_img, right_corners, (11,11), (-1,-1), criteria)
        img_right_points.append(right_corners2)
        left_img_check = cv2.drawChessboardCorners(left_img, PATTERN_SIZE, left_corners2,left_found)
        right_img_check = cv2.drawChessboardCorners(right_img, PATTERN_SIZE, right_corners2,right_found)
        cv2.imshow("left", left_img_check)
        cv2.imshow("right", right_img_check)
        cv2.waitKey(500)
cv2.destroyAllWindows()     

CALIBRATION: STEREO CALIBRATE

cameraMatrix1 = None
cameraMatrix2 = None
distCoeffs1 = None
distCoeffs2 = None
R = None
T = None
E = None
F = None
stereocalib_criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 100, 1e-5)
stereocalib_flags = cv2.CALIB_FIX_ASPECT_RATIO | cv2.CALIB_ZERO_TANGENT_DIST | cv2.CALIB_SAME_FOCAL_LENGTH | cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_FIX_K3 | cv2.CALIB_FIX_K4 | cv2.CALIB_FIX_K5



retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(obj_points,img_left_points,img_right_points,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2,image_size, R, T, E, F, stereocalib_flags, stereocalib_criteria)

new_cameraMatrix1, roi1 = cv2.getOptimalNewCameraMatrix(cameraMatrix1,distCoeffs1,image_size,1,image_size)
new_cameraMatrix2, roi2 = cv2.getOptimalNewCameraMatrix(cameraMatrix2,distCoeffs2,image_size,1,image_size)

UNDISTORTED IMAGE GENERATION

CURRENT_IMAGE = 1
while CURRENT_IMAGE<=NUM_IMAGES:
    left_img_distorted = cv2.imread(PATH_TO_CALIBRATION_IMAGES + "left" + str(CURRENT_IMAGE) + ".jpg", cv2.CV_8UC1)
    right_img_distorted = cv2.imread(PATH_TO_CALIBRATION_IMAGES + "right" + str(CURRENT_IMAGE) + ".jpg", cv2.CV_8UC1)
    #newcamera matrix none on second try
    left_img_undistort = cv2.undistort(left_img_distorted, cameraMatrix1, distCoeffs1, None, new_cameraMatrix1) 
    #newcamera matrix none on second try
    right_img_undistort = cv2.undistort(right_img_distorted, cameraMatrix2, distCoeffs2, None, new_cameraMatrix2) 
## Removed the commented part on second try
##    x,y,w,h = roi1
##    left_img_undistort = left_img_undistort[y:y+h, x:x+w]
##    x,y,w,h = roi2
##    right_img_undistort = right_img_undistort[y:y+h, x:x+w]
    cv2.imwrite(PATH_TO_UNDISTORTED_IMAGES + "undistorted_left" + str(CURRENT_IMAGE) + ".jpg",left_img_undistort)
    cv2.imwrite(PATH_TO_UNDISTORTED_IMAGES + "undistorted_right" + str(CURRENT_IMAGE) + ".jpg",right_img_undistort)
    CURRENT_IMAGE = CURRENT_IMAGE + 1
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.