我有自定义消息 -
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>
。我似乎无法理解这一部分。
如何从此自定义消息中正确检索图像?
答案 0 :(得分:2)
您必须仔细查看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
的调用以及回调的签名。