ros自带将bag包中点云文件解析为pcd文件
rosrun pcl_ros bag_to_pcd 3.bag /velodyne_points pcd
上述命令中,三个参数分别为bag包名,需要解析的节点名称和解析后存储的位置。解析完成后,我们可以在pcd文件夹中看到我们所需的每一帧点云文件。
通过创建view类将数据逐帧读取出来
#include <rosbag/bag.h>
#include <rosbag/view.h>
// 此处为include相关消息类的头文件,如果有自定义的头文件,请将其包含在内
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <sensor_msgs/PointCloud2.h>
//
#include <ros/ros.h>
int main(int argc, char** argv)
{
// 初始化ROS
ros::init (argc, argv, "exe_name");
//打开bag文件
rosbag::Bag bag;
bag.open("xxx.bag", rosbag::bagmode::Read);
//设置需要遍历的topic
std::vector<std::string> topics;
topics.push_back(std::string("/topicname1"));
topics.push_back(std::string("//topicname2"));
topics.push_back(std::string("//topicname3"));
//创建view,用于读取bag中的topic
rosbag::View view(bag, rosbag::TopicQuery(topics));
//rosbag::View view_all(view); //读取全部topic
//使用迭代器的方式遍历,注意:每一个迭代器为一帧数据
rosbag::View::iterator it = view.begin();
for(; it != view.end(); ++it)
{
//获取一帧数据内容,运用auto使其自动满足对应类型
auto m = *it;
//得到该帧数据的topic
std::string topic = m.getTopic();
if(topic == "/topicname1")
{
//此处为消息头文件中的ConstPtr
packagename::msgname::ConstPtr msgPtr = m.instantiate<packagename::msgname>();
if(msgPtr != NULL)
{
packagename::msgname::msg msg = *msgPtr;
...
process code
...
}
else
{
std::cout << "the null message<<std::endl;
continue;
}
}
if(topic == "/topicname2")
{
...
}
}
bag.close();
}
ros自带工具提取图片
sudo apt-get install ffmpeg
1.rosrun image_view extract_images _sec_per_frame:=0.01 image:=/mynteye/left/image_mono #
这种方式提取的图片的名称是从0依次排序的
2. rosbag play <BAGFILE> # <BAGFILE>为bag文件的路径
3. PS:如果输出的图片数量与rosbag info
命令查询得到的数量不符,可以减少_sec_per_frame参数的值,也就是此方法提取的数据可能不完整,不是特别的可靠;
提取bag文件中的数据并保存为csv格式
对于非图片数据,大部分情况下都可用csv文件存储。从section 5中我们就可以指导用rostopic echo即可打印topic中的数据。
rostopic echo -b <BAGFILE> -p <TOPIC> > <output>.csv # <BAGFILE>是bag文件,<TOPIC>为数据所在的topic
bag文件转.txt
rostopic echo -b file_name.bag -p /topic_name > Txt_name.txt