|
| 1 | +""" |
| 2 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 3 | +Manually select points to get the projection map |
| 4 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | +""" |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import numpy as np |
| 9 | +import cv2 |
| 10 | +from surround_view import FisheyeCameraModel, PointSelector, display_image |
| 11 | +import surround_view.param_settings as settings |
| 12 | + |
| 13 | + |
| 14 | +def get_projection_map(camera_model, image, dst_points): |
| 15 | + image = camera_model.undistort(image) |
| 16 | + gui = PointSelector(image, title=camera_model.camera_name) |
| 17 | + choice = gui.loop() |
| 18 | + if choice > 0: |
| 19 | + src = np.float32(gui.keypoints) |
| 20 | + dst = np.float32(dst_points) |
| 21 | + camera_model.project_matrix = cv2.getPerspectiveTransform(src, dst) |
| 22 | + image = camera_model.project(image) |
| 23 | + |
| 24 | + ret = display_image("Bird's View", image) |
| 25 | + if ret > 0: |
| 26 | + return True |
| 27 | + if ret < 0: |
| 28 | + cv2.destroyAllWindows() |
| 29 | + |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +parser = argparse.ArgumentParser() |
| 34 | +parser.add_argument("-camera", required=True, |
| 35 | + choices=["front", "back", "left", "right"], |
| 36 | + help="The camera view to be projected") |
| 37 | +parser.add_argument("-scale", nargs="+", default=None, |
| 38 | + help="scale the undistorted image") |
| 39 | +parser.add_argument("-shift", nargs="+", default=None, |
| 40 | + help="shift the undistorted image") |
| 41 | +args = parser.parse_args() |
| 42 | + |
| 43 | +if args.scale is not None: |
| 44 | + scale = [float(x) for x in args.scale] |
| 45 | +else: |
| 46 | + scale = (1.0, 1.0) |
| 47 | + |
| 48 | +if args.shift is not None: |
| 49 | + shift = [float(x) for x in args.shift] |
| 50 | +else: |
| 51 | + shift = (0, 0) |
| 52 | + |
| 53 | +camera_name = args.camera |
| 54 | +camera_file = os.path.join(os.getcwd(), "yaml", camera_name + ".yaml") |
| 55 | +image_file = os.path.join(os.getcwd(), "images", camera_name + ".png") |
| 56 | +image = cv2.imread(image_file) |
| 57 | +dst_points = settings.dst_points[camera_name] |
| 58 | +camera = FisheyeCameraModel(camera_file, camera_name) |
| 59 | +camera.set_scale_and_shift(scale, shift) |
| 60 | +success = get_projection_map(camera, image, dst_points) |
| 61 | +if success: |
| 62 | + print("saving projection matrix to yaml") |
| 63 | + |
| 64 | +else: |
| 65 | + print("failed to compute the projection map") |
0 commit comments