pcl--第一节 Filters

官方例子在这里,本人使用的pcl1.12.1版本,win11,直接从github下载编译好的版本,使用vs打开cmake,之所以使用cmake,原因是环境配置方便,vs本身配置环境比较麻烦,所以为了方便使用cmake进行学习,这里参考了点云库pcl学习教程以及一些博客和官方例子进行学习,同时呢,笔者不会直接从背景、io、读取点云啥的介绍,这个东西,在实际使用时在查找或者学习也不晚,因此笔者会直接从涉及点云算法方面入手开始

背景介绍

下图给出了噪声消除的示例。由于测量误差,某些数据集会出现大量阴影点。这使得局部点云 3D 要素的估计变得复杂。其中一些异常值可以通过对每个点的邻域执行统计分析并修剪不符合特定条件的邻域来过滤。PCL 中的稀疏异常值去除实现基于对输入数据集中点到相邻距离分布的计算。对于每个点,将计算从该点到其所有相邻点的平均距离。通过假设生成的分布是具有平均值和标准差的高斯分布,则平均距离超出由全局距离平均值和标准差定义的区间之外的所有点都可以被视为异常值并从数据集中修剪。

pcl--第一节 Filters_第1张图片

PCL中总结了几种需要进行点云滤波处理的情况,这几种情况分别如下:

  1. 点云数据密度不规则需要平滑。
  2. 因为遮挡等问题造成离群点需要去除。
  3. 大量数据需要进行下采样(Downsample)
  4. 噪声数据需要去除

对应的方法如下

  1.  按具体给定的规则限制过滤去除点

  2.  通过常用滤波算法修改点的部分属性。

  3.    对数据进行下采样

 

直通滤波器PassThrough

官方介绍很简单,但是filters这节提供了很多东西,需要我们自己去学习,我这边先把官方代码奉上,还有一点就是官方代码不适合高版本,这里也进行了修改和测试

#include
#include
#include

int main()
{
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
	cloud->width = 5;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);

	for (auto& point : *cloud)
	{
		point.x =  0.5+rand() / (RAND_MAX + 1.0f);
		point.y =  0.5+rand() / (RAND_MAX + 1.0f);
		point.z =  0.5+rand() / (RAND_MAX + 1.0f);
	}

	std::cerr << "Cloud before filtering: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;

	pcl::PassThrough pass;
	pass.setInputCloud(cloud);
	pass.setFilterFieldName("z");
	pass.setFilterLimits(0.0, 1.0);
	pass.setFilterLimitsNegative(true);
	pass.filter(*cloud_filtered);

	std::cerr << "Cloud after filtering: " << std::endl;
	for (const auto& point : *cloud_filtered)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;

	return (0);

}
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(test)
SET(EXECUTABLE_OUTPUT_PATH "D:\\project_prj\\pcl\\run")
find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (test  "passthrough.cpp")
target_link_libraries (test ${PCL_LIBRARIES})

把可执行文件都放到run里,需要把需要的dll都复制到这里才可以运行。

VoxelGrid滤波进行数据下采样

使用体素化网格方法实现下采样,即减少点的数量减少点云数据,并同时保持点云的形状特征,在提高配准、曲面重建、形状识别等算法速度中非常实用。PCL实现的VoxelGrid类通过输人的点云数据创建一个三维体素栅格(可把体素栅格想象为微小的空间三维立方体的集合),然后在每个体素(即,三维立方体)内用体素中所有点的重心来近似显示体素中其他点,这样该体素就内所有点就用一个重心点最终表示,对于所有体素处理后得到过滤后的点云。这种方法比用体素中心来逼近的方法更慢,但它对于采样点对应曲面的表示更为准确。

#include
#include
#include
#include
#include
#include 
int main()
{
	// 体素滤波,降采样
	pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
	pcl::PCLPointCloud2::Ptr cloud_filtered(new pcl::PCLPointCloud2());

	//创建pcd读取器
	pcl::PCDReader reader;
	reader.read("D:\\install\\pcl_1.13.1_data_pcd\\tutorials\\table_scene_lms400.pcd", *cloud);
	std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height
		<< " data points (" << pcl::getFieldsList(*cloud) << ")." << std::endl;
	// 创建滤波器对象
	pcl::VoxelGrid 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;
	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_downsampled.pcd", *cloud_filtered,
		Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false
	);

	
	return (0);

}

使用下面的命令在cmd中执行可以看到滤波前和滤波后的可视化

pcl_viewer.exe -multiview 1 .\table_scene_lms400.pcd .\table_scene_lms400_downsampled.pcd

