Last active
October 4, 2024 17:52
-
-
Save wngreene/835cda68ddd9c5416defce876a4d7dd9 to your computer and use it in GitHub Desktop.
Extract images from a rosbag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]): | |
cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough") | |
cv2.imwrite(os.path.join(args.output_dir, "frame%06i.png" % count), cv_img) | |
print "Wrote image %i" % count | |
count += 1 | |
bag.close() | |
return | |
if __name__ == '__main__': | |
main() |
This method doesn't work on compressed messages. Tweaking this code with only subscriber and playing the rosbag can work
Pretty cool:)
I'm trying to import rosbag, but it can't find the module, can someone share how you installed the module?
how can i use cv_bridge in anaconda(windows 10)? i am getting error like this
ERROR: Could not find a version that satisfies the requirement cvbridge (from versions: none)
ERROR: No matching distribution found for cvbridge
Thank you for the share
@wngreene do you have a version of script for ros2 Humble? Thank you.
@monajalal If you have a script for ros2 Humble, do share it! Thank You!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THX! very useful!