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

cv_bridge: sensor_msgs::Image to sensor_msgs::ImageConstPtr

asked 2015-06-03 07:01:10 -0600

Pufpastry gravatar image

Hi there!

I've been having some real trouble with cv_bridge (and, I guess c++ in general); I'm really stumped as to how to solve this issue but I suspect it's down to a fundamental misunderstanding of c++.

I'm trying to write a very basic ros package which will capture a video from a webcam, publish it frame by frame to a topic, and then have a subscriber take the data and display the image. However, I am following an API which stresses that the message must be passed as a sensor_msgs/Image inside another message containing some other information.

message definition:

sensor_msgs/Image frame
uint8 camera_id

My publisher uses cv_bridge in the following manner:

cv::Mat cv_frame;
cap >> cv_frame; //I have a VideoCapture called cap.
vision_ros::img_msg imsg;
sensor_msgs::Image::Ptr ros_frame;
std_msgs::Header head;

ros_frame = cv_bridge::CvImage(head, "", cv_frame).toImageMsg();;

imsg.frame = *ros_frame;

Now, my understanding of c++ and pointers is limited (I reference constantly...) but, my aim here was to capture a new frame from the webcam convert it using cv_bridge (which leaves me with a sensor_msgs::Image::Ptr) and then add this to my img_msg as a sensor_msgs::Image by using the deference operator (*). It seems to work, but if it's wrong, please say something!

I then want my subscriber callback function to work along the lines of:

void streamCallback(vision_ros::img_msg)
    sensor_msgs::Image ros_frame = imsg.frame;
    cv::Mat cv_frame = <some conversion from sensor_msgs::Image to cv::Mat>

The way I feel it should be done (after reading the documentation) is to convert from sensor_msgs::Image to cv_bridge::CvImage and then extract the CvImage.image, property which should be of type cv::Mat.

However, all of the functions return a CvImagePtr for which I thought I could use the deference operator again to get the "data stored at...".

Finally, all of the functions to convert from a sensor_msgs::Image to cv_bridge::CvImagePtr must take a const sensor_msgs::ImageConstPtr as an argument.

So my question is, how do I get from my sensor_msgs::Image to a sesnor_msgs::ImageConstPtr?

I tried:

sensor_msgs::Image ros_frame;
sensor_msgs::ImageConstPtr ros_frame_ptr = &ros_frame;

to no avail.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-06-03 09:52:27 -0600

Wolf gravatar image

Your callback could look like:

void streamCallback(vision_ros::img_msg)
    cv::Mat cv_frame = cv_bridge::toCvCopy( imsg.frame, "mono8" /* or other encoding */ )->image;
edit flag offensive delete link more


Thank you! That fixed the cv_bridge based errors, now I just need to figure out why I can't get imshow to show anything... Thanks again!

Pufpastry gravatar image Pufpastry  ( 2015-06-03 11:10:05 -0600 )edit

Question Tools


Asked: 2015-06-03 07:01:10 -0600

Seen: 7,159 times

Last updated: Jun 03 '15