1.C++对rosbag中的操作
参考链接:http://wiki.ros.org/rosbag/Code%20API#C.2B-.2B-_API

将视差图转换为深度图写入到rosbag中

rosbag::Bag bag_out(argv[2],rosbag::bagmode::Write);

uint16_t fx = 688.34765625;
uint16_t baseline =  120 ;// mm

for(size_t i = 0; i < filenames.size() && ros::ok(); i++)
{
    IplImage *pic = cvLoadImage(filenames[i].string().c_str(),0);
    std::string date_str = filenames[i].string();
    date_str =  date_str.substr(date_str.size()-23, date_str.size());
    //对图片名字进行截取,得到时间戳信息
    int secs = atoi(date_str.substr(0, 10).c_str());
    int nsecs = atoi(date_str.substr(10, date_str.size()).c_str());
    ros::Time t(secs, nsecs);

    sensor_msgs::Image imgMsg;
    imgMsg.header.stamp = t;
    imgMsg.height = pic->height;
    imgMsg.width = pic->width;
    int num = 1; 
    imgMsg.is_bigendian = !(*(char*)&num == 1);

    imgMsg.step = imgMsg.width * sizeof(uint16_t);
    imgMsg.encoding = sensor_msgs::image_encodings::MONO16;

    size_t size = imgMsg.step * imgMsg.height;
    imgMsg.data.resize(size);

    uint16_t* data = (uint16_t*)(&imgMsg.data[0]);
    int dataSize = imgMsg.width * imgMsg.height;
    for(int x = 0; x < pic->height; x ++) {
        for(int y = 0; y < pic->width; y ++) {
            uint16_t pixel = *(pic->imageData + x*pic->widthStep+y);
            if (!pixel)  continue; 
            //视察图转深度图
            pixel = (uint16_t)(fx * baseline /pixel);
            *(data++) = pixel;
        }
    }
    //写入到rosbag中
    bag_out.write(topic, t, imgMsg);
}

2.合并rosbag中的topic数据

//打开含有topic数据的bag
rosbag::Bag bag;
bag.open(argv[3], rosbag::bagmode::Read);
std::vector<std::string> topics;
//添加将要合并的topic名称
topics.push_back(std::string("/tf"));
topics.push_back(std::string("/zed/zed_node/left/camera_info"));
topics.push_back(std::string("/zed/zed_node/left/image_rect_color"));
rosbag::View view(bag, rosbag::TopicQuery(topics));
//遍历bag包
foreach(rosbag::MessageInstance const m, view)
{
    tf2_msgs::TFMessageConstPtr tf = m.instantiate<tf2_msgs::TFMessage>();
    sensor_msgs::ImageConstPtr image = m.instantiate<sensor_msgs::Image>();
    sensor_msgs::CameraInfoConstPtr info = m.instantiate<sensor_msgs::CameraInfo>();
    //赋值给所选择的类型,如果不为空,则添加到rosbag中
    if (tf != NULL){
        //std::cout<<m.getTopic()<<std::endl;
        bag_out.write(m.getTopic(), m.getTime(), *tf);
    }
    if (image != NULL){
        //std::cout<<m.getTopic()<<std::endl;
        bag_out.write(m.getTopic(), m.getTime(), *image);
    }
    if (info != NULL){
        //std::cout<<m.getTopic()<<std::endl;
        bag_out.write(m.getTopic(), m.getTime(), *info);
    }
}
//关闭写入的rosbag包
bag_out.close();
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