0
$\begingroup$

Rosanswers logo

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.


Originally posted by Yianni on ROS Answers with karma: 123 on 2011-05-19

Post score: 3

$\endgroup$

3 Answers 3

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Asomerville with karma: 2743 on 2011-05-19

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Yianni on 2011-05-19:
Thanks. Your answer helped.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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


Originally posted by raphael favier with karma: 1382 on 2011-05-19

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by raphael favier on 2011-05-20:
Check the answer from Asomerville under. I think the easiest for you is to change the prototype of your callback as he explains.

Comment by Yianni on 2011-05-19:
Thanks. I already had a look on this, but on my callback the parameter is not const sensor_msgs::ImageConstPtr& msg...

$\endgroup$
0
$\begingroup$

Rosanswers logo

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>();

Originally posted by mkoval with karma: 524 on 2011-05-19

This answer was NOT ACCEPTED on the original site

Post score: 4


Original comments

Comment by Yianni on 2011-05-19:
Thanks. Your answer helped.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.