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]
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
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()
Asked: 2012-01-19 00:19:29 -0500
Seen: 2,657 times
Last updated: Feb 16 '13
Distorted images after camera calibration+stereo_image_proc [closed]
save file in different directory - launch file
Problems in extracting images from bag file
rqt_graph when playing a rosbag
how to use stereo_image_proc.launch
How can i read messages from a ROSbag file ?
publish only some topics of a rosbag file (Command line or launch file)