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

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_;
bridge_.imgMsgToCv

which takes actually

sensor_msgs::Image::ConstPtr

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
1

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: http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/smart_ptr.htm

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

Comments

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

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

Comments

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

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

raphael favier gravatar image

Hello,

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;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    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);
    cv::waitKey(3);
}

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

Raph

edit flag offensive delete link more

Comments

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

Question Tools

Stats

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

Seen: 16,045 times

Last updated: May 19 '11