forked from gavincangan/wind-prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbag_to_images.py
48 lines (35 loc) · 1.34 KB
/
bag_to_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2016 Massachusetts Institute of Technology
"""Extract images from a rosbag.
"""
import os
import argparse
import cv2
import rosbag
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
def main():
"""Extract a folder of images from a rosbag.
"""
parser = argparse.ArgumentParser(description="Extract images from a ROS bag.")
parser.add_argument("bag_file", help="Input ROS bag.")
parser.add_argument("output_dir", help="Output directory.")
parser.add_argument("image_topic", help="Image topic.")
args = parser.parse_args()
print "Extract images from %s on topic %s into %s" % (args.bag_file,
args.image_topic, args.output_dir)
bag = rosbag.Bag(args.bag_file, "r")
bridge = CvBridge()
count = 0
for topic, msg, t in bag.read_messages(topics=[args.image_topic]):
#print msg.header.stamp.secs, msg.header.stamp.nsecs
cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
im_name = str(msg.header.stamp.secs) + '.' + str(msg.header.stamp.nsecs)
cv2.imwrite(os.path.join(args.output_dir, im_name + ".png"), cv_img)
print "Wrote image %s" % im_name
count += 1
bag.close()
return
if __name__ == '__main__':
main()