def __init__(self, args): self.args = args for path in args.images: print(os.path.join(args.folder, path)) self.images = [cv2.imread(os.path.join(args.folder, path)) for path in args.images] print(args.input_calibration) if args.input_calibration is not None: self.rig = cio.load_opencv_calibration(os.path.join(args.folder, args.input_calibration)) self.images = undistort_stereo(self.rig, self.images[0], self.images[1], 1.8)
def __init__(self, args): self.args = args for path in args.images: print(os.path.join(args.folder, path)) self.images = [ cv2.imread(os.path.join(args.folder, path)) for path in args.images ] print(args.input_calibration) if args.input_calibration is not None: self.rig = cio.load_opencv_calibration( os.path.join(args.folder, args.input_calibration)) self.images = undistort_stereo(self.rig, self.images[0], self.images[1], 1.8)
def run_calibration(self): min_frames = ApplicationSynced.min_frames_to_calibrate if self.usable_frame_count < min_frames: print("Not enough usable frames to calibrate." + " Need at least {0:d}, got {1:d}".format( min_frames, self.usable_frame_count)) return if self.args.test: print("Testing existing calibration (no output will be saved)...") else: print("Calibrating for max. {0:d} iterations...".format( self.args.max_iterations)) calibrate(self.rig, [video.image_points for video in self.videos], self.board_object_corner_set, self.args.use_fisheye_model, self.args.use_rational_model, self.args.use_tangential_coeffs, self.args.use_thin_prism, self.args.fix_radial, self.args.fix_thin_prism, self.args.precalibrate_solo, self.args.stereo_only, self.args.max_iterations, self.args.input_calibration is not None, self.args.test) if len(self.videos) > 1: if self.args.preview: l_im = cv2.imread( osp.join(self.args.folder, self.args.preview_files[0])) r_im = cv2.imread( osp.join(self.args.folder, self.args.preview_files[1])) l_im, r_im = undistort_stereo(self.rig, l_im, r_im) path_l = osp.join( self.args.folder, self.args.preview_files[0][:-4] + "_rect.png") path_r = osp.join( self.args.folder, self.args.preview_files[1][:-4] + "_rect.png") cv2.imwrite(path_l, l_im) cv2.imwrite(path_r, r_im) if not self.args.skip_printing_output: print(self.rig) if not self.args.skip_saving_output and not self.args.test: cio.save_opencv_calibration( osp.join(self.args.folder, self.args.output), self.rig)
def run_calibration(self): min_frames = ApplicationSynced.min_frames_to_calibrate if self.usable_frame_count < min_frames: print("Not enough usable frames to calibrate." + " Need at least {0:d}, got {1:d}".format(min_frames, self.usable_frame_count)) return if self.args.test: print("Testing existing calibration (no output will be saved)...") else: print("Calibrating for max. {0:d} iterations...".format(self.args.max_iterations)) calibrate(self.rig, [video.image_points for video in self.videos], self.board_object_corner_set, self.args.use_fisheye_model, self.args.use_rational_model, self.args.use_tangential_coeffs, self.args.use_thin_prism, self.args.fix_radial, self.args.fix_thin_prism, self.args.precalibrate_solo, self.args.stereo_only, self.args.max_iterations, self.args.input_calibration is not None, self.args.test) if len(self.videos) > 1: if self.args.preview: l_im = cv2.imread(osp.join(self.args.folder, self.args.preview_files[0])) r_im = cv2.imread(osp.join(self.args.folder, self.args.preview_files[1])) l_im, r_im = undistort_stereo(self.rig, l_im, r_im) path_l = osp.join(self.args.folder, self.args.preview_files[0][:-4] + "_rect.png") path_r = osp.join(self.args.folder, self.args.preview_files[1][:-4] + "_rect.png") cv2.imwrite(path_l, l_im) cv2.imwrite(path_r, r_im) if not self.args.skip_printing_output: print(self.rig) if not self.args.skip_saving_output and not self.args.test: cio.save_opencv_calibration(osp.join(self.args.folder, self.args.output), self.rig)
def main(): parser = argparse.ArgumentParser(description='Simply print out a calibration result in a more' + 'humanly-readable form.') parser.add_argument("-f", "--folder", help="Path to root folder to work in", required=False, default="./") parser.add_argument("-c", "--calibration_result_file", help="A calibration result file procded by 'calibrate video opencv.py'", required=False, default="./cvcalib.xml") parser.add_argument("-v", "--verbose", help="Print additional information along the way.", required=False, default=False, action='store_true') parser.add_argument("-i", "--images", help="Input image(s), lens-distorted", required=False, action=required_length(1, 2), nargs="+", metavar="PATH", default=["left.png, right.png"]) parser.add_argument("-o", "--output", help="Output file names, undistored image(s)" , metavar="PATH", action=required_length(1, 2), nargs="+", required=False, default=["left_rect.png", "right_rect.png"]) parser.add_argument("-cf", "--canvas_size_factor", help="Output canvas size, in pixels, will be " + "(input_width * canvas_size_factor, input_height * canvas_size_factor)", type=float, default=1.8) args = parser.parse_args() calibration_info = cio.load_opencv_calibration(osp.join(args.folder, args.calibration_result_file)) if args.verbose: print(calibration_info) if type(calibration_info) == Rig and len(calibration_info.cameras) == 2: if len(args.images) < 2: raise ValueError("Got a stereo rig but less than two input images. Aborting.") if len(args.output) < 2: raise ValueError("Got a stereo rig but less than two output filenames. Aborting.") left = cv2.imread(osp.join(args.folder, args.images[0])) right = cv2.imread(osp.join(args.folder, args.images[1])) rig = calibration_info c0 = rig.cameras[0].intrinsics c1 = rig.cameras[1].intrinsics if (left.shape[0], left.shape[1]) != c0.resolution: raise ValueError("Left image size does not correspond to resolution provided in the calibration file.") if (right.shape[0], right.shape[1]) != c1.resolution: raise ValueError("Right image size does not correspond to resolution provided in the calibration file.") cf = args.canvas_size_factor rect_left, rect_right = undistort_stereo(rig, left, right, cf) cv2.imwrite(osp.join(args.folder, args.output[0]), rect_left) cv2.imwrite(osp.join(args.folder, args.output[1]), rect_right) else: if len(args.images) > 1: print("Warning: provided a single-camera calibration but more than one input image." + " Using only the first one.") img = cv2.imread(osp.join(args.folder, args.images[0])) if type(calibration_info) == Camera: calibration_info = calibration_info.intrinsics elif type(calibration_info) == Rig: calibration_info = calibration_info.cameras[0].intrinsics if (img.shape[0], img.shape[1]) != calibration_info.resolution: raise ValueError("Image size does not correspond to resolution provided in the calibration file.") cf = args.canvas_size_factor map_x, map_y = cv2.initUndistortRectifyMap(calibration_info.intrinsic_mat, calibration_info.distortion_coeffs, None, None, (int(img.shape[1] * cf), int(img.shape[0] * cf)), cv2.CV_32FC1) out = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR) cv2.imwrite(osp.join(args.folder, args.output[0]), out)
def main(): parser = argparse.ArgumentParser( description='Simply print out a calibration result in a more' + 'humanly-readable form.') parser.add_argument("-f", "--folder", help="Path to root folder to work in", required=False, default="./") parser.add_argument( "-c", "--calibration_result_file", help="A calibration result file procded by 'calibrate video opencv.py'", required=False, default="./cvcalib.xml") parser.add_argument("-v", "--verbose", help="Print additional information along the way.", required=False, default=False, action='store_true') parser.add_argument("-i", "--images", help="Input image(s), lens-distorted", required=False, action=required_length(1, 2), nargs="+", metavar="PATH", default=["left.png, right.png"]) parser.add_argument("-o", "--output", help="Output file names, undistored image(s)", metavar="PATH", action=required_length(1, 2), nargs="+", required=False, default=["left_rect.png", "right_rect.png"]) parser.add_argument( "-cf", "--canvas_size_factor", help="Output canvas size, in pixels, will be " + "(input_width * canvas_size_factor, input_height * canvas_size_factor)", type=float, default=1.8) args = parser.parse_args() calibration_info = cio.load_opencv_calibration( osp.join(args.folder, args.calibration_result_file)) if args.verbose: print(calibration_info) if type(calibration_info) == Rig and len(calibration_info.cameras) == 2: if len(args.images) < 2: raise ValueError( "Got a stereo rig but less than two input images. Aborting.") if len(args.output) < 2: raise ValueError( "Got a stereo rig but less than two output filenames. Aborting." ) left = cv2.imread(osp.join(args.folder, args.images[0])) right = cv2.imread(osp.join(args.folder, args.images[1])) rig = calibration_info c0 = rig.cameras[0].intrinsics c1 = rig.cameras[1].intrinsics if (left.shape[0], left.shape[1]) != c0.resolution: raise ValueError( "Left image size does not correspond to resolution provided in the calibration file." ) if (right.shape[0], right.shape[1]) != c1.resolution: raise ValueError( "Right image size does not correspond to resolution provided in the calibration file." ) cf = args.canvas_size_factor rect_left, rect_right = undistort_stereo(rig, left, right, cf) cv2.imwrite(osp.join(args.folder, args.output[0]), rect_left) cv2.imwrite(osp.join(args.folder, args.output[1]), rect_right) else: if len(args.images) > 1: print( "Warning: provided a single-camera calibration but more than one input image." + " Using only the first one.") img = cv2.imread(osp.join(args.folder, args.images[0])) if type(calibration_info) == Camera: calibration_info = calibration_info.intrinsics elif type(calibration_info) == Rig: calibration_info = calibration_info.cameras[0].intrinsics if (img.shape[0], img.shape[1]) != calibration_info.resolution: raise ValueError( "Image size does not correspond to resolution provided in the calibration file." ) cf = args.canvas_size_factor map_x, map_y = cv2.initUndistortRectifyMap( calibration_info.intrinsic_mat, calibration_info.distortion_coeffs, None, None, (int(img.shape[1] * cf), int(img.shape[0] * cf)), cv2.CV_32FC1) out = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR) cv2.imwrite(osp.join(args.folder, args.output[0]), out)