CloudCompare——统计滤波

目录

本文由CSDN点云侠原创,CloudCompare------统计滤波,爬虫自重。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫。

1.统计滤波

算法原理见:PCL 统计滤波器

2.软件实现

参数设置

3.完整操作

4.算法源码

cpp 复制代码
ReferenceCloud* CloudSamplingTools::sorFilter(	GenericIndexedCloudPersist* inputCloud,
												int knn/*=6*/,
												double nSigma/*=1.0*/,
												DgmOctree* inputOctree/*=0*/,
												GenericProgressCallback* progressCb/*=0*/)
{
	if (!inputCloud || knn <= 0 || inputCloud->size() <= static_cast<unsigned>(knn))
	{
		//invalid input
		assert(false);
		return nullptr;
	}

	DgmOctree* octree = inputOctree;
	if (!octree)
	{
		//compute the octree if necessary
		octree = new DgmOctree(inputCloud);
		if (octree->build(progressCb) < 1)
		{
			delete octree;
			return nullptr;
		}
	}

	//output
	ReferenceCloud* filteredCloud = nullptr;

	for (unsigned step = 0; step < 1; ++step) //fake loop for easy break
	{
		unsigned pointCount = inputCloud->size();

		std::vector<PointCoordinateType> meanDistances;
		try
		{
			meanDistances.resize(pointCount, 0);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			break;
		}
		
		double avgDist = 0;
		double stdDev = 0;

		//1st step: compute the average distance to the neighbors
		{
			//additional parameters
			void* additionalParameters[] = {reinterpret_cast<void*>(&knn),
											reinterpret_cast<void*>(&meanDistances)
			};

			unsigned char octreeLevel = octree->findBestLevelForAGivenPopulationPerCell(knn);

			if (octree->executeFunctionForAllCellsAtLevel(	octreeLevel,
															&applySORFilterAtLevel,
															additionalParameters,
															true,
															progressCb,
															"SOR filter") == 0)
			{
				//something went wrong
				break;
			}

			//deduce the average distance and std. dev.
			double sumDist = 0;
			double sumSquareDist = 0;
			for (unsigned i = 0; i < pointCount; ++i)
			{
				sumDist += meanDistances[i];
				sumSquareDist += meanDistances[i] * meanDistances[i];
			}
			avgDist = sumDist / pointCount;
			stdDev = sqrt(std::abs(sumSquareDist / pointCount - avgDist*avgDist));
		}

		//2nd step: remove the farthest points 
		{
			//deduce the max distance
			double maxDist = avgDist + nSigma * stdDev;

			filteredCloud = new ReferenceCloud(inputCloud);
			if (!filteredCloud->reserve(pointCount))
			{
				//not enough memory
				delete filteredCloud;
				filteredCloud = nullptr;
				break;
			}

			for (unsigned i = 0; i < pointCount; ++i)
			{
				if (meanDistances[i] <= maxDist)
				{
					filteredCloud->addPointIndex(i);
				}
			}

			filteredCloud->resize(filteredCloud->size());
		}
	}

	if (!inputOctree)
	{
		delete octree;
		octree = nullptr;
	}

	return filteredCloud;
}

5.相关代码

相关推荐
王老师青少年编程3 分钟前
csp信奥赛C++标准模板库STL(3):list的使用详解
c++·容器·stl·list·标准模板库·csp·信奥赛
ULTRA??4 分钟前
STL deque 的详细特征
c++·算法
九死九歌5 分钟前
【Sympydantic】使用sympydantic,利用pydantic告别numpy与pytorch编程中,tensor形状带来的烦人痛点!
开发语言·pytorch·python·机器学习·numpy·pydantic
Kiri霧5 分钟前
Go切片详解
开发语言·后端·golang
yongui478346 分钟前
MATLAB 二维方腔自然对流 SIMPLE 算法
人工智能·算法·matlab
二进制coder13 分钟前
C++ 中的 Interface:概念、实现与应用详解
开发语言·c++
古城小栈13 分钟前
Go 与 Rust:系统编程语言的竞争与融合
开发语言·golang·rust
随风一样自由15 分钟前
React编码时,什么时候用js文件,什么时候用jsx文件?
开发语言·javascript·react.js
循着风16 分钟前
环形子数组的最大和
数据结构·算法·leetcode
CoovallyAIHub16 分钟前
如何让AI的数据标注“火眼金睛”?人机协同才是可靠途径
深度学习·算法·计算机视觉