pcl--第一节 Filters_第2张图片 

使用StatisticalOutlierRemoval滤波器移除离群点

学习如何使用统计分析技术,从一个点云数据集中移除测量噪声点(也就是离群点)。

背景知识:

激光扫描通常会产生密度不均匀的点云数据集。另外,测量中的误差会产生稀疏的离群点,使效果更糟。估计局部点云特征(例如采样点处法向量或曲率变化率)的运算很复杂,这会导致错误的数值,反过来有可能导致点云的配准等后期处理失败。以下方法可以解决其中部分问题:对每个点的邻域进行一个统计分析,并修剪掉那些不符合一定标准的点。我们的稀疏离群点移除方法基于在输入数据中对点到临近点的距离分布的计算。对每个点,我们计算它到它的所有临近点的平均距离。假设得到的结果是一个高斯分布,其形状由均值和标准差决定,平均距离在标准范围(由全局距离平均值和方差定义)之外的点,可被定义为离群点并可从数据集中去除掉。

pcl--第一节 Filters_第3张图片

 

#include
#include
#include
#include
#include
#include 
#include 
int main()
{
	
	// 统计分析滤波
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);

	// Fill in the cloud data
	pcl::PCDReader reader;
	// Replace the path below with the path where you saved your file
	reader.read("table_scene_lms400.pcd", *cloud);

	std::cerr << "Cloud before filtering: " << std::endl;
	std::cerr << *cloud << std::endl;
	// 创建统计滤波
	pcl::StatisticalOutlierRemoval sor;
	sor.setInputCloud(cloud);
	sor.setMeanK(50);// 使用临近50个点做统计
	sor.setStddevMulThresh(1.0); // 设置标准差倍数为1,一个点的距离超出平均距离的一个标准差以上认为是离群点
	sor.filter(*cloud_filtered);

	std::cerr << "Cloud after filtering: " << std::endl;
	std::cerr << *cloud_filtered << std::endl;

	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_inliers.pcd", *cloud_filtered, false);

	sor.setNegative(true);
	sor.filter(*cloud_filtered);
	writer.write("table_scene_lms400_outliers.pcd", *cloud_filtered, false);
return 0
}
pcl_viewer.exe -multiview 1 .\table_scene_lms400.pcd .\table_scene_lms400_inliers.pcd .\table_scene_lms400_outliers.pcd

pcl--第一节 Filters_第4张图片

使用参数化模型投影点云project_inliers

学习如何将点投影到一个参数化模型上(例如平面或球等)。参数化模型通过一组参数来设定,对于平面来说使用其等式形式a+by+cz+d=0,在PCL中有特意存储常见模型系数的数据结构。

#include
#include
#include
#include
#include
#include 
#include 
#include 
#include 
int main()
{
	// 使用参数化模型投影点云
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_projected(new pcl::PointCloud);

	// Fill in the cloud data
	cloud->width = 5;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);

	for (auto& point : *cloud)
	{
		point.x = 1024 * rand() / (RAND_MAX + 1.0f);
		point.y = 1024 * rand() / (RAND_MAX + 1.0f);
		point.z = 1024 * rand() / (RAND_MAX + 1.0f);
	}

	std::cerr << "Cloud before projection: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
	/*
	* 填充ModelCoefficients 的值,本例中使用一个ax+by+cz+d=0的平面模型其中a=b=d=0,c=1,
	* 换句话说,也就是 X-Y平面,用户可以任意定义 PCL中支持的模型圆球、圆柱、锥形等进行投影滤波。
	*/
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
	coefficients->values.resize(4);
	coefficients->values[0] = coefficients->values[1] = 0;
	coefficients->values[2] = 1.0;
	coefficients->values[3] = 0;

	// 创建滤波对象
	pcl::ProjectInliers proj;
	proj.setModelType(pcl::SACMODEL_PLANE);
	proj.setInputCloud(cloud);
	proj.setModelCoefficients(coefficients);
	proj.filter(*cloud_projected);
	std::cerr << "Cloud after projection: " << std::endl;
	for (const auto& point : *cloud_projected)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;



	return (0);

}
Cloud before projection:
    1.28125 577.094 197.938
    828.125 599.031 491.375
    358.688 917.438 842.562
    764.5 178.281 879.531
    727.531 525.844 311.281
Cloud after projection:
    1.28125 577.094 0
    828.125 599.031 0
    358.688 917.438 0
    764.5 178.281 0
    727.531 525.844 0

从一个点云中提取索引extract_indices

