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

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

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

Kei Okada gravatar image

I was wondering about how to publish cv:Mat. The tutorial wiki 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");

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

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

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


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


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 -0500 )edit

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 -0500 )edit

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

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


I checked diamondback tab in cv_bridge Tutorials(, but I couldn't find the exact answer.
Kei Okada gravatar image Kei Okada  ( 2011-04-19 21:22:30 -0500 )edit

Question Tools

1 follower


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

Seen: 20,916 times

Last updated: Apr 20 '11