ROS 自定义消息 sensor_msgs/Image 订户问题

ROS custom message with sensor_msgs/Image issue with subscriber

我有一条自定义消息 -

sensor_msgs/Image im
float32 age
string name

我可以成功地为这条消息写一个发布者,看起来 运行 没问题。但是,我的订阅者有问题。

#include <ros/ros.h>

#include <custom_msg/MyString.h>
#include <custom_msg/MyImage.h>

#include <image_transport/image_transport.h>
#include <sensor_msgs/Image.h>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>


void custom_image_rcvd( const custom_msg::MyImage& msg )
{
    ROS_INFO_STREAM( "msg::: Name:"<< msg.name << " Age:"<< msg.age );

    cv::Mat im =  cv_bridge::toCvShare(  msg.im, "bgr8" )->image ;
    cv::imshow("viewz", im );
    cv::waitKey(30);
}

int main( int argc, char ** argv )
{
    ros::init(argc, argv, "custom_subscriber");
    ros::NodeHandle nh;

    ros::Subscriber sub2 = nh.subscribe( "custom_image", 2, custom_image_rcvd );

    ros::spin();
}

当我尝试 catkin_make 时,出现以下错误。

/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/subsc.cpp: In function ‘void custom_image_rcvd(const MyImage&)’:
/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/subsc.cpp:22:57: error: no matching function for call to ‘toCvShare(const _im_type&, const char [5])’
/home/eeuser/ros_workspaces/HeloRosProject/src/custom_msg/subsc.cpp:22:57: note: candidates are:
/opt/ros/hydro/include/cv_bridge/cv_bridge.h:198:17: note: cv_bridge::CvImageConstPtr cv_bridge::toCvShare(const Image&, const boost::shared_ptr<const void>&, const string&)
/opt/ros/hydro/include/cv_bridge/cv_bridge.h:198:17: note:   no known conversion for argument 2 from ‘const char [5]’ to ‘const boost::shared_ptr<const void>&’
/opt/ros/hydro/include/cv_bridge/cv_bridge.h:171:17: note: cv_bridge::CvImageConstPtr cv_bridge::toCvShare(const ImageConstPtr&, const string&)
/opt/ros/hydro/include/cv_bridge/cv_bridge.h:171:17: note:   no known conversion for argument 1 from ‘const _im_type {aka const sensor_msgs::Image_<std::allocator<void> >}’ to ‘const ImageConstPtr& {aka const boost::shared_ptr<const sensor_msgs::Image_<std::allocator<void> > >&}’
make[2]: *** [custom_msg/CMakeFiles/subscribe.dir/subsc.cpp.o] Error 1
make[1]: *** [custom_msg/CMakeFiles/subscribe.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed

我能确定的是 msg.im 又名 _im_type 类型。 sensor_msgs::Image_<ContainerAllocator>。我似乎无法理解这部分。

如何从此自定义消息中正确检索我的图像?

你得仔细看看toCvShare的签名。从报错信息可以看出,这个函数有两个重载:

cv_bridge::CvImageConstPtr cv_bridge::toCvShare(const Image&, const boost::shared_ptr<const void>&, const string&)

cv_bridge::CvImageConstPtr cv_bridge::toCvShare(const ImageConstPtr&, const string&)

所以该函数要么需要一个 Image 加上一个指向某个对象的指针(第一种情况),要么需要一个 ImageConstPtr(第二种情况)。但是,您只传递了 Image,因此这两个选项都不匹配。

如果我对 API documentation 的理解正确,第一种情况下的第二个参数应该是指向包含图像的消息的指针。试试下面的代码:

void custom_image_rcvd(const custom_msg::MyImageConstPtr& msg)
{
    ROS_INFO_STREAM("msg::: Name:" << msg->name << " Age:" << msg->age);

    cv::Mat im =  cv_bridge::toCvShare(msg->im, msg, "bgr8")->image;
    cv::imshow("viewz", im);
    cv::waitKey(30);
}

请注意,我更改了 toCvShare 的调用以及回调的签名。