前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在ROS中使用PCL(2)

如何在ROS中使用PCL(2)

作者头像
点云PCL博主
发布2019-07-31 14:38:16
1.9K0
发布2019-07-31 14:38:16
举报
文章被收录于专栏:点云PCL点云PCL

记录关于我们运行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

代码语言:javascript
复制
 查看图像: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

代码语言:javascript
复制
例如: rosrun pcl_ros pointcloud_to_pcd input:=/velodyne/pointcloud2

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

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

代码语言:javascript
复制
/********************关于使用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);
}intmain (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发布的点云数据,同时你也可以指定点云的数据

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 点云PCL 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档