Loading...
Searching...
No Matches
radius_outlier_removal.h
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2012, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#pragma once
#include <pcl/filters/filter_indices.h>
#include <pcl/search/search.h> // for Search, Search<>::Ptr
namespace pcl
{
/** \brief @b RadiusOutlierRemoval filters points in a cloud based on the number of neighbors they have.
* \details Iterates through the entire input once, and for each point, retrieves the number of neighbors within a certain radius.
* The point will be considered an outlier if it has too few neighbors, as determined by setMinNeighborsInRadius().
* The radius can be changed using setRadiusSearch().
* <br>
* The neighbors found for each query point will be found amongst ALL points of setInputCloud(), not just those indexed by setIndices().
* The setIndices() method only indexes the points that will be iterated through as search query points.
* <br><br>
* Usage example:
* \code
* pcl::RadiusOutlierRemoval<PointType> rorfilter (true); // Initializing with true will allow us to extract the removed indices
* rorfilter.setInputCloud (cloud_in);
* rorfilter.setRadiusSearch (0.1);
* rorfilter.setMinNeighborsInRadius (5);
* rorfilter.setNegative (true);
* rorfilter.filter (*cloud_out);
* // The resulting cloud_out contains all points of cloud_in that have 4 or less neighbors within the 0.1 search radius
* indices_rem = rorfilter.getRemovedIndices ();
* // The indices_rem array indexes all points of cloud_in that have 5 or more neighbors within the 0.1 search radius
* \endcode
* \sa StatisticalOutlierRemoval
* \author Radu Bogdan Rusu
* \ingroup filters
*/
template<typename PointT>
class RadiusOutlierRemoval : public FilterIndices<PointT>
{
protected:
using PointCloud = typename FilterIndices<PointT>::PointCloud;
using PointCloudPtr = typename PointCloud::Ptr;
using PointCloudConstPtr = typename PointCloud::ConstPtr;
using SearcherPtr = typename pcl::search::Search<PointT>::Ptr;
public:
using Ptr = shared_ptr<RadiusOutlierRemoval<PointT> >;
using ConstPtr = shared_ptr<const RadiusOutlierRemoval<PointT> >;
/** \brief Constructor.
* \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
*/
RadiusOutlierRemoval (bool extract_removed_indices = false) :
FilterIndices<PointT> (extract_removed_indices),
searcher_ ()
{
filter_name_ = "RadiusOutlierRemoval";
}
/** \brief Set the radius of the sphere that will determine which points are neighbors.
* \details The number of points within this distance from the query point will need to be equal or greater
* than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
* \param[in] radius The radius of the sphere for nearest neighbor searching.
*/
inline void
setRadiusSearch (double radius)
{
search_radius_ = radius;
}
/** \brief Get the radius of the sphere that will determine which points are neighbors.
* \details The number of points within this distance from the query point will need to be equal or greater
* than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
* \return The radius of the sphere for nearest neighbor searching.
*/
inline double
getRadiusSearch ()
{
return (search_radius_);
}
/** \brief Set the number of neighbors that need to be present in order to be classified as an inlier.
* \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
* than this number in order to be classified as an inlier point (i.e. will not be filtered).
* \param min_pts The minimum number of neighbors (default = 1).
*/
inline void
setMinNeighborsInRadius (int min_pts)
{
min_pts_radius_ = min_pts;
}
/** \brief Get the number of neighbors that need to be present in order to be classified as an inlier.
* \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
* than this number in order to be classified as an inlier point (i.e. will not be filtered).
* \return The minimum number of neighbors (default = 1).
*/
inline int
getMinNeighborsInRadius ()
{
return (min_pts_radius_);
}
/** \brief Provide a pointer to the search object.
* Calling this is optional. If not called, the search method will be chosen automatically.
* \param[in] searcher a pointer to the spatial search object.
*/
inline void
setSearchMethod (const SearcherPtr &searcher) { searcher_ = searcher; }
/** \brief Set the number of threads to use.
* \param nr_threads the number of hardware threads to use (0 sets the value back
* to automatic)
*/
void
setNumberOfThreads(unsigned int nr_threads = 0)
{
#ifdef _OPENMP
num_threads_ = nr_threads != 0 ? nr_threads : omp_get_num_procs();
#else
if (num_threads_ != 1) {
PCL_WARN("OpenMP is not available. Keeping number of threads unchanged at 1\n");
}
num_threads_ = 1;
#endif
}
protected:
using PCLBase<PointT>::input_;
using PCLBase<PointT>::indices_;
using Filter<PointT>::filter_name_;
using Filter<PointT>::getClassName;
using FilterIndices<PointT>::negative_;
using FilterIndices<PointT>::keep_organized_;
using FilterIndices<PointT>::user_filter_value_;
using FilterIndices<PointT>::extract_removed_indices_;
using FilterIndices<PointT>::removed_indices_;
/** \brief Filtered results are indexed by an indices array.
* \param[out] indices The resultant indices.
*/
void
applyFilter (Indices &indices) override
{
applyFilterIndices (indices);
}
/** \brief Filtered results are indexed by an indices array.
* \param[out] indices The resultant indices.
*/
void
applyFilterIndices (Indices &indices);
private:
/** \brief A pointer to the spatial search object. */
SearcherPtr searcher_;
/** \brief The nearest neighbors search radius for each point. */
double search_radius_{0.0};
/** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered an inlier. */
int min_pts_radius_{1};
/**
* @brief Number of threads used during filtering
*/
int num_threads_{1};
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** \brief @b RadiusOutlierRemoval is a simple filter that removes outliers if the number of neighbors in a certain
* search radius is smaller than a given K.
* \author Radu Bogdan Rusu
* \ingroup filters
*/
template<>
class PCL_EXPORTS RadiusOutlierRemoval<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
{
using Filter<pcl::PCLPointCloud2>::filter_name_;
using Filter<pcl::PCLPointCloud2>::getClassName;
using Filter<pcl::PCLPointCloud2>::removed_indices_;
using Filter<pcl::PCLPointCloud2>::extract_removed_indices_;
using KdTree = pcl::search::Search<pcl::PointXYZ>;
using KdTreePtr = pcl::search::Search<pcl::PointXYZ>::Ptr;
using PCLPointCloud2 = pcl::PCLPointCloud2;
using PCLPointCloud2Ptr = PCLPointCloud2::Ptr;
using PCLPointCloud2ConstPtr = PCLPointCloud2::ConstPtr;
public:
/** \brief Empty constructor. */
RadiusOutlierRemoval (bool extract_removed_indices = false) :
FilterIndices<pcl::PCLPointCloud2>::FilterIndices (extract_removed_indices)
{
filter_name_ = "RadiusOutlierRemoval";
}
/** \brief Set the sphere radius that is to be used for determining the k-nearest neighbors for filtering.
* \param radius the sphere radius that is to contain all k-nearest neighbors
*/
inline void
setRadiusSearch (double radius)
{
search_radius_ = radius;
}
/** \brief Get the sphere radius used for determining the k-nearest neighbors. */
inline double
getRadiusSearch ()
{
return (search_radius_);
}
/** \brief Set the minimum number of neighbors that a point needs to have in the given search radius in order to
* be considered an inlier (i.e., valid).
* \param min_pts the minimum number of neighbors
*/
inline void
setMinNeighborsInRadius (int min_pts)
{
min_pts_radius_ = min_pts;
}
/** \brief Get the minimum number of neighbors that a point needs to have in the given search radius to be
* considered an inlier and avoid being filtered.
*/
inline double
getMinNeighborsInRadius ()
{
return (min_pts_radius_);
}
protected:
/** \brief The nearest neighbors search radius for each point. */
double search_radius_{0.0};
/** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered
* an inlier.
*/
int min_pts_radius_{1};
/** \brief A pointer to the spatial search object. */
KdTreePtr searcher_;
void
applyFilter (PCLPointCloud2 &output) override;
void
applyFilter (Indices &indices) override;
};
}
#ifdef PCL_NO_PRECOMPILE
#include <pcl/filters/impl/radius_outlier_removal.hpp>
#endif
pcl::Filter
Filter represents the base filter class.
Definition filter.h:81
pcl::Filter::extract_removed_indices_
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition filter.h:161
pcl::Filter::getClassName
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition filter.h:174
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition filter.h:158
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition filter.h:155
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition filter_indices.h:75
pcl::FilterIndices::user_filter_value_
float user_filter_value_
The user given value that the filtered point dimensions should be set to (default = NaN).
Definition filter_indices.h:173
pcl::FilterIndices::keep_organized_
bool keep_organized_
False = remove points (default), true = redefine points, keep structure.
Definition filter_indices.h:170
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition filter_indices.h:167
pcl::PCLBase
PCL base class.
Definition pcl_base.h:70
pcl::PCLBase::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition pcl_base.h:150
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition point_cloud.h:173
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition point_cloud.h:413
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition point_cloud.h:414
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::applyFilter
void applyFilter(Indices &indices) override
Abstract filter method for point cloud indices.
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::getRadiusSearch
double getRadiusSearch()
Get the sphere radius used for determining the k-nearest neighbors.
Definition radius_outlier_removal.h:247
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::getMinNeighborsInRadius
double getMinNeighborsInRadius()
Get the minimum number of neighbors that a point needs to have in the given search radius to be consi...
Definition radius_outlier_removal.h:266
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::setRadiusSearch
void setRadiusSearch(double radius)
Set the sphere radius that is to be used for determining the k-nearest neighbors for filtering.
Definition radius_outlier_removal.h:240
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::searcher_
KdTreePtr searcher_
A pointer to the spatial search object.
Definition radius_outlier_removal.h:281
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::RadiusOutlierRemoval
RadiusOutlierRemoval(bool extract_removed_indices=false)
Empty constructor.
Definition radius_outlier_removal.h:230
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::applyFilter
void applyFilter(PCLPointCloud2 &output) override
Abstract filter method for point cloud.
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::setMinNeighborsInRadius
void setMinNeighborsInRadius(int min_pts)
Set the minimum number of neighbors that a point needs to have in the given search radius in order to...
Definition radius_outlier_removal.h:257
pcl::RadiusOutlierRemoval
RadiusOutlierRemoval filters points in a cloud based on the number of neighbors they have.
Definition radius_outlier_removal.h:73
pcl::RadiusOutlierRemoval::applyFilterIndices
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
Definition radius_outlier_removal.hpp:49
pcl::RadiusOutlierRemoval::getMinNeighborsInRadius
int getMinNeighborsInRadius()
Get the number of neighbors that need to be present in order to be classified as an inlier.
Definition radius_outlier_removal.h:135
pcl::RadiusOutlierRemoval::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition radius_outlier_removal.h:179
pcl::RadiusOutlierRemoval::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition radius_outlier_removal.h:76
pcl::RadiusOutlierRemoval::setMinNeighborsInRadius
void setMinNeighborsInRadius(int min_pts)
Set the number of neighbors that need to be present in order to be classified as an inlier.
Definition radius_outlier_removal.h:124
pcl::RadiusOutlierRemoval::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition radius_outlier_removal.h:77
pcl::RadiusOutlierRemoval::setRadiusSearch
void setRadiusSearch(double radius)
Set the radius of the sphere that will determine which points are neighbors.
Definition radius_outlier_removal.h:102
pcl::RadiusOutlierRemoval::Ptr
shared_ptr< RadiusOutlierRemoval< PointT > > Ptr
Definition radius_outlier_removal.h:82
pcl::RadiusOutlierRemoval::RadiusOutlierRemoval
RadiusOutlierRemoval(bool extract_removed_indices=false)
Constructor.
Definition radius_outlier_removal.h:89
pcl::RadiusOutlierRemoval::ConstPtr
shared_ptr< const RadiusOutlierRemoval< PointT > > ConstPtr
Definition radius_outlier_removal.h:83
pcl::RadiusOutlierRemoval::SearcherPtr
typename pcl::search::Search< PointT >::Ptr SearcherPtr
Definition radius_outlier_removal.h:78
pcl::RadiusOutlierRemoval::PointCloud
typename FilterIndices< PointT >::PointCloud PointCloud
Definition radius_outlier_removal.h:75
pcl::RadiusOutlierRemoval::getRadiusSearch
double getRadiusSearch()
Get the radius of the sphere that will determine which points are neighbors.
Definition radius_outlier_removal.h:113
pcl::RadiusOutlierRemoval::setSearchMethod
void setSearchMethod(const SearcherPtr &searcher)
Provide a pointer to the search object.
Definition radius_outlier_removal.h:145
pcl::RadiusOutlierRemoval::setNumberOfThreads
void setNumberOfThreads(unsigned int nr_threads=0)
Set the number of threads to use.
Definition radius_outlier_removal.h:152
pcl::search::Search< pcl::PointXYZ >
pcl::search::Search::Ptr
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition search.h:81
pcl
Definition convolution.h:46
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
pcl::PCLPointCloud2
Definition PCLPointCloud2.h:17
pcl::PCLPointCloud2::Ptr
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
Definition PCLPointCloud2.h:35
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition PCLPointCloud2.h:36
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition point_types.hpp:594