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

Storing sensor_msgs::Image from cv_bridge

asked 2013-03-07 06:59:33 -0500

Geropan gravatar image

Hello everyone,

I am having some trouble to store the result of cv_bridge::CvImage::toImageMsg() as a sensor_msgs::Image.

The reason why I want to do this is because the sensor_msgs::Image belongs to a custom message I have created which contains some other piece of information.

Is there any way to store a cv::Mat in a sensor_msgs::Image besides doing it "manually" in a for loop?

Thank you in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-26 19:15:10 -0500

DamienJadeDuff gravatar image

The signatures for the method (from CvImage) are:

void                    toImageMsg (sensor_msgs::Image &ros_image)   const
sensor_msgs::ImagePtr   toImageMsg ()                                const

So we use the first option. And note that the image message is passed by reference. So you construct your message, let's say it's my_msg, and let's say the image that is a part of it is called img. If your CvImage object is my_cv_img then you would call:


And you are done. Note that toImageMsg allocates the memory for the image data itself, as can be seen by looking at the code for it: cv_bridge.cpp

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-03-07 06:59:33 -0500

Seen: 1,134 times

Last updated: Sep 26 '13