40 #ifndef PCL_FILTERS_IMPL_STATISTICAL_OUTLIER_REMOVAL_H_
41 #define PCL_FILTERS_IMPL_STATISTICAL_OUTLIER_REMOVAL_H_
43 #include <pcl/filters/statistical_outlier_removal.h>
44 #include <pcl/search/organized.h>
45 #include <pcl/search/kdtree.h>
48 template <
typename Po
intT>
void
54 if (input_->isOrganized ())
59 if (!searcher_->setInputCloud (input_))
61 PCL_ERROR (
"[pcl::%s::applyFilter] Error when initializing search method!\n", getClassName ().c_str ());
63 removed_indices_->clear ();
68 const int searcher_k = mean_k_ + 1;
69 Indices nn_indices (searcher_k);
70 std::vector<float> nn_dists (searcher_k);
71 std::vector<float> distances (indices_->size ());
72 indices.resize (indices_->size ());
73 removed_indices_->resize (indices_->size ());
77 int valid_distances = 0;
78 for (
int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii)
80 if (!std::isfinite ((*input_)[(*indices_)[iii]].x) ||
81 !std::isfinite ((*input_)[(*indices_)[iii]].y) ||
82 !std::isfinite ((*input_)[(*indices_)[iii]].z))
89 if (searcher_->nearestKSearch ((*indices_)[iii], searcher_k, nn_indices, nn_dists) == 0)
92 PCL_WARN (
"[pcl::%s::applyFilter] Searching for the closest %d neighbors failed.\n", getClassName ().c_str (), mean_k_);
97 double dist_sum = 0.0;
98 for (std::size_t k = 1; k < nn_dists.size(); ++k)
99 dist_sum += sqrt(nn_dists[k]);
100 distances[iii] =
static_cast<float>(dist_sum / (nn_dists.size() - 1));
105 double sum = 0, sq_sum = 0;
106 for (
const float &
distance : distances)
111 double mean = sum /
static_cast<double>(valid_distances);
112 double variance = (sq_sum - sum * sum /
static_cast<double>(valid_distances)) / (
static_cast<double>(valid_distances) - 1);
113 double stddev = sqrt (variance);
116 double distance_threshold = mean + std_mul_ * stddev;
119 for (
int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii)
123 if ((!negative_ && distances[iii] > distance_threshold) || (negative_ && distances[iii] <= distance_threshold))
125 if (extract_removed_indices_)
126 (*removed_indices_)[rii++] = (*indices_)[iii];
131 indices[oii++] = (*indices_)[iii];
135 indices.resize (oii);
136 removed_indices_->resize (rii);
139 #define PCL_INSTANTIATE_StatisticalOutlierRemoval(T) template class PCL_EXPORTS pcl::StatisticalOutlierRemoval<T>;
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.