作用是去除稀疏离群噪点。在采集点云的过程中,由于测量噪声的影响,会引入部分离群噪点,它们在点云空间中分布稀疏。在估算点云局部特征(例如计算采样点处的法向量和曲率变化率)时,这些噪点可能导致错误的计算结果,从而使点云配准等后期处理失败。统计滤波器的主要思想是假设点云中所有的点与其最近的k个邻居点的平均距离满足高斯分布,那么,根据均值和方差可确定一个距离阈值,当某个点与其最近k个点的平均距离大于这个阈值时,判定该点为离群点并去除。统计滤波器的实现原理如下:首先,遍历点云,计算每个点与其最近的k个邻居点之间的平均距离;其次,计算所有平均距离的均值μ与标准差σ,则距离阈值dmax可表示为dmax=μ+α×σ,α是一个常数,可称为比例系数,它取决于邻居点的数目;最后,再次遍历点云,剔除与k个邻居点的平均距离大于dmax的点。
PCL中的源码记录:
template <typename PointT> void
pcl::StatisticalOutlierRemoval<PointT>::applyFilterIndices (std::vector<int> &indices)
{// Initialize the search classif (!searcher_){if (input_->isOrganized ())searcher_.reset (new pcl::search::OrganizedNeighbor<PointT> ());elsesearcher_.reset (new pcl::search::KdTree<PointT> (false));}searcher_->setInputCloud (input_);// The arrays to be usedstd::vector<int> nn_indices (mean_k_);std::vector<float> nn_dists (mean_k_);//distances数组用于存储每个点到其最近邻点的平均距离std::vector<float> distances (indices_->size ());//indices和removed_indices_用于存储滤波后的点的索引。indices.resize (indices_->size ());removed_indices_->resize (indices_->size ());//oii和rii分别是输出点索引的迭代器和被移除点索引的迭代器。int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator// First pass: Compute the mean distances for all points with respect to their k nearest neighborsint valid_distances = 0;for (int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii) // iii = input indices iterator{if (!std::isfinite (input_->points[(*indices_)[iii]].x) ||!std::isfinite (input_->points[(*indices_)[iii]].y) ||!std::isfinite (input_->points[(*indices_)[iii]].z)){distances[iii] = 0.0;continue;}// Perform the nearest k searchif (searcher_->nearestKSearch ((*indices_)[iii], mean_k_ + 1, nn_indices, nn_dists) == 0){distances[iii] = 0.0;PCL_WARN ("[pcl::%s::applyFilter] Searching for the closest %d neighbors failed.\n", getClassName ().c_str (), mean_k_);continue;}// Calculate the mean distance to its neighborsdouble dist_sum = 0.0;for (int k = 1; k < mean_k_ + 1; ++k) // k = 0 is the query pointdist_sum += sqrt (nn_dists[k]);distances[iii] = static_cast<float> (dist_sum / mean_k_);//valid_distances用于记录有效的距离计算次数。valid_distances++;}// Estimate the mean and the standard deviation of the distance vector//sum和sq_sum分别用于累加距离和距离的平方double sum = 0, sq_sum = 0;for (const float &distance : distances){sum += distance;sq_sum += distance * distance;}double mean = sum / static_cast<double>(valid_distances);double variance = (sq_sum - sum * sum / static_cast<double>(valid_distances)) / (static_cast<double>(valid_distances) - 1);double stddev = sqrt (variance);//getMeanStd (distances, mean, stddev);double distance_threshold = mean + std_mul_ * stddev;// Second pass: Classify the points on the computed distance threshold//如果点的平均距离超过阈值,即被认为是异常点,并将其索引存储到removed_indices_中。否则,将点的索引存储到indices中。for (int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii) // iii = input indices iterator{// Points having a too high average distance are outliers and are passed to removed indices// Unless negative was set, then it's the opposite conditionif ((!negative_ && distances[iii] > distance_threshold) || (negative_ && distances[iii] <= distance_threshold)){if (extract_removed_indices_)(*removed_indices_)[rii++] = (*indices_)[iii];continue;}// Otherwise it was a normal point for output (inlier)indices[oii++] = (*indices_)[iii];}// Resize the output arraysindices.resize (oii);removed_indices_->resize (rii);
}