Search code examples
imagecompilationros

ROS ImagePtr and Image Compilation Confusion


I am trying to create a publish a custom message that has two images. Here is the custom message file:

Header header
sensor_msgs/Image leftimage
sensor_msgs/Image rightimage

And here is the file where I try to publish the images from my stereo camera over ROS using my custom message:

sensor_msgs::ImagePtr leftmsg;
sensor_msgs::ImagePtr rightmsg;
leftmsg = cv_bridge::CvImage(std_msgs::Header(), "mono8", leftframe).toImageMsg();
rightmsg = cv_bridge::CvImage(std_msgs::Header(), "mono8", rightframe).toImageMsg();
stereo.leftimage = leftmsg;
stereo.rightimage = rightmsg;
stereo_pub.publish(stereo);

However, I am getting the following compilation error when I catkin_make:

    error: no match for ‘operator=’ (operand types are ‘duo_cam::Stereo_<std::allocator<void> >::_leftimage_type {aka sensor_msgs::Image_<std::allocator<void> >}’ and ‘sensor_msgs::ImagePtr {aka boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > >}’)
            stereo.leftimage = leftmsg;
                             ^
    note: candidate is:
    In file included from /opt/ros/indigo/include/image_transport/publisher.h:39:0,
             from /opt/ros/indigo/include/image_transport/image_transport.h:38,
    sensor_msgs::Image_<std::allocator<void> >& sensor_msgs::Image_<std::allocator<void> >::operator=(const sensor_msgs::Image_<std::allocator<void> >&)
     struct Image_
            ^

How do I correct this? I already am using the toImageMsg() function which I see online makes the ImagePtr into an Image, so not sure what is going wrong.


Solution

  • According to the documentation the signature of toImageMsg is

    sensor_msgs::ImagePtr toImageMsg() const;
    

    that is it does not convert the ImagePtr to Image (CvImage is used to convert OpenCV images to ROS images). This can also be seen by the fact that you declared leftmsg and rightmsg as ImagePtr; if toImageMsg would return Image, this would not work.

    To get the Image of a ImagePtr, just dereference the pointer. The following should work:

    stereo.leftimage = *leftmsg;
    stereo.rightimage = *rightmsg;