与订阅者有sensor_msgs/图像问题的 ROS 自定义消息



我有一个自定义消息 -

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 文档,则第一种情况下的第二个参数应该是指向包含图像的消息的指针。请尝试以下代码:

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的调用以及回调的签名。

最新更新