ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

sensor_msgs::Image to sensor_msgs::ImagePtr

asked 2011-05-19 04:25:02 -0600

Yianni gravatar image

updated 2011-05-19 04:26:07 -0600

Hi, it might be simple so apologies for this post... but I can't find the solution.

I would like to get the sensor_msgs::Image::Ptr of a sensor_msgs::Image. Basically I would like to do the conversion of a sensor_msgs::Image to an cv IplImage using:

sensor_msgs::CvBridge bridge_;

which takes actually


as found in CvBridge.h (diamondback)

IplImage* imgMsgToCv(sensor_msgs::Image::ConstPtr rosimg, std::string desired_encoding = "passthrough")

and hence my failing.

Basically, I would like to do the following:

sensor_msgs::CvBridge bridge_;
sensor_msgs::Image image_msg = <to a sensor_msgs::Image>;
sensor_msgs::Image::Ptr rosimg;
missing statement??
bridge_.imgMsgToCv(rosimg, "bgr8");

Many thanks.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2011-05-19 12:03:25 -0600

updated 2011-05-19 13:20:31 -0600

It looks like you might misunderstand the way smart pointers work. You can look hear for an intro:

But in the mean time smart pointers can only be safely used to represent heap allocations (things created with new) If you try to get image_msg from your example into a smart pointer rosimg it will cause a segfault because it will be deleted twice: Once from going out of scope, and second by the smart pointer when ever it goes out of scope or is otherwise destroyed.

If you write your message callback to use const sensor_msgs::Image::Ptr & image_msg, ros will pass you the message wrapped in a smart pointer already , and you won't have to worry about it.

edit flag offensive delete link more


Thanks. Your answer helped.
Yianni gravatar image Yianni  ( 2011-05-19 19:41:36 -0600 )edit

answered 2011-05-19 05:24:54 -0600

mkoval gravatar image

I'm not sure that I entirely understand the question. If you want to convert a sensor_msgs::Image to a sensor_msgs::Ptr or sensor_msgs:ConsPtr, you only need to wrap it in a boost::shared_ptr:

sensor_msgs::Image::Ptr rosimg = boost::shared_ptr<sensor_msgs::Image>(image_msg);

Note that this is safe only if rosimg is heap-allocated. For example, you could initialize rosimg as:

sensor_msgs::Image::Ptr rosimg = boost::make_shared<sensor_msgs::Image>();
edit flag offensive delete link more


Thanks. Your answer helped.
Yianni gravatar image Yianni  ( 2011-05-19 19:41:59 -0600 )edit

answered 2011-05-19 04:43:01 -0600

raphael favier gravatar image


If the only thing you want to do is converting a ROS image message into openCV, why don't you follow this cv_bridge tutorial?

Here is the snippet that should interest you:

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

    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    cv::imshow(WINDOW, cv_ptr->image);

Or do you want to do something different? Hope it helps


edit flag offensive delete link more


Thanks. I already had a look on this, but on my callback the parameter is not const sensor_msgs::ImageConstPtr& msg...
Yianni gravatar image Yianni  ( 2011-05-19 19:43:14 -0600 )edit
Check the answer from Asomerville under. I think the easiest for you is to change the prototype of your callback as he explains.
raphael favier gravatar image raphael favier  ( 2011-05-20 00:16:01 -0600 )edit

Question Tools


Asked: 2011-05-19 04:25:02 -0600

Seen: 15,568 times

Last updated: May 19 '11