zoukankan      html  css  js  c++  java
  • 如何在ROS中使用PCL(2)

    记录关于我们运行roslaunch openni_launch openni.launch  命令时生成的话题以及这些话题的数据类型便于后期的处理,只有知道它们的数据结构,才能很好的对数据进行处理,我们观察到使用rostopic list的所有话题的列表,当然其中也有一些不经常使用的话题类型,比如下面这些话题是我们经常使用的
    /camera/depth/image
    /camera/depth/image_raw
    /camera/depth/points
    /camera/ir/image_raw
    /camera/rgb/image_color
    /camera/rgb/image_raw

    发布的话题:

    image_raw (sensor_msgs/Image) : 未处理的原始图像

    使用命令查看sensor_msgs/Image的数据

    camera_info (sensor_msgs/CameraInfo):包含了相机标定配置以及相关数据

    介绍几个ROS节点运行的几种工具。他们的作用是ROS格式点云或包与点云数据(PCD)文件格式之间的相互转换。

    (1)bag_to_pcd

    用法:rosrun pcl_ros bag_to_pcd <input_file.bag> <topic> <output_directory>

    读取一个包文件,保存所有ROS点云消息在指定的PCD文件中。

    (2)convert_pcd_to_image

    用法:rosrun pcl_ros convert_pcd_to_image <cloud.pcd>

    加载一个PCD文件,将其作为ROS图像消息每秒中发布五次。

    (3) convert_pointcloud_to_image

    用法:rosrun pcl_ros convert_pointcloud_to_image input:=/my_cloud output:=/my_image

     查看图像:rosrun image_view image_view image:=/my_image

    订阅一个ROS的点云的话题并以图像的信息发布出去。
    (4)pcd_to_pointcloud

    用法:rosrun pcl_ros pcd_to_pointcloud <file.pcd> [ <interval> ]

    • <file.pcd> is the (required) file name to read.

    • <interval> is the (optional) number of seconds to sleep between messages. If <interval> is zero or not specified the message is published once.

    加载一个PCD文件,发布一次或多次作为ROS点云消息
    (5)pointcloud_to_pcd

    例如: rosrun pcl_ros pointcloud_to_pcd input:=/velodyne/pointcloud2

    订阅一个ROS的话题和保存为点云PCD文件。每个消息被保存到一个单独的文件,名称是由一个可自定义的前缀参数,ROS时间的消息,和以PCD扩展的文件。

    那么我们使用一个简单的例子来实现在ROS中进行平面的分割,同时注意到使用的数据转换的使用

    /**************************************************************************
    关于使用pcl/PointCloud<T>的举例应用。这一类型的数据格式是PCL库中定义的一种数据格式
    这里面使用了两次数据转换从 sensor_msgs/PointCloud2 到 pcl/PointCloud<T> 和 
                           从 pcl::ModelCoefficients 到 pcl_msgs::ModelCoefficients.
    ************************************************************************/
    #include <iostream>
    //ROS
    #include <ros/ros.h>
    // PCL specific includes
    #include <sensor_msgs/PointCloud2.h>
    #include <pcl_conversions/pcl_conversions.h>
    #include <pcl/ros/conversions.h>
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    
    #include <pcl/io/pcd_io.h>
    
    //关于平面分割的头文件
    #include <pcl/sample_consensus/model_types.h>   //分割模型的头文件
    #include <pcl/sample_consensus/method_types.h>   //采样一致性的方法
    #include <pcl/segmentation/sac_segmentation.h>  //ransac分割法
    
    ros::Publisher pub;
    
    void 
    cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
    {
      // 将点云格式为sensor_msgs/PointCloud2 格式转为 pcl/PointCloud
      pcl::PointCloud<pcl::PointXYZ> cloud;
      pcl::fromROSMsg (*input, cloud);   //关键的一句数据的转换
    
      pcl::ModelCoefficients coefficients;   //申明模型的参数
      pcl::PointIndices inliers;             //申明存储模型的内点的索引
      // 创建一个分割方法
      pcl::SACSegmentation<pcl::PointXYZ> seg;
      // 这一句可以选择最优化参数的因子
      seg.setOptimizeCoefficients (true);
      // 以下都是强制性的需要设置的
      seg.setModelType (pcl::SACMODEL_PLANE);   //平面模型
      seg.setMethodType (pcl::SAC_RANSAC);    //分割平面模型所使用的分割方法
      seg.setDistanceThreshold (0.01);        //设置最小的阀值距离
    
      seg.setInputCloud (cloud.makeShared ());   //设置输入的点云
      seg.segment (inliers, coefficients);       //cloud.makeShared() 创建一个 boost shared_ptr
      
    
     // pcl_msgs::fromROSMsg(const sensor_msgs::PointCloud2 &, pcl::PointCloud<T>&);  
      //pcl::io::savePCDFileASCII("test_pcd.pcd",cloud);
    
      // 把提取出来的内点形成的平面模型的参数发布出去
      pcl_msgs::ModelCoefficients ros_coefficients;
      pcl_conversions::fromPCL(coefficients, ros_coefficients);
      pub.publish (ros_coefficients);
    }
    
    int
    main (int argc, char** argv)
    {
      // Initialize ROS
      ros::init (argc, argv, "my_pcl_tutorial");
      ros::NodeHandle nh;
    
      // Create a ROS subscriber for the input point cloud
      ros::Subscriber sub = nh.subscribe ("input", 1, cloud_cb);
    
      // Create a ROS publisher for the output model coefficients
      pub = nh.advertise<pcl_msgs::ModelCoefficients> ("output", 1);
    
      // Spin
      ros::spin ();
    }

    在这里我们的input就是要订阅的话题/camera/depth/points

    我们在rosrun 的时候注明input:=/camera/depth/points的这样就可以使用kienct发布的点云数据,同时你也可以指定点云的数据

  • 相关阅读:
    IOS照相
    起学习iOS开发专用词汇
    django[post与get测试]
    起名字好难啊!(初识Django)
    MTV模型
    Django安装以及介绍
    数据库操作
    数据库其它操作
    数据库经典习题,
    数据库基本操作
  • 原文地址:https://www.cnblogs.com/li-yao7758258/p/6653961.html
Copyright © 2011-2022 走看看