
Effective Robotics Programming with ROS
By :

ROS provide the sensor_msgs::Image
message to send images between nodes. However, we usually need a data type or object to manipulate the images in order to do some useful work. The most common library for that is OpenCV, so ROS offers a bridge class to transform ROS images back and forth from OpenCV.
If we have an OpenCV image, that is, cv::Mat
image, we need the cv_bridge
library to convert it into a ROS image message and publish it. We have the option to share or copy the image with CvShare
or CvCopy
, respectively. However, if possible, it is easier to use the OpenCV image field inside the CvImage
class provided bycv_bridge
. That is exactly what we do in the camera driver as a pointer:
cv_bridge::CvImagePtr frame;
Being a pointer, we initialize it in the following way:
frame = boost::make_shared<cv_bridge::CvImage>();
If we know the image encoding beforehand, we can use the following code:
frame->encoding = sensor_msgs::image_encodings::BGR8;
Later, we set the OpenCV image...