ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
To publish cv::Mat to ROS topic do tjhe following:
1) Include cv_bridge and sensor_msgs Image (You already did that=) )
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <sensor_msgs/Image.h>
2) Set up image publisher for sensor_msgs/Image somewhere:
ros::Publisher out_img_pub =nh.advertise<sensor_msgs::Image>("image_out", 2, false);
3) In your conde where you have filled your cv::Mat convert it to sensor_msgs/image and publish:
// assuming you have declared and filled cv::Mat your_mat with data in colorspace "bgr8"
cv_bridge::CvImage lo_img;
lo_img.encoding = "bgr8"; // or which enconding your data has
lo_img.header.stamp = ros::Time::now(); // or whatever timestamp suits here;
lo_img.header.frame_id = "whatever_frame"; // frame id as neededby you
lo_img.image = your_mat; // point cv_bridge to your object
// publishing data
out_img_pub.publish( lo_img.toImageMsg() );