-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_images_from_bag.py
executable file
·40 lines (28 loc) · 1.12 KB
/
extract_images_from_bag.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
#!/usr/bin/env python
import os
import argparse
import cv2
import rosbag
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
def main():
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]):
cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
timestamp = rospy.Time(msg.header.stamp.secs, msg.header.stamp.nsecs)
cv2.imwrite(os.path.join(args.output_dir, "%.6f.png" % (timestamp.to_sec())), cv_img)
print "Wrote image %i" % count
count += 1
bag.close()
return
if __name__ == '__main__':
main()