Replacing camera_info messages
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.
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
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.
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)
try:
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)
else:
rebag.add(topic, msg, t, raw=False)
rebag.close()
except rosrecord.ROSRecordException, e:
print " Migration failed: %s"%(e.message)
os.remove(outbag)
continue
oldnamebase = b+'.old'
oldname = oldnamebase
i = 1
while os.path.isfile(oldname):
i=i+1
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:
fix(sys.argv[1:])
else:
print "usage: %s bag1 [bag2 bag3 ...]" % sys.argv[0]
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)
else:
outbag.write(t, msg, ts)
inbag.close()
outbag.close()
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
Asked: 2012-01-19 00:19:29 -0600
Seen: 2,734 times
Last updated: Feb 16 '13
TF Frame Convention for Stereo Cameras
Is it better/prefered to combine nodelets programmatically or in a launch file?
How can additional nodelets be attached to the stereo_image_proc's nodelet manager in Diamondback?
proliferation of dynamic_reconfigure servers due to image_proc or image_transport
How to recover extrinsic parameters for unrectified images from stereo camera [closed]
Rosbag: Recording configurations or regenerate later?
How can I speed up image rectification?
How to extract data from *.bag?