// 发布
image_transport::ImageTransport it(n);
image_transport::Publisher pub = it.advertise("video_image", 1);
sensor_msgs::ImagePtr msg;
//显示
cv::imshow( "video", cv_bridge::toCvShare(msg, "bgr8")->image );
//获得Mat类型图像数据
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8)->image; //将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
cv_bridge::toCvShare(msg, “bgr8”)->image表示的是一副图像,它将ROS中的消息msg通过cv_bridge的toCvShare函数转换成OpenCV的图像格式,也是按照bgr8进行编码,结果是一个指针,加上->image就是图像了。这句话后面一定要加上cv::waitKey(30); 要不然无法显示接收到的图像。
// 订阅
image_transport::Subscriber sub = it.subscribe( "video_image", 1, imageCalllback );
//在需要使用OpenCV的时候使用cv_bridge将消息转化为Mat类型,由此进行OpenCV的处理,处理结束之后如果还需要处理之后的数据。
//利用cv_bridge将Mat转化为sensor_msgs/Image即可
msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
//OpenCV格式的图像转化为ROS中的消息,需要借助cv_bridge来实现,std_msgs::Header()表示的是一些默认的头信息,"bgr8"是指需要编码的文件格式,frame则是需要转换的图像,msg就是转换之后的消息了。
(1) long int count_=0;
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
char base_name[256]; //图像保存
sprintf(base_name,"/home/zhy/catkin_ws_sliding_lane/image/%04ld.jpg",count_++);
cv::imwrite(base_name,cv_ptr->image);
(1) int chess_num =0;
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
char path[15];
sprintf(path, "%s%d%s", "chessbord", chess_num++, ".JPG");
imwrite(path,cv_ptr->image);
cd ~
mkdir Example
cd ~/.ros
mv ~/.ros/*.jpg ~/Example
<launch>
<node pkg="rosbag" type="play" name="rosbag" required="true" args="/home/zhy/catkin_ws/2020-01-20-14-39-50.bag"/>
<node name="extract" pkg="image_view" type="extract_images" respawn="false" required="true" output="screen" cwd="ROS_HOME">
<remap from="image" to="pylon_camera_node/image_raw"/>
<param name="sec_per_frame" value="0.03"/>
</node>
</launch>
rosrun image_view extract_images _sec_per_frame:=0.01 image:=/pylon_camera_node/image_raw
压缩bag文件只能用cv::Mat image = cv::imdecode(cv::Mat(msg->data),1)转换
jpeg2yuv -I p -f 15 -j chessbord%03d.JPG -b 103 > tmp.mp4
//其中-f 15 表示频率, -b 1 表示起始图片的编号。