Converting images between ROS and OpenCV using cv_bridge

In this section, we will see how to convert between the ROS image message (sensor_msgs/Image) and the OpenCV image data type (cv::Mat). The main ROS package used for this conversion is cv_bridge, which is part of the vision_opencv stack. The ROS library inside cv_bridge, called CvBridge, helps to perform this conversion. We can use the CvBridge library inside our code and perform the conversion. The following figure shows how the conversion is performed between ROS and OpenCV:

Figure 5: Converting images using CvBridge

Here, the CvBridge library acts as a bridge for converting the ROS ...

Get Mastering ROS for Robotics Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.