0
$\begingroup$

Rosanswers logo

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 http://www.cplusplus.com/doc/tutorial/pointers/ 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.


Originally posted by Pufpastry on ROS Answers with karma: 3 on 2015-06-03

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by Wolf with karma: 7555 on 2015-06-03

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Pufpastry on 2015-06-03:
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!

$\endgroup$

Your Answer

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