Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
def main(argv=None):
    parser = argparse.ArgumentParser(description='Simply print out a calibration result in a more'+
                                     'humanly-readable form.')

    parser.add_argument("-f", "--calibration_result_file", 
                        help="A calibration result file procded by 'calibrate video opencv.py'", 
                        required=False, default= "./cvcalib.xml")

    
    args = parser.parse_args()
    if(not osp.isfile(args.calibration_result_file)):
        raise ValueError("The file at " + args.calibration_result_file + " could not be openened.")

    calibration_info = cio.load_opencv_calibration(args.calibration_result_file)
    print(calibration_info)
Esempio n. 4
0
def load_rig(root_path, cams):
    intrinsics = []

    for cam in cams:
        intrinsics.append(
            cio.load_opencv_calibration(
                os.path.join(root_path, "calib/intrinsics/" + cam +
                             ".xml")).cameras[0].intrinsics)

    stereo_pair_labels = ["A", "E", "C", "D"]
    stereo_rigs = []
    stereo_extrinsics = []
    i_rig = 0

    for sp in stereo_pair_labels:
        stereo_rig = cio.load_opencv_calibration(
            os.path.join(root_path, "calib/stereo/" + sp + ".xml"))
        stereo_rig.label = stereo_pair_labels[i_rig]
        i_rig += 1
        stereo_rigs.append(stereo_rig)
        stereo_extrinsics.append(stereo_rig.cameras[1].extrinsics)

    cross_pairs = ["ar_el", "er_cl", "cr_dl", "dr_al"]
    cross_extrinsics = []
    for cp in cross_pairs:
        cross_extrinsics.append(
            cio.load_opencv_calibration(
                os.path.join(root_path, "calib/cross_pair/" + cp +
                             ".xml")).cameras[1].extrinsics)

    cur_pose = cb.Camera.Extrinsics()
    global_extrinsics = [cur_pose]
    tvecs = [cur_pose.translation]

    for i_pair in range(len(stereo_pair_labels)):
        stereo_rigs[i_pair].extrinsics = cur_pose
        stereo_extr = stereo_extrinsics[i_pair]  # type: cb.Camera.Extrinsics
        cur_pose = apply_extr(cur_pose, stereo_extr)
        global_extrinsics.append(cur_pose)
        tvecs.append(cur_pose.translation)
        cross_extr = cross_extrinsics[i_pair]  # type: cb.Camera.Extrinsics
        cur_pose = apply_extr(cur_pose, cross_extr)
        global_extrinsics.append(cur_pose)
        tvecs.append(cur_pose.translation)

    global_extrinsics = global_extrinsics[:-1]
    tvecs = tvecs[:-1]
    tvecs = np.array(tvecs).reshape(8, 3)
    center = tvecs.mean(axis=0).reshape(1, 3)
    offset = -center

    # move center to center of cage
    for pose in global_extrinsics:
        pose.translation = pose.translation + offset
    print("Stereo camera positions (m):")
    for stereo_rig in stereo_rigs:
        print(stereo_rig.extrinsics.translation)

    cameras = []
    for i_cam in range(len(intrinsics)):
        cam = cb.Camera(intrinsics=intrinsics[i_cam],
                        extrinsics=global_extrinsics[i_cam])
        cameras.append(cam)

    rig = Rig(cameras=cameras)

    ms_rig = MultiStereoRig(stereo_rigs=stereo_rigs)

    return rig, ms_rig
Esempio n. 5
0
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)
Esempio n. 6
0
    def __init__(self, args):
        """
        Base constructor
        """
        self.args = args

        self.aux_data_file = {}
        # load or initialize auxiliary data
        if args.aux_data_file is not None:
            self.aux_data_path = os.path.join(self.args.folder, self.args.aux_data_file)
            if osp.isfile(self.aux_data_path):
                npz_archive = np.load(osp.join(args.folder, args.aux_data_file))
                # convert to dict
                for key in npz_archive:
                    self.aux_data_file[key] = npz_archive[key]

        # set up board (3D object points of checkerboard used for calibration)
        self.object_points = []
        self.board_dims = (args.board_width, args.board_height)
        self.board_object_corner_set = np.zeros((args.board_height * args.board_width, 1, 3), np.float32)
        self.board_object_corner_set[:, :, :2] = np.indices(self.board_dims).T.reshape(-1, 1, 2)
        self.board_object_corner_set *= args.board_square_size

        self.pixel_difference_factor = 1.0 / (self.board_dims[0] * self.board_dims[1] * 3 * 256.0)

        # some vars set to default
        self.criteria_subpix = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 40, 0.001)

        if args.output is None:
            args.output = "intrinsics{0:s}.xml".format(re.sub(r"-|:", "",
                                                              str(datetime.datetime.now())[:-7])
                                                       .replace(" ", "-"))

        self.videos = [Video(os.path.join(args.folder, video_filename)) for video_filename in args.videos]
        if args.input_calibration is not None:
            intrinsic_arr = []
            # load calibration files
            initial_calibration = []
            for calib_file in args.input_calibration:
                initial_calibration.append(load_opencv_calibration(os.path.join(args.folder, calib_file)))

            for calibration_info in initial_calibration:
                if type(calibration_info) == MultiStereoRig:
                    for rig in calibration_info.rigs:
                        for camera in rig.cameras:
                            intrinsic_arr.append(camera.intrinsics)
                elif type(calibration_info) == Rig:
                    for camera in calibration_info.cameras:
                        intrinsic_arr.append(camera.intrinsics)
                elif type(calibration_info) == Camera:
                    intrinsic_arr.append(calibration_info.intrinsics)
                elif type(calibration_info) == Camera.Intrinsics:
                    intrinsic_arr.append(calibration_info)
                else:
                    raise RuntimeError("Unsupported calibration file format.")

            if len(intrinsic_arr) != len(args.videos):
                raise ValueError("The total number of intrinsics in all the provided input calibration files " +
                                 "combined ({:d}) does not equal the total number provided of video file paths ({:d})." +
                                 "These numbers must match."
                                 .format(len(intrinsic_arr), len(args.videos)))

            self.cameras = [Camera(intrinsics=intrinsics) for intrinsics in intrinsic_arr]
            if len(initial_calibration) == 1 and (type(initial_calibration[0]) == Rig or
                                                  type(initial_calibration[0]) == MultiStereoRig):
                self.rig = initial_calibration[0]
            else:
                self.rig = Rig(tuple(self.cameras))
        else:
            self.cameras = [Camera(resolution=video.frame_dims) for video in self.videos]
            self.rig = Rig(tuple(self.cameras))
