PCL教程指南-直通滤波器,体素滤波器,统计滤波器
详细解读官方文档代码同时,引出具体类中其他常用方法
1.直通滤波器
- 官方文档原文
- 顾名思义,直通滤波器指通过某一个单一条件进行直接限制,比如过滤出x大于10的点;
- 代码较简单,直接对关键点进行复述
// Create the filtering object
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
//**省略自行输入点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
//直通滤波对象
pcl::PassThrough<pcl::PointXYZ> pass;
//输入要过滤的点云指针
pass.setInputCloud (cloud);
//设置过滤条件的字段名,此为Z轴
pass.setFilterFieldName ("z");
//设置限制;z大于0小于1
pass.setFilterLimits (0.0, 1.0);
//pass.setFilterLimitsNegative (true); 反向 即如果我们想返回setFilterLimits (min, max)指定的间隔之外的数据,则设置为true。
//输出到cloud_filtered中
pass.filter (*cloud_filtered);
直通滤波器所在头文件为
#include <pcl/filters/passthrough.h>
2.体素滤波器
- 官方文档原文
- 将数据分割为一个个三维规则体,以每个小规则体内的点云数据质心为代表点,以此来实现下采样
- 代码包含读取与写入的新方法,注释解读官方代码
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
int
main (int argc, char** argv)
{
pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2 ());
pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());
// pcd读取器
pcl::PCDReader reader;
// 读取PCD文件
reader.read ("table_scene_lms400.pcd", *cloud); // Remember to download the file first!
std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height
<< " data points (" << pcl::getFieldsList (*cloud) << ")." << std::endl;
// 体素滤波器
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud (cloud);
//每个小规则体的长宽高大小
sor.setLeafSize (0.01f, 0.01f, 0.01f);
sor.filter (*cloud_filtered);
std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height
<< " data points (" << pcl::getFieldsList (*cloud_filtered) << ")." << std::endl;
//PCD写入器
pcl::PCDWriter writer;
writer.write ("table_scene_lms400_downsampled.pcd", *cloud_filtered,
Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), false);
return (0);
}
代码中部分解释:
- PCDReader 除了直接读取ASCII码的PCD,还能够通过
readHeader()和readBodyASCII()
分头文件和主体分别读取,还可分为ASCII和二进制两种形式读入,具体详见API文档,因各类方法参数有不同的重载方法,以下直接介参数最多的重载方法以此解释参数的意思(PCDWriter同理)
int pcl::PCDReader::read(
const std::string & file_name, //输入文件名
pcl::PCLPointCloud2 & cloud, //输出 此处亦可用泛型形式pcl::PointCloud< PointT>
Eigen::Vector4f & origin, //输出 传感器采集原点,若不存在则为NULL
Eigen::Quaternionf & orientation,//输出 传感器采集方向,若不存在则为NULL
int & pcd_version, //输出 pcd版本
const int offset = 0 //输入 读取头时,偏移量设置(特殊情况文件需要偏移才能读到数据)
)
int pcl::PCDWriter::write(
const std::string & file_name,//输入
const pcl::PCLPointCloud2::ConstPtr & cloud,//输入
const Eigen::Vector4f & origin = Eigen::Vector4f::Zero (),//输入
const Eigen::Quaternionf & orientation = Eigen::Quaternionf::Identity (),//输入
const bool binary = false //输入 false表示ASCII码,true表示二进制
)
- Eigen是C++的矩阵库,
Eigen::Vector4f
指4维float型向量,Eigen::Quaternionf`指float的四元数(四元数用于表示旋转,具体另查)
pcl::PCLPointCloud2
是一种定义更普遍性的数据类型,相比于泛型方式,此定义不会限制字段和数据,其中以vector方式存储 比如data可以是XYZ 也可以是XYZRGB 或者更多的值
3.统计滤波器
- 官方文档原文
- 遍历计算各测点与其邻域点的平均距离,以此为标量,假设为高斯正态分布,按标准差去除离群点
- 代码部分与之前重复,直接复述相关部分
// Create the filtering object
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
sor.setInputCloud (cloud);
sor.setMeanK (50); //计算平均距离时,设置邻域点数量
sor.setStddevMulThresh (1.0);//设置标准差阈值倍数
sor.filter (*cloud_filtered);
//
这些类中还有一些常用的方法,例如:
继承自pcl::FilterIndices< PointT >
类的方法
方法 | 作用 |
---|---|
setNegative(bool) | 设置反向输出 |
void setKeepOrganized (bool keep_organized) | 是否保持过滤点存在 |
void setUserFilterValue(float value) | 将过滤的点统一设置为一个值而不是删除,默认NAN |
void filter ( Indices & indices ) | 过滤后的结果以索引数组存储 |
其中Indices 是PCL封装的索引结构pcl::Indices
,非常常用,后续很多方法需要输出为PCL索引类型