Ros下使用OpenCV对图像进行处理

Ros中的图像信息是sensor_msgs/Image的格式,而OpenCV 中图像是cv:Mat格式。所以从Ros中获取的图像不能直接用OpenCV进行处理,需要进行格式转化。还好,Ros中提供了进行ROS和OpenCV之间图像信息格式转化的接口CvBridge

下面附上Ros官网提供的源码:

#include 
#include 
#include 
#include 
#include 
#include 

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;
}
本段代码源自:
http://wiki.ros.org/cv_bridge/Tutorials/UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages

下面来解释一下里面的代码

namespace cv_bridge {

class CvImage
{
public:
  std_msgs::Header header;
  std::string encoding;
  cv::Mat image;
};

typedef boost::shared_ptr CvImagePtr;
typedef boost::shared_ptr CvImageConstPtr;

}
cv_bridge里定义了类CvImage,类中包含了ros的头文件,OpenCV图像信息和编码。我们可以用这个类来进行两种图像格式的转换

cv_bridge里提供了以下两种函数来将ros中的sensor_msgs::image转换为Cvimage

// Case 1: Always copy, returning a mutable CvImage
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr& source,
                    const std::string& encoding = std::string());
CvImagePtr toCvCopy(const sensor_msgs::Image& source,
                    const std::string& encoding = std::string());

// Case 2: Share if possible, returning a const CvImage
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
                          const std::string& encoding = std::string());
CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
                          const boost::shared_ptr& tracked_object,
                          const std::string& encoding = std::string());
toCvCopy复制ROS消息中的图像数据,可以对返回的cvimage进行 自由修改

toCvShare用来进行数据共享,安全地共享ROS消息中包含的数据。也就是说如果想使用opencv进行图像处理的话,使用toCvCopy


使用toImageMsg()成员函数可以将opencv图像转换成ROS图像信息

class CvImage
{
  sensor_msgs::ImagePtr toImageMsg() const;

  // Overload mainly intended for aggregate messages that contain
  // a sensor_msgs::Image as a member.
  void toImageMsg(sensor_msgs::Image& ros_image) const;
};
如要进行图像处理时,直接对cv_ptr->image进行处理即可

如要对kinect2中获得的图像rgb信息进行处理,将节点订阅中的/camera/image_raw换成kinect2发布的信息即可






                                                



你可能感兴趣的:(ROS学习笔记,ROS,图像处理,opencv,kinect2)