这里就是简单的使用模型获取点云的平面,然后提取对应的索引,然后获取对应的点云数据

#include
#include
#include
#include
#include
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

int main()
{
	// 从一个点云中提取索引
	pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2), cloud_filtered_blob(new pcl::PCLPointCloud2);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud), cloud_p(new pcl::PointCloud), cloud_f(new pcl::PointCloud);

	// 读取点云原始数据
	pcl::PCDReader reader;
	reader.read("table_scene_lms400.pcd", *cloud_blob);

	std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;

	// Create the filtering object: downsample the dataset using a leaf size of 1cm
	// 降采样,加速后续的计算速度
	pcl::VoxelGrid sor;
	sor.setInputCloud(cloud_blob);
	sor.setLeafSize(0.01f, 0.01f, 0.01f);
	sor.filter(*cloud_filtered_blob);

	// Convert to the templated PointCloud
	pcl::fromPCLPointCloud2(*cloud_filtered_blob, *cloud_filtered);

	std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;

	// Write the downsampled version to disk
	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_downsampled.pcd", *cloud_filtered, false);

	// 创建一个模型参事对象
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
	pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
	// 创建一个分割对象
	pcl::SACSegmentation seg;
	// 设置参数
	seg.setOptimizeCoefficients(true);
	seg.setModelType(pcl::SACMODEL_PLANE);
	seg.setMethodType(pcl::SAC_RANSAC);
	seg.setMaxIterations(1000);
	seg.setDistanceThreshold(0.01);

	// 创建滤波器对象
	pcl::ExtractIndices extract;
	int i = 0;
	int nr_points = cloud_filtered->size();
	while (cloud_filtered->size()>0.3*nr_points)
	{
		seg.setInputCloud(cloud_filtered);
		seg.segment(*inliers, *coefficients);
		if (inliers->indices.size() == 0)
		{
			std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
			break;
		}
		// 提取点
		extract.setInputCloud(cloud_filtered);
		extract.setIndices(inliers);
		extract.setNegative(false);
		extract.filter(*cloud_p);
		std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;

		std::stringstream ss;
		ss << "table_scene_lms400_plane_" << i << ".pcd";
		writer.write(ss.str(), *cloud_p, false);

		// Create the filtering object
		extract.setNegative(true);
		extract.filter(*cloud_f);
		cloud_filtered.swap(cloud_f);
		i++;
	}
	return (0);

}
pcl_viewer.exe -multiview 1 .\table_scene_lms400_downsampled.pcd .\table_scene_lms400_plane_0.pcd .\table_scene_lms400_plane_1.pcd

pcl--第一节 Filters_第5张图片

使用ConditionalRemoval或 RadiusOutlierRemoval移除离群点 

ConditionalRemoval¶

条件滤波,设置不同维度滤波规则进行滤波,具体见下个与【半径离群值滤波】合并的案例。

RadiusOutlierRemoval¶

半径离群值滤波

下图有助于可视化RadiusOutlierRemoval过滤器对象的作用。用户指定邻居的个数,要每个点必须在指定半径内具有指定个邻居才能保留在PointCloud中。例如,如果指定了1个邻居,则只会从PointCloud中删除黄点。如果指定了2个邻居,则黄色和绿色的点都将从PointCloud中删除。

pcl--第一节 Filters_第6张图片

// 使用ConditionalRemoval或 RadiusOutlierRemoval移除离群点 
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);

	// Fill in the cloud data
	cloud->width = 15;
	cloud->height = 1;
	cloud->resize(cloud->width* cloud->height);

	for (auto& point : *cloud)
	{
		point.x =  rand() / (RAND_MAX + 1.0f);
		point.y =  rand() / (RAND_MAX + 1.0f);
		point.z =  rand() / (RAND_MAX + 1.0f);
	}

	if (true) {
		pcl::RadiusOutlierRemoval outrem;
		// build the filter
		outrem.setInputCloud(cloud);
		outrem.setRadiusSearch(0.5);
		outrem.setMinNeighborsInRadius(3);
		outrem.setKeepOrganized(true);
		// apply filter
		outrem.filter(*cloud_filtered);
	}
	else if (false) {
		// build the condition
		pcl::ConditionAnd::Ptr range_cond(new
			pcl::ConditionAnd());
		range_cond->addComparison(pcl::FieldComparison::ConstPtr(new
			pcl::FieldComparison("z", pcl::ComparisonOps::GT, 0.0)));
		range_cond->addComparison(pcl::FieldComparison::ConstPtr(new
			pcl::FieldComparison("z", pcl::ComparisonOps::LT, 0.8)));
		// build the filter
		pcl::ConditionalRemoval condrem;
		condrem.setCondition(range_cond);
		condrem.setInputCloud(cloud);
		condrem.setKeepOrganized(true);
		// apply filter
		condrem.filter(*cloud_filtered);
	}
	else {
		std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
		exit(0);
	}
	std::cerr << "Cloud before filtering: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
	// display pointcloud after filtering
	std::cerr << "Cloud after filtering: " << std::endl;
	for (const auto& point : *cloud_filtered)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;

 目前就这些例子,后续在其他应用中会大量的使用这边滤波方法

