ROS ImagePtr与图片编译混淆

ROS ImagePtr and Image Compilation Confusion

我正在尝试创建一个发布具有两个图像的自定义消息。这是自定义消息文件:

Header header
sensor_msgs/Image leftimage
sensor_msgs/Image rightimage

这是我尝试使用我的自定义消息通过 ROS 从我的立体相机发布图像的文件:

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

但是,当我 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_
            ^

我该如何纠正这个问题?我已经在使用我在网上看到的 toImageMsg() 函数将 ImagePtr 变成一个图像,所以不确定出了什么问题。非常感谢任何帮助!

根据the documentation toImageMsg 的签名是

sensor_msgs::ImagePtr toImageMsg() const;

也就是说它ImagePtr转换为ImageCvImage用于将OpenCV图像转换为ROS图像)。您将 leftmsgrightmsg 声明为 ImagePtr 也可以看出这一点;如果 toImageMsg 会 return Image,这将不起作用。

要获取 ImagePtrImage,只需取消引用指针即可。以下应该有效:

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