ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Replacing camera_info messages

asked 2012-01-19 00:19:29 -0600

Hordur gravatar image

Is there any easy way to replace the camera_info messages stored in a bag file. In particular when using either the image_proc or stereo_image_proc nodes.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2012-01-20 09:14:12 -0600

Patrick Mihelich gravatar image

Some simple operations can be done from the command line. For example, if you just want to remove the camera info messages, you can do:

rosbag filter orig.bag filtered.bag "m._type != 'sensor_msgs/CameraInfo'"

If you need to mutate messages in a bag file or some other more complicated operation, the easiest way is to write a Python script using the rosbag code API. Below is a script I used at some point to replace the value of header.frame_id for all CameraInfo and Image messages in one or more bags:

#!/usr/bin/env python

PKG = 'sensor_msgs'
import roslib; roslib.load_manifest(PKG)
import rospy
import rosrecord
import fileinput
import os

def fix(inbags):
  for b in inbags:
    print "Trying to migrating file: %s"%b
    outbag = b+'.tmp'
    rebag = rosrecord.Rebagger(outbag)
      for (topic, msg, t) in rosrecord.logplayer(b, raw=False):
        if msg._type == 'sensor_msgs/CameraInfo' or msg._type == 'sensor_msgs/Image':
          msg.header.frame_id = 'l_forearm_cam_optical_frame'
          rebag.add(topic, msg, t, raw=False)
          rebag.add(topic, msg, t, raw=False)
    except rosrecord.ROSRecordException, e:
      print " Migration failed: %s"%(e.message)

    oldnamebase = b+'.old'
    oldname = oldnamebase
    i = 1
    while os.path.isfile(oldname):
      oldname = oldnamebase + str(i)
    os.rename(b, oldname)
    os.rename(outbag, b)
    print " Migration successful.  Original stored as: %s"%oldname

if __name__ == '__main__':
  import sys
  if len(sys.argv) >= 2:
    print "usage: %s bag1 [bag2 bag3 ...]" % sys.argv[0]
edit flag offensive delete link more

answered 2013-02-16 11:55:17 -0600

sergey_alexandrov gravatar image

You can use the following function to replace all camera info messages in a certain topic with another given camera info message:

def replace_camera_info_messages(bag, topic, camera_info):                  
    import os.path                                                          
    import rosbag                                                           
    orig = os.path.splitext(bag)[0] + '.orig.bag'                           
    os.rename(bag, orig)                                                    
    inbag = rosbag.Bag(orig, 'r')                                           
    outbag = rosbag.Bag(bag, 'w')                                           
    for t, msg, ts in inbag.read_messages():                                
        if t == topic:                                                      
            camera_info.header.seq = msg.header.seq                         
            camera_info.header.stamp = msg.header.stamp                     
            outbag.write(t, camera_info, ts)                                
            outbag.write(t, msg, ts)                                        
edit flag offensive delete link more

answered 2012-01-19 05:48:14 -0600

I've not tried this but...

I think either the

  • rosbag filter
  • rosbag fix

commands might be able to do what you're hoping to do


edit flag offensive delete link more

Question Tools


Asked: 2012-01-19 00:19:29 -0600

Seen: 2,642 times

Last updated: Feb 16 '13