总体代码

 

#include
#include
#include
#include
#include
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

#include 
#include 


int main()
{
	// 直通滤波器
#if 0
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);
	cloud->width = 5;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);

	for (auto& point : *cloud)
	{
		point.x =  0.5+rand() / (RAND_MAX + 1.0f);
		point.y =  0.5+rand() / (RAND_MAX + 1.0f);
		point.z =  0.5+rand() / (RAND_MAX + 1.0f);
	}

	std::cerr << "Cloud before filtering: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;

	pcl::PassThrough pass;
	pass.setInputCloud(cloud);
	pass.setFilterFieldName("z");
	pass.setFilterLimits(0.0, 1.0);
	pass.setFilterLimitsNegative(true);
	pass.filter(*cloud_filtered);

	std::cerr << "Cloud after filtering: " << std::endl;
	for (const auto& point : *cloud_filtered)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
	
#endif

	// 体素滤波,降采样
#if 0
	pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
	pcl::PCLPointCloud2::Ptr cloud_filtered(new pcl::PCLPointCloud2());

	//创建pcd读取器
	pcl::PCDReader reader;
	reader.read("D:\\install\\pcl_1.13.1_data_pcd\\tutorials\\table_scene_lms400.pcd", *cloud);
	std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height
		<< " data points (" << pcl::getFieldsList(*cloud) << ")." << std::endl;
	// 创建滤波器对象
	pcl::VoxelGrid 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;
	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_downsampled.pcd", *cloud_filtered,
		Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false
	);
#endif
	
	// 统计分析滤波
#if 0
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);

	// Fill in the cloud data
	pcl::PCDReader reader;
	// Replace the path below with the path where you saved your file
	reader.read("table_scene_lms400.pcd", *cloud);

	std::cerr << "Cloud before filtering: " << std::endl;
	std::cerr << *cloud << std::endl;
	// 创建统计滤波
	pcl::StatisticalOutlierRemoval sor;
	sor.setInputCloud(cloud);
	sor.setMeanK(50);// 使用临近50个点做统计
	sor.setStddevMulThresh(1.0); // 设置标准差倍数为1,一个点的距离超出平均距离的一个标准差以上认为是离群点
	sor.filter(*cloud_filtered);

	std::cerr << "Cloud after filtering: " << std::endl;
	std::cerr << *cloud_filtered << std::endl;

	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_inliers.pcd", *cloud_filtered, false);

	sor.setNegative(true);
	sor.filter(*cloud_filtered);
	writer.write("table_scene_lms400_outliers.pcd", *cloud_filtered, false);

#endif
	// 使用参数化模型投影点云
#if 0
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_projected(new pcl::PointCloud);

	// Fill in the cloud data
	cloud->width = 5;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);

	for (auto& point : *cloud)
	{
		point.x = 1024 * rand() / (RAND_MAX + 1.0f);
		point.y = 1024 * rand() / (RAND_MAX + 1.0f);
		point.z = 1024 * rand() / (RAND_MAX + 1.0f);
	}

	std::cerr << "Cloud before projection: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
	/*
	* 填充ModelCoefficients 的值,本例中使用一个ax+by+cz+d=0的平面模型其中a=b=d=0,c=1,
	* 换句话说,也就是 X-Y平面,用户可以任意定义 PCL中支持的模型圆球、圆柱、锥形等进行投影滤波。
	*/
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
	coefficients->values.resize(4);
	coefficients->values[0] = coefficients->values[1] = 0;
	coefficients->values[2] = 1.0;
	coefficients->values[3] = 0;

	// 创建滤波对象
	pcl::ProjectInliers proj;
	proj.setModelType(pcl::SACMODEL_PLANE);
	proj.setInputCloud(cloud);
	proj.setModelCoefficients(coefficients);
	proj.filter(*cloud_projected);
	std::cerr << "Cloud after projection: " << std::endl;
	for (const auto& point : *cloud_projected)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
