赞
踩
背景:某些情况下需要录图像数据的包,非常占空间和带宽,尤其对于一些工业相机图像一张好几兆,每秒30帧的话一份钟好几个G,这时候可以选择的订阅压缩图像,下面直接来个demo。
先订阅一个sensor::Image 消息,然后使用image_transport直接发布图像,会同时产生一个压缩image_compressed/compressed和不压缩的image_compressed图像话题,订阅时直接订阅压缩话题即可。
- #include "ros/ros.h"
- #include "sensor_msgs/Image.h"
- #include "sensor_msgs/CompressedImage.h"
- #include "sensor_msgs/image_encodings.h"
- #include <image_transport/image_transport.h>
- #include <cv_bridge/cv_bridge.h>
- #include <opencv2/opencv.hpp>
- #include <iostream>
- using namespace std;
- cv::Mat imgCallback;
- image_transport::Publisher image_pub;
- static void ImageCallback(const sensor_msgs::ImageConstPtr &msg)
- {
- try
- {
- cout<<"FLIR time:"<<msg->header.stamp<<endl;
- return;
- image_pub.publish(msg);
- cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);
- imgCallback = cv_ptr->image;
- cv::imshow("imgCallback",imgCallback);
- cv::waitKey(1);
- cout<<"cv_ptr: "<<cv_ptr->image.cols<<" h: "<<cv_ptr->image.rows<<endl;
- //std::cout<<"Image: "<<msg->width<<" x "<<msg->height<<std::endl;
- }
- catch (cv_bridge::Exception& e)
- {
- //ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
- //ROS_ERROR("Could not convert from '%s' to 'bgr8'.",msg->format.c_str());
- }
- //原文链接:https://blog.csdn.net/qq_30460905/article/details/80489841
- }
- int main(int argc, char **argv)
- {
- ros::init(argc, argv, "CompressedImage");
- ros::NodeHandle nh;
- image_transport::ImageTransport it(nh);
- image_transport::Subscriber image_sub;
- std::string image_topic = "usb_cam/image_raw";
- image_sub = it.subscribe(image_topic,10,ImageCallback);
- image_pub = it.advertise("image_compressed",1);
- ros::spin();
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
订阅压缩图像话题消息很简单,直接用ros::Publisher订阅即可
- #include "ros/ros.h"
- #include "sensor_msgs/CompressedImage.h"
- #include "sensor_msgs/image_encodings.h"
- #include <cv_bridge/cv_bridge.h>
- #include <opencv2/opencv.hpp>
- #include <iostream>
- using namespace std;
- cv::Mat imgCallback;
- static void ImageCallback(const sensor_msgs::CompressedImageConstPtr &msg)
- {
- try
- {
- cv_bridge::CvImagePtr cv_ptr_compressed = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);
- imgCallback = cv_ptr_compressed->image;
- cv::imshow("imgCallback",imgCallback);
- cv::waitKey(1);
- cout<<"cv_ptr_compressed: "<<cv_ptr_compressed->image.cols<<" h: "<<cv_ptr_compressed->image.rows<<endl;
- }
- catch (cv_bridge::Exception& e)
- {
- //ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
- }
- }
- int main(int argc, char **argv)
- {
- ros::init(argc, argv, "CompressedImage");
- ros::NodeHandle nh;
- ros::Subscriber image_sub;
- std::string image_topic = "image_compressed/compressed";
- image_sub = nh.subscribe(image_topic,10,ImageCallback);
-
- ros::Rate loop_rate(10);
- while (ros::ok())
- {
- ROS_INFO("ROS OK!");
- ros::spinOnce();
- loop_rate.sleep();
- }
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
CMakeLists.txt
- cmake_minimum_required(VERSION 2.8.3)
- project(compress_Image)
-
- ## Compile as C++11, supported in ROS Kinetic and newer
- # add_compile_options(-std=c++11)
- find_package(catkin REQUIRED COMPONENTS
- roscpp sensor_msgs cv_bridge image_transport
- )
- find_package(OpenCV 3.3 REQUIRED)
- include_directories(${OpenCV_INCLUDE_DIRS})
- link_directories(${OpenCV_LIBRARY_DIRS})
- catkin_package()
- include_directories(
- # include
- ${catkin_INCLUDE_DIRS}
- )
- add_executable(${PROJECT_NAME}_sub src/compress_sub.cpp)
-
- ## Specify libraries to link a library or executable target against
- target_link_libraries(${PROJECT_NAME}_sub
- ${catkin_LIBRARIES}
- ${OpenCV_LIBRARIES}
- )
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。