def calibrate_intrinsic_parameters(calibration_data, calibration_results_file): """Calibrate intrinsic parameters of the camera given different images taken for the Charuco board from different views, the resulting parameters are saved to the provided filename. Args: calibration_data (str): directory of the stored images of the Charuco board. calibration_results_file (str): filepath that will be used to write the calibration results in. """ handler = CharucoBoardHandler() camera_matrix, dist_coeffs, error = handler.calibrate( calibration_data, visualize=False ) camera_info = dict() camera_info["camera_matrix"] = dict() camera_info["camera_matrix"]["rows"] = 3 camera_info["camera_matrix"]["cols"] = 3 camera_info["camera_matrix"]["data"] = camera_matrix.flatten().tolist() camera_info["distortion_coefficients"] = dict() camera_info["distortion_coefficients"]["rows"] = 1 camera_info["distortion_coefficients"]["cols"] = 5 camera_info["distortion_coefficients"][ "data" ] = dist_coeffs.flatten().tolist() with open(calibration_results_file, "w") as outfile: yaml.dump( camera_info, outfile, default_flow_style=False, ) return
def main(): """Execute an action depending on arguments passed by the user.""" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "action", choices=["create_board", "detect_live", "detect_image", "calibrate"], help="""Action that is executed.""", ) parser.add_argument( "--filename", type=str, help="""Filename used for saving or loading images (depending on the action). """, ) parser.add_argument( "--calibration-data", type=str, help="""Path to the calibration data directory (only used for action 'calibrate'). """, ) parser.add_argument( "--camera-info", type=str, help="""Camera info file, including intrinsic parameters.""", ) parser.add_argument( "--no-gui", action="store_true", help="""Set to disable any GUI-based visualization.""", ) args = parser.parse_args() camera_matrix = None distortion_coeffs = None if args.camera_info: camera_info = CameraCalibrationFile(args.camera_info) camera_matrix = camera_info["camera_matrix"] distortion_coeffs = camera_info["distortion_coefficients"] handler = CharucoBoardHandler( BOARD_SIZE_X, BOARD_SIZE_Y, BOARD_SQUARE_SIZE, BOARD_MARKER_SIZE, camera_matrix, distortion_coeffs, ) if args.action == "create_board": if not args.filename: raise RuntimeError("Filename not specified.") handler.save_board(args.filename) elif args.action == "detect_live": handler.detect_board_in_camera_stream() elif args.action == "detect_image": if not args.filename: raise RuntimeError("Filename not specified.") handler.detect_board_in_image(args.filename, visualize=not args.no_gui) elif args.action == "calibrate": pattern = os.path.join(args.calibration_data, args.filename) files = glob.glob(pattern) handler.calibrate( files, visualize=not args.no_gui, )