ROS无法直接进行图像处理,需要借助于opencv,要使用cv_bridge把ROS 的图像数据格式转为Opencv可以使用的数据格式。即是一个提供ROS和OpenCV库提供之间的接口的开发包。然后可以将opencv处理好的图像再转换回ros中的数据格式。
包含的头文件如下:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
从主函数开始:
int main(int argc, char** argv )
{
//初始化节点
ros::init(argc, argv, "simple_grasping_vision_detection");
//创建节点句柄
ros::NodeHandle n_;
//输出等待2s..
ROS_INFO_STREAM("Waiting for two seconds..");
ros::WallDuration(2.0).sleep();
float length = 0.3;
float breadth = 0.3;
//创建一个VisionManager类的对象
VisionManager vm(n_, length, breadth);
while (ros::ok())
{
// Process image callback
ros::spinOnce();
ros::WallDuration(2.0).sleep();
}
return 0;
}
首先初始化节点,节点名为simple_grasping_vision_detection,创建一个VisionManager类的对象vm,然后再循环中不断的进行处理。
看一下类的构造函数:
VisionManager::VisionManager(ros::NodeHandle n_, float length, float breadth) : it_(n_)
{
this->table_length = length;
this->table_breadth = breadth;
// Subscribe to input video feed and publish object location
image_sub_ = it_.subscribe("/probot_anno/camera/image_raw", 1, &VisionManager::imageCb, this);
image1_pub_ = it_.advertise("/table_detect", 1);
image2_pub_ = it_.advertise("/object_detect", 1);
}
构造函数输入有三个形参,VisionManager::VisionManager(ros::NodeHandle n_, float length, float breadth):
- ros::NodeHandle n_ 是节点句柄;
- float length 是
- float breadth 是
然后订阅了摄像头发布信息的话题,image_sub_ = it_.subscribe("/probot_anno/camera/image_raw", 1, &VisionManager::imageCb, this);
发布两个话题,一个是检测到的桌面,image1_pub_ = it_.advertise("/table_detect", 1);另外一个是检测到的物体,image2_pub_ = it_.advertise("/object_detect", 1);
当订阅者