Skip to Content
Mastering ROS for Robotics Programming - Second Edition
book

Mastering ROS for Robotics Programming - Second Edition

by Lentin Joseph, Jonathan Cacace
February 2018
Beginner
580 pages
13h 20m
English
Packt Publishing
Content preview from Mastering ROS for Robotics Programming - Second Edition

Converting OpenCV to ROS images using cv_bridge

This is an image callback function, and it basically converts the ROS image messages into the OpenCV cv::Mat type using the CvBridge APIs. The following is how we can convert ROS to OpenCV, and vice versa:

  void imageCb(const sensor_msgs::ImageConstPtr& msg) 
  { 
 
    cv_bridge::CvImagePtr cv_ptr; 
    namespace enc = sensor_msgs::image_encodings; 
 
    try 
    { 
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); 
    } 
    catch (cv_bridge::Exception& e) 
    { 
      ROS_ERROR("cv_bridge exception: %s", e.what()); 
      return; 
    } 

To start with CvBridge, we should start with creating an instance of a CvImage. Given next is the creation of the CvImage pointer:

 cv_bridge::CvImagePtr cv_ptr; 

The CvImage type is a class ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering ROS for Robotics Programming - Third Edition

Mastering ROS for Robotics Programming - Third Edition

Lentin Joseph, Jonathan Cacace
Effective Robotics Programming with ROS - Third Edition

Effective Robotics Programming with ROS - Third Edition

Luis Sanchez, Enrique Fernandez Perdomo, Anil Mahtani

Publisher Resources

ISBN: 9781788478953Supplemental Content