首先,我是参考这篇文章:
https://blog.csdn.net/u010925447/article/details/80033288
但是,消息转换并不成功:
cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
所以,我又改成了这样:
// ros 消息定义
cv_bridge::CvImagePtr frame, frame1;
frame = boost::make_shared< cv_bridge::CvImage >();
frame->encoding = sensor_msgs::image_encodings::MONO8;
frame->image = RefImg;
frame1 = boost::make_shared< cv_bridge::CvImage >();
frame1->encoding = sensor_msgs::image_encodings::BGR8;
// publish
frame1->image = pesudo_img;
if( not frame->image.empty() || not frame1->image.empty())
{
frame->header.stamp = ros::Time::now();
frame1->header.stamp = ros::Time::now();
pub.publish( frame->toImageMsg() );
pub1.publish( frame1->toImageMsg() );
}
// 订阅图像主题:原始图和深度图
image_transport::ImageTransport it(n);
image_transport::Subscriber sub2 = it.subscribe("camera/image", 1, imageCallback);
通过,上面操作才完成了图像消息发布与接收。
参考:
http://docs.ros.org/diamondback/api/cv_bridge/html/c++/classcv__bridge_1_1CvImage.html