|
2 | 2 |
|
3 | 3 | import argparse
|
4 | 4 |
|
| 5 | +import cv2 as cv |
| 6 | +import numpy as np |
5 | 7 | import progressbar
|
| 8 | +from cv_bridge import CvBridge |
| 9 | +from image_geometry import PinholeCameraModel |
6 | 10 |
|
7 | 11 | import rosbag
|
8 | 12 |
|
9 |
| -topic_whitelist = ['/tf', '/tf_static', '/bus/vls128_roof/eth_scan/bus_to_host', '/bus/os32_left/lidar_packets', |
10 |
| - '/bus/os32_right/lidar_packets'] |
| 13 | + |
| 14 | +def generate_rectify_map(cam_model): |
| 15 | + pixel_coordinate_mapping = np.ndarray(shape=(cam_model.height, cam_model.width, 2), dtype="float32") |
| 16 | + cv.initUndistortRectifyMap(cam_model.full_K, cam_model.D, cam_model.R, cam_model.full_P, |
| 17 | + (cam_model.width, cam_model.height), cv.CV_32FC2, pixel_coordinate_mapping, None) |
| 18 | + |
| 19 | + # account for region of interest (roi) |
| 20 | + pixel_coordinate_mapping = pixel_coordinate_mapping[ |
| 21 | + cam_model.raw_roi.y_offset:cam_model.raw_roi.y_offset + cam_model.raw_roi.height, |
| 22 | + cam_model.raw_roi.x_offset:cam_model.raw_roi.x_offset + cam_model.raw_roi.width] |
| 23 | + pixel_coordinate_mapping -= np.array([cam_model.raw_roi.x_offset, cam_model.raw_roi.y_offset]) |
| 24 | + return pixel_coordinate_mapping |
| 25 | + |
| 26 | + |
| 27 | +def debayer_blur_and_rectify_image(msg_image, topic): |
| 28 | + cv_image = open_cv_bridge.imgmsg_to_cv2(msg_image, desired_encoding='rgb8') |
| 29 | + cv_image = cv.GaussianBlur(cv_image, (41, 41), 0) |
| 30 | + # try to rectify image if camera info is already available |
| 31 | + try: |
| 32 | + cv_image_rectified = cv.remap(cv_image, rectify_maps[topic], None, cv.INTER_CUBIC) |
| 33 | + except KeyError: |
| 34 | + cv_image_rectified = cv_image |
| 35 | + out = open_cv_bridge.cv2_to_imgmsg(cv_image_rectified, encoding="rgb8") |
| 36 | + out.header = msg_image.header |
| 37 | + return out |
| 38 | + |
| 39 | + |
| 40 | +topic_whitelist = [ |
| 41 | + '/tf', |
| 42 | + '/tf_static', |
| 43 | + '/bus/vls128_roof/eth_scan/bus_to_host', |
| 44 | + '/bus/os32_left/lidar_packets', |
| 45 | + '/bus/os32_right/lidar_packets', |
| 46 | + '/sensor/camera/surround/front/image_raw', |
| 47 | + '/sensor/camera/surround/front/camera_info' |
| 48 | +] |
| 49 | + |
| 50 | +# window_name = 'Remap demo' |
| 51 | +# cv.namedWindow(window_name) |
11 | 52 |
|
12 | 53 | parser = argparse.ArgumentParser(description='Creates minimal rosbag')
|
13 | 54 | parser.add_argument('--input_bag', type=str, help='specify input bag', required=True)
|
14 | 55 | parser.add_argument('--output_bag', type=str, help='specify output bag', required=True)
|
15 | 56 | parser.add_argument('--start_time', type=str, help='specify start time relative to bag start', default=0)
|
16 | 57 | parser.add_argument('--end_time', type=str, help='specify end time relative to bag start', default=-1)
|
| 58 | +parser.add_argument('--blur_image', action='store_true') |
17 | 59 | args = parser.parse_args()
|
18 | 60 |
|
19 | 61 | start = float(args.start_time)
|
20 | 62 | end = float(args.end_time)
|
21 | 63 |
|
| 64 | +open_cv_bridge = CvBridge() |
| 65 | +rectify_maps = dict() |
| 66 | + |
22 | 67 | record_stamp_of_first_msg = None
|
23 | 68 | latched_messages_before_start = []
|
24 | 69 |
|
|
34 | 79 |
|
35 | 80 | # check if topic is in whitelist
|
36 | 81 | if m.topic in topic_whitelist:
|
| 82 | + |
| 83 | + # generate rectify map from camera info in order to be able to rectify the raw image later |
| 84 | + if args.blur_image and m.connection_header['type'] == b'sensor_msgs/CameraInfo' and m.topic not in rectify_maps: |
| 85 | + model = PinholeCameraModel() |
| 86 | + model.fromCameraInfo(m.message) |
| 87 | + rectify_maps[m.topic.replace("/camera_info", "")] = generate_rectify_map(model) |
| 88 | + |
| 89 | + # check if image and whether it should be blurred |
| 90 | + if args.blur_image and m.connection_header['type'] == b'sensor_msgs/Image' and m.topic.endswith( |
| 91 | + 'image_raw'): |
| 92 | + msg = debayer_blur_and_rectify_image(m.message, m.topic.replace("/image_raw", "")) |
| 93 | + topic = m.topic[:-9] + "image_rect_color" |
| 94 | + else: |
| 95 | + msg = m.message |
| 96 | + topic = m.topic |
| 97 | + |
37 | 98 | if t_rel < start and 'latching' in m.connection_header and m.connection_header['latching'] == b'1':
|
38 | 99 | # latched message before start; save and insert it later
|
39 | 100 | latched_messages_before_start.append(m)
|
|
45 | 106 | connection_header=m_latched.connection_header) # using timestamp of m to avoid gap
|
46 | 107 |
|
47 | 108 | # write message
|
48 |
| - output_bag.write(m.topic, m.message, m.timestamp, connection_header=m.connection_header) |
| 109 | + output_bag.write(topic, msg, m.timestamp, connection_header=m.connection_header) |
0 commit comments