01 Opencv系列(一)ROS和opencv图像数据的转换

ROS和opencv的使用关系

01基本工作原理

a. 为了便于ros与opencv联合使用,通过CvBridge库(ROS的一个库),提供ros与opencv间的接口;

b. CvBridge可以在cv_bridge包,在vision_opencv stack找到;

c. 各自图像格式及相互间的转换关系(ROS-sensor_msgs / Image传递图像格式/OpenCV cv :: Mat格式

02代码实现示例

这是一个侦听ROS图像消息主题的节点,将图像转换为cv :: Mat,在其上绘制一个圆圈并使用OpenCV显示图像。然后通过ROS重新发布图像。

a. 在package.xml和CMakeLists.xml中(或使用catkin_create_pkg时),添加以下依赖项:

 

sensor_msgs
cv_bridge
roscpp
std_msgs
image_transport

 

b. 在/ src文件夹中创建image_converter.cpp文件并添加以下内容:

#include <ros/ros.h>
#include <image_transport/image_transport.h>//使用image_transport在ROS中发布和订阅图像,并允许您订阅压缩图像流。
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
 
static const std::string OPENCV_WINDOW = "Image window";
 
class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;
 
public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/image_raw", 1,
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);
 
    cv::namedWindow(OPENCV_WINDOW);
  }
 
  ~ImageConverter()
  {
    cv::destroyWindow(OPENCV_WINDOW);
  }
 
  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }
 
    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
 
    // Update GUI Window
    cv::imshow(OPENCV_WINDOW, cv_ptr->image);
    cv::waitKey(3);
 
    // Output modified video stream
    image_pub_.publish(cv_ptr->toImageMsg());
  }
};
 
int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ImageConverter ic;
  ros::spin();
  return 0;
}

 感谢两位博主的无私分享,侵权请联系删除,参考链接:

1 https://blog.csdn.net/dengheCSDN/article/details/78983993

2 详细讲解分享,原文链接:https://junjun.blog.csdn.net/article/details/83720949

posted @ 2021-09-02 11:28  下山道士又出家  阅读(630)  评论(0编辑  收藏  举报