ROS学习笔记52--图片从compressed格式转raw格式代码接口介绍

Posted 鸿_H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS学习笔记52--图片从compressed格式转raw格式代码接口介绍相关的知识,希望对你有一定的参考价值。

背景:操作rosbag,目的将图片的compressed格式转raw格式,让后进行操作。发现python并没有对应的工具接口实现,而image_transport接口用起来也没有时效性(总不能将bag播放录制,过于费时费存储空间)

概要:这里介绍image_transport其中常用类型图片格式compressed到raw转换接口,当然对于存在其他类型的图片类型应该选用其他接口实现。

代码:

/// read rosbag
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <boost/foreach.hpp>
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CompressedImage.h>

// ros compressed/image ---> image
#include <turbojpeg.h>
#include <sensor_msgs/image_encodings.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>

  //rosbag
  tjhandle tj_;
  tj_ = tjInitDecompress();
   
sensor_msgs::ImagePtr decompressJPEG(const std::vector<uint8_t>& data, const std::string& source_encoding, const std_msgs::Header& header)

    if (!tj_)
        tj_ = tjInitDecompress();

    int width, height, jpegSub, jpegColor;
    
    // Old TurboJPEG require a const_cast here. This was fixed in TurboJPEG 1.5.
    uint8_t* src = const_cast<uint8_t*>(data.data());
    
    if (tjDecompressHeader3(tj_, src, data.size(), &width, &height, &jpegSub, &jpegColor) != 0)
    
        return sensor_msgs::ImagePtr(); // If we cannot decode the JPEG header, silently fall back to OpenCV
    
        
    sensor_msgs::ImagePtr ret(new sensor_msgs::Image);
    ret->header = header;
    ret->width = width;
    ret->height = height;
    ret->encoding = source_encoding;

    int pixelFormat;

    if (source_encoding == enc::MONO8)
    
        ret->data.resize(height*width);
        ret->step = ret->width;
        pixelFormat = TJPF_GRAY;
    
    else if (source_encoding == enc::RGB8)
    
        ret->data.resize(height*width*3);
        ret->step = width*3;
        pixelFormat = TJPF_RGB;
    
    else if (source_encoding == enc::BGR8)
    
        ret->data.resize(height*width*3);
        ret->step = width*3;
        pixelFormat = TJPF_BGR;
    
    else if (source_encoding == enc::RGBA8)
    
        ret->data.resize(height*width*4);
        ret->step = width*4;
        pixelFormat = TJPF_RGBA;
    
    else if (source_encoding == enc::BGRA8)
    
        ret->data.resize(height*width*4);
        ret->step = width*4;
        pixelFormat = TJPF_BGRA;
    
    else if (source_encoding.empty())
    
        // Autodetect based on image
        if(jpegColor == TJCS_GRAY)
        
        ret->data.resize(height*width);
        ret->step = width;
        ret->encoding = enc::MONO8;
        pixelFormat = TJPF_GRAY;
        
        else
        
        ret->data.resize(height*width*3);
        ret->step = width*3;
        ret->encoding = enc::RGB8;
        pixelFormat = TJPF_RGB;
        
    
    else
    
        ROS_WARN_THROTTLE(10.0, "Encountered a source encoding that is not supported by TurboJPEG: '%s'", source_encoding.c_str());
        return sensor_msgs::ImagePtr();
    

    if (tjDecompress2(tj_, src, data.size(), ret->data.data(), width, 0, height, pixelFormat, 0) != 0)
    
        ROS_WARN_THROTTLE(10.0, "Could not decompress data using TurboJPEG, falling back to OpenCV");
        return sensor_msgs::ImagePtr();
    
    return ret;

若该接口未能满足需求,可以参考image_transport里面其他图片格式的转换方式。

#####################
不积硅步,无以至千里
好记性不如烂笔头
感觉有点收获的话,麻烦点赞收藏哈

以上是关于ROS学习笔记52--图片从compressed格式转raw格式代码接口介绍的主要内容,如果未能解决你的问题,请参考以下文章

ROS从入门到放弃(学习笔记1)

ROS2学习笔记29--项目从ROS1迁移到ROS2的经验参考

ROS学习笔记三(理解ROS节点)

ROS学习笔记-导入并启动导航用的仿真地图

狂神说Java---java学习笔记(图片有些问题,有时间将图片从电脑上传上来)

狂神说Java---java学习笔记(图片有些问题,有时间将图片从电脑上传上来)