Ask Your Question

sensor_msgs::Image to sensor_msgs::ImagePtr

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

Yianni gravatar image

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

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 -0500

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

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 -0500 )edit

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

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 -0500 )edit

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

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 -0500 )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 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


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

Seen: 12,555 times

Last updated: May 19 '11