Esempio n. 7
0
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)
Esempio n. 8
0
    def __init__(self, args):
        """
        Base constructor
        """
        self.args = args

        self.aux_data_file = {}
        # load or initialize auxiliary data
        if args.aux_data_file is not None:
            self.aux_data_path = os.path.join(self.args.folder,
                                              self.args.aux_data_file)
            if osp.isfile(self.aux_data_path):
                npz_archive = np.load(osp.join(args.folder,
                                               args.aux_data_file))
                # convert to dict
                for key in npz_archive:
                    self.aux_data_file[key] = npz_archive[key]

        # set up board (3D object points of checkerboard used for calibration)
        self.object_points = []
        self.board_dims = (args.board_width, args.board_height)
        self.board_object_corner_set = np.zeros(
            (args.board_height * args.board_width, 1, 3), np.float32)
        self.board_object_corner_set[:, :, :2] = np.indices(
            self.board_dims).T.reshape(-1, 1, 2)
        self.board_object_corner_set *= args.board_square_size

        self.pixel_difference_factor = 1.0 / (self.board_dims[0] *
                                              self.board_dims[1] * 3 * 256.0)

        # some vars set to default
        self.criteria_subpix = (cv2.TERM_CRITERIA_EPS +
                                cv2.TERM_CRITERIA_MAX_ITER, 40, 0.001)

        if args.output is None:
            args.output = "intrinsics{0:s}.xml".format(
                re.sub(r"-|:", "",
                       str(datetime.datetime.now())[:-7]).replace(" ", "-"))

        self.videos = [
            Video(os.path.join(args.folder, video_filename))
            for video_filename in args.videos
        ]
        if args.input_calibration is not None:
            intrinsic_arr = []
            # load calibration files
            initial_calibration = []
            for calib_file in args.input_calibration:
                initial_calibration.append(
                    load_opencv_calibration(
                        os.path.join(args.folder, calib_file)))

            for calibration_info in initial_calibration:
                if type(calibration_info) == MultiStereoRig:
                    for rig in calibration_info.rigs:
                        for camera in rig.cameras:
                            intrinsic_arr.append(camera.intrinsics)
                elif type(calibration_info) == Rig:
                    for camera in calibration_info.cameras:
                        intrinsic_arr.append(camera.intrinsics)
                elif type(calibration_info) == Camera:
                    intrinsic_arr.append(calibration_info.intrinsics)
                elif type(calibration_info) == Camera.Intrinsics:
                    intrinsic_arr.append(calibration_info)
                else:
                    raise RuntimeError("Unsupported calibration file format.")

            if len(intrinsic_arr) != len(args.videos):
                raise ValueError(
                    "The total number of intrinsics in all the provided input calibration files "
                    +
                    "combined ({:d}) does not equal the total number provided of video file paths ({:d})."
                    + "These numbers must match.".format(
                        len(intrinsic_arr), len(args.videos)))

            self.cameras = [
                Camera(intrinsics=intrinsics) for intrinsics in intrinsic_arr
            ]
            if len(initial_calibration) == 1 and (
                    type(initial_calibration[0]) == Rig
                    or type(initial_calibration[0]) == MultiStereoRig):
                self.rig = initial_calibration[0]
            else:
                self.rig = Rig(tuple(self.cameras))
        else:
            self.cameras = [
                Camera(resolution=video.frame_dims) for video in self.videos
            ]
            self.rig = Rig(tuple(self.cameras))