Ros发布图像消息

ros的图像消息的格式为

sensor_msgs::ImagePtr

一般发送图像消息都是通过opencv来进行转换,ros提供了一个类cv_bridge来负责ros图像消息和opencv图像消息的转换
在OpenCV中,图像是以Mat矩阵的形式存储的,这与ROS定义的图像消息的格式有一定的区别,所以需要利用cv_bridge将这两种不相同的格式联系起来,如下图所示。
Ros发布图像消息_第1张图片
转换后的图像消息格式为

sensor_msgs::ImagePtr

opencv和cv_bridge转换的核心代码

cv::Mat img = imread("imagepath", IMREAD_COLOR);    
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", img).toImageMsg();

但在转换后的消息还不能直接用平时直接定义的node发出去,要使用image_transport进行转换,才能发出去

ros::NodeHandle nh;
 image_transport::ImageTransport it(nh);

  //topic name is /camera_front/image_color,the publish message queue size is 1.
  image_transport::Publisher pub = it.advertise("/camera_front/image_color", 1);
  pub.publish(msg);

但是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

配置CmakeList.txt文件
主要是要添加opencv的支持

find_package(OpenCV  REQUIRED)
include_directories(
 
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(node  ${catkin_LIBRARIES} ${OpenCV_LIBS} )

你可能感兴趣的:(ROS,ROS,openCV,图像转换,image_transport)