def get_tv_to_rgb_matrix(
    rgb_calibration_file_names, tv_calibration_file_names, rgb_relative_file_names,
        tv_relative_file_names, chessboard_cell_width_meters, inner_width, inner_height,
        on_calibrated_signal=None):

    ret_rgb, mtx_rgb, dist_rgb, rvecs_rgb, tvecs_rgb, img_points_rgb, objpoints, used_rgb_files = \
        calib.calibrate_camera(
            rgb_calibration_file_names, inner_width, inner_height, on_calibrated_signal)

    ret_tv, mtx_tv, dist_tv, rvecs_tv, tvecs_tv, img_points_tv, objpoints, used_tv_files = \
        calib.calibrate_camera(
            tv_calibration_file_names, inner_width, inner_height, on_calibrated_signal)

    if rgb_calibration_file_names is not None:
        image_size = calib.get_image_size(rgb_calibration_file_names[0])
    else:
        image_size = calib.get_image_size(rgb_relative_file_names[0])

    objpoints = calib.build_obj_points(inner_width, inner_height)

    solo_calibrated_points_tv = img_points_tv
    solo_calibrated_points_rgb = img_points_rgb
    img_points_tv = []
    img_points_rgb = []
    for fname_rgb, fname_tv in zip(rgb_relative_file_names, tv_relative_file_names):
        tv_points = solo_calibrated_points_tv[
            tv_calibration_file_names.index(fname_tv)]
        rgb_points = solo_calibrated_points_rgb[
            rgb_calibration_file_names.index(fname_rgb)]
        # rgb_points = calib.get_image_points(fname_rgb, inner_width, inner_height)
        # tv_points = calib.get_image_points(fname_tv, inner_width,
        # inner_height)

        if (rgb_points is not None and tv_points is not None):
            img_points_rgb.append(rgb_points)
            img_points_tv.append(tv_points)

    if len(img_points_rgb) == 0:
        print("Considered photos can't be used to determine relative position")
        return None

    retval, cameraMatrix1, distCoeffs1, cameraMatrix2, \
        distCoeffs2, R, T, E, F = calib.calibrate_rgb_and_tv(
            objpoints, img_points_rgb,
            img_points_tv, image_size, mtx_rgb, dist_rgb, mtx_tv, dist_tv)

    T *= chessboard_cell_width_meters  # now translations is in meters

    A = np.append(np.append(R, T, axis=1), np.array([[0, 0, 0, 1]]), axis=0)
    return A, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2
def run_calibration(rgb_images, tv_images, rgb_relative, tv_relative, cell_size, inner_width, inner_height,
                    on_calibrated_signal=None):
    A, cameraMatrix_tv, distCoeffs_tv, cameraMatrix_rgb, distCoeffs_rgb = \
        get_tv_to_rgb_matrix(rgb_images, tv_images, rgb_relative, tv_relative, cell_size, inner_width,
                inner_height, on_calibrated_signal)
    tv_image_width, tv_image_height = calib.get_image_size(tv_images[0])
    return tv_image_width, tv_image_height, \
        A, cameraMatrix_tv, distCoeffs_tv, cameraMatrix_rgb, distCoeffs_rgb