#endif
	// 从一个点云中提取索引
#if 0
	pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2), cloud_filtered_blob(new pcl::PCLPointCloud2);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud), cloud_p(new pcl::PointCloud), cloud_f(new pcl::PointCloud);

	// 读取点云原始数据
	pcl::PCDReader reader;
	reader.read("table_scene_lms400.pcd", *cloud_blob);

	std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl;

	// Create the filtering object: downsample the dataset using a leaf size of 1cm
	// 降采样,加速后续的计算速度
	pcl::VoxelGrid sor;
	sor.setInputCloud(cloud_blob);
	sor.setLeafSize(0.01f, 0.01f, 0.01f);
	sor.filter(*cloud_filtered_blob);

	// Convert to the templated PointCloud
	pcl::fromPCLPointCloud2(*cloud_filtered_blob, *cloud_filtered);

	std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl;

	// Write the downsampled version to disk
	pcl::PCDWriter writer;
	writer.write("table_scene_lms400_downsampled.pcd", *cloud_filtered, false);

	// 创建一个模型参事对象
	pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
	pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
	// 创建一个分割对象
	pcl::SACSegmentation seg;
	// 设置参数
	seg.setOptimizeCoefficients(true);
	seg.setModelType(pcl::SACMODEL_PLANE);
	seg.setMethodType(pcl::SAC_RANSAC);
	seg.setMaxIterations(1000);
	seg.setDistanceThreshold(0.01);

	// 创建滤波器对象
	pcl::ExtractIndices extract;
	int i = 0;
	int nr_points = cloud_filtered->size();
	while (cloud_filtered->size()>0.3*nr_points)
	{
		seg.setInputCloud(cloud_filtered);
		seg.segment(*inliers, *coefficients);
		if (inliers->indices.size() == 0)
		{
			std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
			break;
		}
		// 提取点
		extract.setInputCloud(cloud_filtered);
		extract.setIndices(inliers);
		extract.setNegative(false);
		extract.filter(*cloud_p);
		std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl;

		std::stringstream ss;
		ss << "table_scene_lms400_plane_" << i << ".pcd";
		writer.write(ss.str(), *cloud_p, false);

		// Create the filtering object
		extract.setNegative(true);
		extract.filter(*cloud_f);
		cloud_filtered.swap(cloud_f);
		i++;
	}
#endif

	// 使用ConditionalRemoval或 RadiusOutlierRemoval移除离群点 
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered(new pcl::PointCloud);

	// Fill in the cloud data
	cloud->width = 15;
	cloud->height = 1;
	cloud->resize(cloud->width* cloud->height);

	for (auto& point : *cloud)
	{
		point.x =  rand() / (RAND_MAX + 1.0f);
		point.y =  rand() / (RAND_MAX + 1.0f);
		point.z =  rand() / (RAND_MAX + 1.0f);
	}

	if (true) {
		pcl::RadiusOutlierRemoval outrem;
		// build the filter
		outrem.setInputCloud(cloud);
		outrem.setRadiusSearch(0.5);
		outrem.setMinNeighborsInRadius(3);
		outrem.setKeepOrganized(true);
		// apply filter
		outrem.filter(*cloud_filtered);
	}
	else if (false) {
		// build the condition
		pcl::ConditionAnd::Ptr range_cond(new
			pcl::ConditionAnd());
		range_cond->addComparison(pcl::FieldComparison::ConstPtr(new
			pcl::FieldComparison("z", pcl::ComparisonOps::GT, 0.0)));
		range_cond->addComparison(pcl::FieldComparison::ConstPtr(new
			pcl::FieldComparison("z", pcl::ComparisonOps::LT, 0.8)));
		// build the filter
		pcl::ConditionalRemoval condrem;
		condrem.setCondition(range_cond);
		condrem.setInputCloud(cloud);
		condrem.setKeepOrganized(true);
		// apply filter
		condrem.filter(*cloud_filtered);
	}
	else {
		std::cerr << "please specify command line arg '-r' or '-c'" << std::endl;
		exit(0);
	}
	std::cerr << "Cloud before filtering: " << std::endl;
	for (const auto& point : *cloud)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;
	// display pointcloud after filtering
	std::cerr << "Cloud after filtering: " << std::endl;
	for (const auto& point : *cloud_filtered)
		std::cerr << "    " << point.x << " "
		<< point.y << " "
		<< point.z << std::endl;


	return (0);

}

你可能感兴趣的:(3d,算法,c++)