image_transport包用于传输图片,程序以插件的形式给出,传输格式包括JPEG/PNG压缩图片格式和Thero视频流。
// Use the image_transport classes instead.
#include <ros/ros.h>
#include <image_transport/image_transport.h>
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
// ...
}
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe("in_image_base_topic", 1, imageCallback);
image_transport::Publisher pub = it.advertise("out_image_base_topic", 1);
image_transport Publishers
image_transport publishers的使用和ROS一样,会提供一系列传输选项(JPEG compression,streaming video.etc)。不同的subscribers可以使用不同的transports策略从相同的publisher请求图片。
C++: image_transport::Publisher (API), image_transport::CameraPublisher (API)
Published topics
image_transport publishers为每一个transport发布单独的ROS Topic。
原始图片sensor_msgs/Image发布base_topic。如果有额外的插件使用,他们发布消息到base topic的子话题,格式为<base topic>/<transport name>
Parameters
image_transport publishers没有独立的参数,但是可以通过Parameter Server实现reconfigure。
可以通过dynamic_reconfigure包来调整参数
参数格式为:
<base topic>/<transport name><parameter name>
例如:
/camera/image/compressed/jpeg_quality
image_transport Subscribers
C++: image_transport::Subscriber (API), image_transport::CameraSubscriber (API)
node
$ rosrun image_transport republish [in_transport] in:=<in_base_topic> [out_transport] out:=<out_base_topic>
in_transport和out_transport是图片传输格式:raw 、compressed 和thero,对应原始图片格式、压缩图片格式和视频流格式。
假设我们使用theora视频流传输格式来发布机器人的图像。我们有几个节点收听图像主题。为了不让每个节点都单独将视频流转换为原始图片格式,浪费资源,使用image_transport包的republish节点将视频流式转换为sensor_msgs/image messages格式,重新发布到topic:
$ rosrun image_transport republish theora in:=camera/image raw out:=camera/image_decompressed
opencv编解码函数
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
std::string image_name = "path/to/.jpg";
std::ifstream file(image_name.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
std::string buffer(size, ' ');
file.read(&buffer[0], size);
file.close();
//解码
std::vector<char> vec_data(&buffer[0], &buffer[0] + size);
cv::Mat mat = cv::imdecode(vec_data, CV_LOAD_IMAGE_COLOR);
//编码
std::vector<uchar> buf;
cv::imencode(".jpg", mat, buf);
参考资料
image_transport-ROS-wiki
https://blog.csdn.net/fengbingchun/article/details/60780232