bag_images_extract.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # This little program will extract some images from a bag file
4 # and write them out in .pnm format. The usage is:
5 #
6 # bag_images_extract.py bag_file_name [images_directory]
7 
8 import rosbag
9 import rospy
10 import cv
11 from sensor_msgs.msg import Image
12 from cv_bridge import CvBridge, CvBridgeError
13 import sys
14 
15 # Perform command line argument processing first:
16 arguments = sys.argv
17 del arguments[0]
18 arguments_size = len(arguments)
19 if arguments_size == 0:
20  print("Usage: bag_images_extract.py bag_file_name [images_directory]")
21 else:
22 
23  # Default directory is "."
24  directory = "."
25  if arguments_size >= 2:
26  directory = arguments[1]
27 
28  # Open the bag file:
29  bag_file_name = arguments[0]
30  bag = rosbag.Bag(bag_file_name)
31  bridge = CvBridge()
32 
33  # Sweep through messages that match *topic_name*:
34  topic_name = "/fiducials_localization/interesting_images"
35  index = 0
36  previous_time = None
37  for topic, msg, t in bag.read_messages(topic_name):
38  # Extract *cv_image* in RGB8 mode:
39  index += 1
40  try:
41  cv_image = bridge.imgmsg_to_cv(msg, desired_encoding="rgb8")
42  except CvBridgeError, e:
43  print e
44 
45  # Save the image
46  cv.SaveImage("{0}/Image{1:03d}.pnm".format(directory, index), cv_image)
47 
48  # Print out time changes:
49  if previous_time == None:
50  previous_time = t
51  dt = t - previous_time
52  previous_time = t
53  print("[{0}]:{1}".format(index, dt))
54 


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53