ROS 学习系列 -- image

mac2025-12-08  4

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 <ros/ros.h> #include <stdlib.h> #include <image_transport/image_transport.h> #include <opencv2/highgui/highgui.hpp> #include <cv_bridge/cv_bridge.h>

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’

最新回复(0)