ROS 学习系列 -- image_transport 发布jpg图片文件流在image_view中看视频效果

https://www.it610.com/article/5477845.htm

目标:

   目录/home/camera 中保存了2000张照相机连续拍照的JPG文件,名字按数字排序 1.jpg  2.jpg  3.jpg  ......... 现在需要在ROS中建立一个node按频率发布所有照片,并可以在image_view中看视频效果。

   详细见链接:http://wiki.ros.org/image_transport/Tutorials/PublishingImages

首先安装image_transport,一般在安装ROS时都安装过了。执行

sudo apt-get install ros-indigo-image-transport

发布node的代码:

#include
#include
#include
#include
#include

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::Rate loop_rate(5);
for(int i = 0; i < 2000; i++)
{
if(!nh.ok())
break;
std::ostringstream stringStream;
stringStream << “/home/camero/” << i << “.jpg”;
cv::Mat image = cv::imread(stringStream.str(), CV_LOAD_IMAGE_COLOR);
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), “bgr8”, image).toImageMsg();

pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();

}
}

使用image_view 查看视频流

上面的node发布视频流到 /camera/image 上,但是image_transport同时还发布了其它的topic用来兼容image_view的功能,就是说上面的代码自动帮助发布了有压缩格式的视频流:

/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

使用下面的命令查看图片流效果:

rosrun image_view image_view image:=/camera/image

如果要查看压缩视频流效果,需要增加一个参数就可以了:

rosrun image_view image_view image:=/camera/image/ image_transport:=‘compressed’

你可能感兴趣的:(ROS 学习系列 -- image_transport 发布jpg图片文件流在image_view中看视频效果)