Rosanswers logo


I'd like to filter out some "bad messages" from my rosbags. I'm using the C++ rosbag API. Should I open the bagfile in read mode and republish the good messages and record them in another bag using 'rosbag record'? Or is there a way to modify the bag in place when it's in read or write mode?

I've gone through the C++ API of rosbag a bit, I couldn't find examples similar to what I want to do. Thanks.

Update: Is there a method to find out the list of topics that are present in the bag and other statistics through the API? Is there a way to find out which topic, a particular message instance is from while iterating over them in a boost foreach loop as shown in the example here

Originally posted by 2ROS0 on ROS Answers with karma: 1133 on 2016-08-01

Post score: 2


2 Answers 2


Rosanswers logo

It's a bit easier with python:

import rosbag

with rosbag.Bag('output.bag', 'w') as outbag:
    for topic, msg, t in rosbag.Bag('input.bag').read_messages():
        if topic == "/tf" and msg.transforms:
            outbag.write(topic, msg, msg.transforms[0].header.stamp)
            outbag.write(topic, msg, msg.header.stamp if msg._has_header else t)


Originally posted by NEngelhard with karma: 3519 on 2016-08-01

This answer was NOT ACCEPTED on the original site

Post score: 1

Original comments

Comment by 2ROS0 on 2016-08-02:
Yeah, I came across this on the wiki. This doesn't help though because I specifically want to use the C++ API. I figured out a roundabout way to do it (based off of the C++ API which I have shared in my answer).

Comment by eRCaGuy on 2020-06-06:
More specific link where you got this code from: http://wiki.ros.org/rosbag/Cookbook#Rewrite_bag_with_header_timestamps

Comment by eRCaGuy on 2020-06-28:
I recently wrote ros_readbagfile, and this tutorial on how to use it, here: http://wiki.ros.org/ROS/Tutorials/reading%20msgs%20from%20a%20bag%20file. See my answer here too: https://answers.ros.org/question/240829/filter-out-messages-from-a-ros-bag/?answer=240861#post-id-240861.


Rosanswers logo

I opened the bag file in read mode and used the rosbag::View class to look at each topic individually. Then saved all the messages into an object I'm more comfortable with (std::vector for instance). It's based off of the example on this page in the Wiki.

And so perform all operations/logic on the more familiar data structure and then open a new bag file in write mode into which you can add all the required messages.

This is a roundabout way to do it, but I couldn't find a method in the API to: 1. find out which topic a particular message is coming from (if I were to view all topics at once in a single for loop) or 2. modify the messages in a particular topic in place or query what topics are present in a bag and other things like that.

Originally posted by 2ROS0 with karma: 1133 on 2016-08-02

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.