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

How to convert cv::Mat to sensor_msgs::Image::Ptr?

asked 2011-04-19 01:13:38 -0600

Kei Okada gravatar image

I was wondering about how to publish cv:Mat. The tutorial wiki http://www.ros.org/wiki/cv_bridge/Tut... didn't give me an answer and following code seems working, but I suppose there are a way to do this without using old CvBridge interface.

        IplImage out_msg = sal_float_image;
        sensor_msgs::CvBridge bridge_;
        sensor_msgs::Image::Ptr saliency_msg = bridge_.cvToImgMsg(&out_msg, "passthrough");

        saliency_img_pub.publish(saliency_msg);
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
16

answered 2011-04-20 09:55:27 -0600

Patrick Mihelich gravatar image

cv::Mat by itself does not contain all of the information that sensor_msgs::Image does, namely the encoding (e.g. bgr8 vs rgb8) and the header (time stamp and tf frame). So you create a cv_bridge::CvImage object to convert:

#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

cv_bridge::CvImage out_msg;
out_msg.header   = in_msg->header; // Same timestamp and tf frame as input image
out_msg.encoding = sensor_msgs::image_encodings::TYPE_32FC1; // Or whatever
out_msg.image    = sal_float_image; // Your cv::Mat

saliency_img_pub.publish(out_msg.toImageMsg());

It's a little verbose; in the future we could add constructors to CvImage to streamline this. Note that forgetting to set the header fields is a common bug when using the old CvBridge.

edit flag offensive delete link more

Comments

Thanks, this is exactly what I want to know. Need to update wiki?
Kei Okada gravatar image Kei Okada  ( 2011-04-22 13:20:02 -0600 )edit
3

Hi, but what if you don't have the input message? what information can you set to the header?

andrestoga gravatar image andrestoga  ( 2015-12-09 12:55:23 -0600 )edit
0

answered 2011-04-19 01:26:16 -0600

makokal gravatar image

The cv_bridge API changed alot in diamond back, as I see you want to work with cv::Mat, you might want to take a look at the API again and make sure the diamondback button on the wiki is selected. Here is a quick link cv_bridge Tutorials

edit flag offensive delete link more

Comments

1
I checked diamondback tab in cv_bridge Tutorials(http://www.ros.org/wiki/cv_bridge/Tutorials/UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImage)s, but I couldn't find the exact answer.
Kei Okada gravatar image Kei Okada  ( 2011-04-19 21:22:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2011-04-19 01:13:38 -0600

Seen: 21,229 times

Last updated: Apr 20 '11