示例#1
0
def stereo_calibrate(left_file,
                     right_file,
                     left_dir,
                     left_prefix,
                     right_dir,
                     right_prefix,
                     image_format,
                     save_file,
                     square_size,
                     width=9,
                     height=6):
    """ Stereo calibration and rectification """
    objp, leftp, rightp = load_image_points(left_dir, left_prefix, right_dir,
                                            right_prefix, image_format,
                                            square_size, width, height)

    K1, D1 = load_coefficients(left_file)
    K2, D2 = load_coefficients(right_file)

    flags = 0
    flags |= cv2.CALIB_FIX_INTRINSIC
    #flags |= cv2.CALIB_USE_INTRINSIC_GUESS
    flags |= cv2.CALIB_FIX_ASPECT_RATIO
    #flags |= cv2.CALIB_SAME_FOCAL_LENGTH
    #flags |= cv2.CALIB_SAME_FOCAL_LENGTH

    ret, K1, D1, K2, D2, R, T, E, F = cv2.stereoCalibrate(objp,
                                                          leftp,
                                                          rightp,
                                                          K1,
                                                          D1,
                                                          K2,
                                                          D2,
                                                          image_size,
                                                          flags=flags)
    print("Stereo calibration rms: ", ret)
    R1, R2, P1, P2, Q, roi_left, roi_right = cv2.stereoRectify(
        K1,
        D1,
        K2,
        D2,
        image_size,
        R,
        T,
        flags=cv2.CALIB_ZERO_DISPARITY,
        alpha=0.6)

    save_stereo_coefficients(save_file, K1, D1, K2, D2, R, T, E, F, R1, R2, P1,
                             P2, Q)
def stereo_calibrate(left_file,
                     right_file,
                     left_dir,
                     left_prefix,
                     right_dir,
                     right_prefix,
                     image_format,
                     save_file,
                     square_size,
                     width=9,
                     height=6):
    """ Stereo calibration and rectification """
    objp, leftp, rightp = load_image_points(left_dir, left_prefix, right_dir,
                                            right_prefix, image_format,
                                            square_size, width, height)

    K1, D1 = load_coefficients(left_file)
    print("left_camera_matrix:")
    print(K1)
    print("left_distortion:")
    print(D1)
    print("--" * 30)
    K2, D2 = load_coefficients(right_file)
    print("right_camera_matrix:")
    print(K2)
    print("right_distortion:")
    print(D2)
    print("--" * 30)

    flag = 0
    # flag |= cv2.CALIB_FIX_INTRINSIC
    flag |= cv2.CALIB_USE_INTRINSIC_GUESS
    ret, K1, D1, K2, D2, R, T, E, F = cv2.stereoCalibrate(
        objp, leftp, rightp, K1, D1, K2, D2, image_size)
    print("Stereo calibration rms: ", ret)
    R1, R2, P1, P2, Q, roi_left, roi_right = cv2.stereoRectify(
        K1,
        D1,
        K2,
        D2,
        image_size,
        R,
        T,
        flags=cv2.CALIB_ZERO_DISPARITY,
        alpha=0.9)

    save_stereo_coefficients(save_file, K1, D1, K2, D2, R, T, E, F, R1, R2, P1,
                             P2, Q)
def extrinsic_calibration(left_path: list, right_path: list, img_width: int, img_height: int, used_image_ratio: int):
    """Find intrinsic and extrinsic calibration parameters for stereo pair of pinhol cameras."""
    leftp_dict = find_chessboard_corners_2d(left_path, window_size=(5, 5),
                                            find_flags=cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS)
    rightp_dict = find_chessboard_corners_2d(right_path, window_size=(5, 5),
                                             find_flags=cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS)
    good_couples_path = set(leftp_dict).intersection(rightp_dict)
    leftp = [leftp_dict.get(key) for key in good_couples_path]
    rightp = [rightp_dict.get(key) for key in good_couples_path]
    leftp = leftp[0::used_image_ratio]
    rightp = rightp[0::used_image_ratio]
    objp = define_chessboard_corners_3d(len(leftp))
    print('Begin stereo calibration for frames: ', len(objp))
    K1, D1 = load_coefficients(LEFT_INTRINSIC)
    K2, D2 = load_coefficients(RIGHT_INTRINSIC)
    ret, K1, D1, K2, D2, R, T, E, F = cv2.stereoCalibrate(objp, leftp, rightp, K1, D1, K2, D2, (img_height, img_width))
    R1, R2, P1, P2, Q, roi_left, roi_right = cv2.stereoRectify(K1, D1, K2, D2, (img_height, img_width), R, T,
                                                               flags=cv2.CALIB_ZERO_DISPARITY, alpha=0.9)
    save_stereo_coefficients(EXTRINSIC, K1, D1, K2, D2, R, T, E, F, R1, R2, P1, P2, Q)