作者: Sam (甄峰) sam_code@hotmail.com
0.
image_transport简介:
image_transport应该总被用在image订阅和发布上。它为低带宽压缩格式(compressed formats)image传输提供透明支持。例如:为JPEG/PNG压缩和视频流提供单独插件,为此类image提供传输(订阅和发布)。
image_transport应该总被用在image订阅和发布上。它为低带宽压缩格式(compressed formats)image传输提供透明支持。例如:为JPEG/PNG压缩和视频流提供单独插件,为此类image提供传输(订阅和发布)。
当我们基于Image工作时,我们常希望指定传输策略。例如使用压缩Image或视频流编码。
image_transport提供class和node提供支持位随意格式的各类传输。抽离了格式等复杂信息,所以用户只看到sensor_msgs/Image这个message.
指定格式的传输由插件提供,image_transport自身只提供“raw”传输。其它的格式支持依赖于其它子package.
image_transport_plugins.
1. image_transport
使用:
image_transport可以使用C++或Python接口。我们主要看看C++接口。
1.1: 下载image_transport源码:
$cd catkin_ws/src
1.2: 学习用法:
正常发布和订阅,是通过NodeHandle, 如下:
1 // Do not communicate images this way!
2 #include /ros.h>
3
4 void imageCallback(const sensor_msgs::ImageConstPtr& msg)
5 {
6 // ...
7 }
8
9 ros::NodeHandle nh;
10 ros::Subscriber sub = nh.subscribe("in_image_topic", 1, imageCallback);
11 ros::Publisher pub = nh.advertise<<SPAN class=ID style="BOX-SIZING: border-box; COLOR: rgb(0,0,0)">sensor_msgs::Image>("out_image_topic", 1);
而使用image_transport:
1 // Use the image_transport classes instead.
2 #include /ros.h>
3 #include /image_transport.h>
4
5 void imageCallback(const sensor_msgs::ImageConstPtr& msg)
6 {
7 // ...
8 }
9
10 ros::NodeHandle nh;
11 image_transport::ImageTransport it(nh);
12 image_transport::Subscriber sub = it.subscribe("in_image_base_topic", 1, imageCallback);
13 image_transport::Publisher pub = it.advertise("out_image_base_topic", 1);
image_transport 发布和订阅独特的ROS Topic为每一个可用的Transport。 与ROS
Publisher不同,它并非注册一个单独的Topic。 而是多个Topic,
Topic命名遵循一个标准规则。在接口中,我们只能指定Base Topic名。其它具体名字,由规则指定。
例如,上面发布了名为:out_image_base_topic地Topic。但这个名称只是Base Topic
Name。
而advertise会创建多个以base Topic
Name 为基础的Topic。 例如: /out_image_base_topic/compressed
.....
1.3:例子察看:
察看image_transport\tutorial目录,其中为例子代码。
首先看CMakeLists.txt.
add_executable(my_publisher src/my_publisher.cpp)
add_executable(my_subscriber src/my_subscriber.cpp)
add_library(resized_publisher src/manifest.cpp
src/resized_publisher.cpp src/resized_subscriber.cpp)
可以看到,例子中生成了一个库和两个可执行文件。
my_publisher.cpp:
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("camera/image", 1);
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("camera/image", 1);
cv::Mat image = cv::imread(argv[1],
CV_LOAD_IMAGE_COLOR);
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
ros::Rate loop_rate(5);
while (nh.ok()) {
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
while (nh.ok()) {
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
运行:
$roscore
$rosrun image_transport_tutorial my_publisher
/home/sam/image/image1.jpg
看效果:
$rosrun rqt_image_view rqt_image_view
选择不同的Topic, 可以看到图像。
$rostopic list
/camera/image
/camera/image/compressed
/camera/image/compressed/parameter_descriptions
/camera/image/compressed/parameter_updates
/camera/image/compressedDepth
/camera/image/compressedDepth/parameter_descriptions
/camera/image/compressedDepth/parameter_updates
/camera/image/theora
/camera/image/theora/parameter_descriptions
/camera/image/theora/parameter_updates
/camera/image/compressed
/camera/image/compressed/parameter_descriptions
/camera/image/compressed/parameter_updates
/camera/image/compressedDepth
/camera/image/compressedDepth/parameter_descriptions
/camera/image/compressedDepth/parameter_updates
/camera/image/theora
/camera/image/theora/parameter_descriptions
/camera/image/theora/parameter_updates
在usb_cam
Package中,也使用了image_transport.
只是它使用的接口是advertiseCamera()
相关API: