PCL大场景点云离群点剔除(八叉树滤波)

说明:该方法适用于数据量较大点云场景离群点剔除,效率较高 

#include 
#include 
#include 
#include 
#include 

typedef pcl::PointCloud::Ptr	pcXYZIPtr;
using namespace std;

/*
*  @brief 八叉树离群点剔除函数
*  param[in] cloud:输入点云  output:滤波后的点云
*/
void octreeRemovePoints(pcXYZIPtr cloud, pcXYZIPtr output)
{
	pcl::octree::OctreePointCloud octree(0.1);
	octree.setInputCloud(cloud);
	octree.addPointsFromInputCloud();   // 构建Octree
	vector vec_point_index, vec_total_index;    //  体素内点的索引,   要删除的点的索引
	vector vec_key;
	for (auto iter = octree.leaf_begin(); iter != octree.leaf_end(); ++iter)
	{
		auto key = iter.getCurrentOctreeKey();
		vec_key.emplace_back(key);
		auto it_key = octree.findLeaf(key.x, key.y, key.z);
		if (it_key != nullptr)
		{
			vec_point_index = iter.getLeafContainer().getPointIndicesVector();
			if (vec_point_index.size() < 10)             // 体素内点小于10时删除
			{
				for (size_t i = 0; i < vec_point_index.size(); i++)
				{
					vec_total_index.push_back(vec_point_index[i]);
				}
			}
		}
	}
	// 使用pcl index 滤波
	pcl::PointIndices::Ptr outliners(new pcl::PointIndices());
	outliners->indices.resize(vec_total_index.size());
	for (size_t i = 0; i < vec_total_index.size(); i++)
	{
		outliners->indices[i] = vec_total_index[i];
	}
	pcl::ExtractIndices extract;
	extract.setInputCloud(cloud);
	extract.setIndices(outliners);
	extract.setNegative(true);
	extract.filter(*output);
}

你可能感兴趣的:(点云数据处理,c++)