Rosanswers logo

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.

Originally posted by Geropan on ROS Answers with karma: 21 on 2013-03-07

Post score: 1


1 Answer 1


Rosanswers logo

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

Originally posted by DamienJadeDuff with karma: 399 on 2013-09-26

This answer was ACCEPTED on the original site

Post score: 2


Your Answer

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