ROS+OpenCV读取图像

ROS+OpenCV读取图像

新建一个freenect.launch文件,填入一下内容


	
	
		
		
		
		
		
		
		
		
		
		
	

在ros下创建工作空间

mkdir -p cv_ws/src
cd cv_ws/src
catkin_init_workspace
cd ..
catkin_make

在工作空间下创建程序包

cd src
catkin_create_pkg robot_vision roscpp std_msgs cv_bridge image_transport sensor_msgs
cd ..
创建程序包的一般格式是catkin_create_pkg  

在src/robot_vision/src/目录下创建getImage.cpp

#include //ros标准库头文件
#include //C++标准输入输出库
/*
  cv_bridge中包含CvBridge库
*/
#include 
/*
  ROS图象类型的编码函数
*/
#include 
/*
   image_transport 头文件用来在ROS系统中的话题上发布和订阅图象消息
*/
#include 
 
//OpenCV2标准头文件
#include
#include
#include
 
static const std::string INPUT = "Input"; //定义输入窗口名称
static const std::string OUTPUT = "Output"; //定义输出窗口名称
static const std::string DEPTH = "DEPTH"; //定义depth窗口名称

//定义一个转换的类
class Main
{
private:
    ros::NodeHandle nh_; //定义ROS句柄
    image_transport::ImageTransport it_; //定义一个image_transport实例
    image_transport::Subscriber image_sub_0,image_sub_1; //定义ROS图象接收器
    //image_transport::Publisher image_pub_; //定义ROS图象发布器
public:
    Main()
      :it_(nh_) //构造函数
    {
        image_sub_0 = it_.subscribe("/camera/rgb/image_raw", 1, &Main::convert_callback, this); //定义图象接受器,订阅话题是“camera/rgb/image_raw”
        image_sub_1 = it_.subscribe("/camera/depth_registered/image_raw", 1, &
        Main::convert_callback_depth, this); //定义图象接受器,订阅话题是“/camera/depth_registered/image_raw”
       
       // image_pub_ = it_.publishe("", 1); //定义图象发布器
        //初始化输入输出窗口
        cv::namedWindow(INPUT);
        cv::namedWindow(OUTPUT);
        cv::namedWindow(DEPTH);
    }
    ~Main() //析构函数
    {
         cv::destroyWindow(INPUT);
         cv::destroyWindow(OUTPUT);
         cv::destroyWindow(DEPTH);
    }
    /*
      rgb图像的转换会调函数
    */
    void convert_callback(const sensor_msgs::ImageConstPtr& msg) 
    {
        cv_bridge::CvImagePtr cv_ptr; // 声明一个CvImage指针的实例
 
        try
        {
            cv_ptr =  cv_bridge::toCvCopy(msg, "bgr8"); //将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
        }
        catch(cv_bridge::Exception& e)  //异常处理
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            return;
        }
 
        image_process(cv_ptr->image); //得到了cv::Mat类型的图象,在CvImage指针的image中,将结果传送给处理函数   
    }
    /*
      depth图像的转换会调函数
    */
    void convert_callback_depth(const sensor_msgs::ImageConstPtr& msg) 
    {
        cv_bridge::CvImagePtr cv_ptr; // 声明一个CvImage指针的实例
 
        try
        {
            cv_ptr =  cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1); //将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
        }
        catch(cv_bridge::Exception& e)  //异常处理
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            return;
        }
 
        imshow(DEPTH,cv_ptr->image); //得到了cv::Mat类型的图象,在CvImage指针的image中,将结果传送给处理函数   
    }
    /*
       这是图象处理的主要函数,一般会把图像处理的主要程序写在这个函数中。这里的例子只是一个彩色图象到灰度图象的转化
    */
    void image_process(cv::Mat img) 
    {
       cv::Mat img_out;
       cv::cvtColor(img, img_out, CV_RGB2GRAY);  //转换成灰度图象
       cv::imshow(INPUT, img);
       cv::imshow(OUTPUT, img_out);
       cv::waitKey(5);
    }
};
 
//主函数
int main(int argc, char** argv)
{
    ros::init(argc, argv, "RGB");
    Main obj;
    ros::spin();
}

  • 将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
    • cv_ptr = cv_bridge::toCvCopy(msg, “bgr8”);
    • cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1);
    • 第二个参数表示转换的类型
      • mono8: CV_8UC1, grayscale image
      • mono16: CV_16UC1, 16-bit grayscale image
      • bgr8: CV_8UC3, color image with blue-green-red color order
      • rgb8: CV_8UC3, color image with red-green-blue color order
      • bgra8: CV_8UC4, BGR color image with an alpha channel
      • rgba8: CV_8UC4, RGB color image with an alpha channel
    • cv_ptr->image转化成cv::Mat类型
  • ros图片路径
    • rgb图片:/camera/rgb/image_raw
    • 深度图片:/camera/depth_registered/image_raw

向src/robot_vision/CMakeLists.txt添加如下内容

FIND_PACKAGE(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(getImage src/getImage.cpp)
target_link_libraries(getImage ${catkin_LIBRARIES} ${OpenCV_LIBS})
add_dependencies(getImage robot_vision_generate_messages_cpp) 

编译

  • 返回根目录
  • 终端输入

catkin_make

运行

  • 将节点导入bash,在终端输入

source ./devel/setup.bash

  • 打开另一个终端进入之前创建的freenect.launch目录,输入

roslaunch freenect.launch

  • 在终端输入运行(rosrun 包名 节点名)

rosrun robot_vision getImage

参考博客

  • https://blog.csdn.net/qq_31918961/article/details/93092640
  • https://blog.csdn.net/u010925447/article/details/80033288
  • https://blog.csdn.net/danwuxie/article/details/86708805
  • https://blog.csdn.net/u013794793/article/details/79925491

你可能感兴趣的:(SLAM,kinect,opencv,slam)