Point Cloud Library (PCL)  1.10.1
radius_outlier_removal.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/filters/filter_indices.h>
43 #include <pcl/search/pcl_search.h>
44 
45 namespace pcl
46 {
47  /** \brief @b RadiusOutlierRemoval filters points in a cloud based on the number of neighbors they have.
48  * \details Iterates through the entire input once, and for each point, retrieves the number of neighbors within a certain radius.
49  * The point will be considered an outlier if it has too few neighbors, as determined by setMinNeighborsInRadius().
50  * The radius can be changed using setRadiusSearch().
51  * <br>
52  * The neighbors found for each query point will be found amongst ALL points of setInputCloud(), not just those indexed by setIndices().
53  * The setIndices() method only indexes the points that will be iterated through as search query points.
54  * <br><br>
55  * Usage example:
56  * \code
57  * pcl::RadiusOutlierRemoval<PointType> rorfilter (true); // Initializing with true will allow us to extract the removed indices
58  * rorfilter.setInputCloud (cloud_in);
59  * rorfilter.setRadiusSearch (0.1);
60  * rorfilter.setMinNeighborsInRadius (5);
61  * rorfilter.setNegative (true);
62  * rorfilter.filter (*cloud_out);
63  * // The resulting cloud_out contains all points of cloud_in that have 4 or less neighbors within the 0.1 search radius
64  * indices_rem = rorfilter.getRemovedIndices ();
65  * // The indices_rem array indexes all points of cloud_in that have 5 or more neighbors within the 0.1 search radius
66  * \endcode
67  * \author Radu Bogdan Rusu
68  * \ingroup filters
69  */
70  template<typename PointT>
71  class RadiusOutlierRemoval : public FilterIndices<PointT>
72  {
73  protected:
75  using PointCloudPtr = typename PointCloud::Ptr;
78 
79  public:
80 
83 
84 
85  /** \brief Constructor.
86  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
87  */
88  RadiusOutlierRemoval (bool extract_removed_indices = false) :
89  FilterIndices<PointT>::FilterIndices (extract_removed_indices),
90  searcher_ (),
91  search_radius_ (0.0),
92  min_pts_radius_ (1)
93  {
94  filter_name_ = "RadiusOutlierRemoval";
95  }
96 
97  /** \brief Set the radius of the sphere that will determine which points are neighbors.
98  * \details The number of points within this distance from the query point will need to be equal or greater
99  * than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
100  * \param[in] radius The radius of the sphere for nearest neighbor searching.
101  */
102  inline void
103  setRadiusSearch (double radius)
104  {
105  search_radius_ = radius;
106  }
107 
108  /** \brief Get the radius of the sphere that will determine which points are neighbors.
109  * \details The number of points within this distance from the query point will need to be equal or greater
110  * than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
111  * \return The radius of the sphere for nearest neighbor searching.
112  */
113  inline double
115  {
116  return (search_radius_);
117  }
118 
119  /** \brief Set the number of neighbors that need to be present in order to be classified as an inlier.
120  * \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
121  * than this number in order to be classified as an inlier point (i.e. will not be filtered).
122  * \param min_pts The minimum number of neighbors (default = 1).
123  */
124  inline void
126  {
127  min_pts_radius_ = min_pts;
128  }
129 
130  /** \brief Get the number of neighbors that need to be present in order to be classified as an inlier.
131  * \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
132  * than this number in order to be classified as an inlier point (i.e. will not be filtered).
133  * \return The minimum number of neighbors (default = 1).
134  */
135  inline int
137  {
138  return (min_pts_radius_);
139  }
140 
141  protected:
151 
152  /** \brief Filtered results are stored in a separate point cloud.
153  * \param[out] output The resultant point cloud.
154  */
155  void
156  applyFilter (PointCloud &output) override;
157 
158  /** \brief Filtered results are indexed by an indices array.
159  * \param[out] indices The resultant indices.
160  */
161  void
162  applyFilter (std::vector<int> &indices) override
163  {
164  applyFilterIndices (indices);
165  }
166 
167  /** \brief Filtered results are indexed by an indices array.
168  * \param[out] indices The resultant indices.
169  */
170  void
171  applyFilterIndices (std::vector<int> &indices);
172 
173  private:
174  /** \brief A pointer to the spatial search object. */
175  SearcherPtr searcher_;
176 
177  /** \brief The nearest neighbors search radius for each point. */
178  double search_radius_;
179 
180  /** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered an inlier. */
181  int min_pts_radius_;
182  };
183 
184  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
185  /** \brief @b RadiusOutlierRemoval is a simple filter that removes outliers if the number of neighbors in a certain
186  * search radius is smaller than a given K.
187  * \note setFilterFieldName (), setFilterLimits (), and setFilterLimitNegative () are ignored.
188  * \author Radu Bogdan Rusu
189  * \ingroup filters
190  */
191  template<>
192  class PCL_EXPORTS RadiusOutlierRemoval<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
193  {
196 
199 
201  using KdTreePtr = pcl::search::Search<pcl::PointXYZ>::Ptr;
202 
206 
207  public:
208  /** \brief Empty constructor. */
209  RadiusOutlierRemoval (bool extract_removed_indices = false) :
210  FilterIndices<pcl::PCLPointCloud2>::FilterIndices (extract_removed_indices),
211  search_radius_ (0.0), min_pts_radius_ (1)
212  {
213  filter_name_ = "RadiusOutlierRemoval";
214  }
215 
216  /** \brief Set the sphere radius that is to be used for determining the k-nearest neighbors for filtering.
217  * \param radius the sphere radius that is to contain all k-nearest neighbors
218  */
219  inline void
220  setRadiusSearch (double radius)
221  {
222  search_radius_ = radius;
223  }
224 
225  /** \brief Get the sphere radius used for determining the k-nearest neighbors. */
226  inline double
228  {
229  return (search_radius_);
230  }
231 
232  /** \brief Set the minimum number of neighbors that a point needs to have in the given search radius in order to
233  * be considered an inlier (i.e., valid).
234  * \param min_pts the minimum number of neighbors
235  */
236  inline void
238  {
239  min_pts_radius_ = min_pts;
240  }
241 
242  /** \brief Get the minimum number of neighbors that a point needs to have in the given search radius to be
243  * considered an inlier and avoid being filtered.
244  */
245  inline double
247  {
248  return (min_pts_radius_);
249  }
250 
251  protected:
252  /** \brief The nearest neighbors search radius for each point. */
254 
255  /** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered
256  * an inlier.
257  */
259 
260  /** \brief A pointer to the spatial search object. */
261  KdTreePtr searcher_;
262 
263  void
264  applyFilter (PCLPointCloud2 &output) override;
265 
266  void
267  applyFilter (std::vector<int> &indices) override;
268  };
269 }
270 
271 #ifdef PCL_NO_PRECOMPILE
272 #include <pcl/filters/impl/radius_outlier_removal.hpp>
273 #endif
pcl::search::Search< pcl::PointXYZ >
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::searcher_
KdTreePtr searcher_
A pointer to the spatial search object.
Definition: radius_outlier_removal.h:261
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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:125
pcl::RadiusOutlierRemoval::Ptr
shared_ptr< RadiusOutlierRemoval< PointT > > Ptr
Definition: radius_outlier_removal.h:81
pcl::PCLBase::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::PCLBase::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::RadiusOutlierRemoval::RadiusOutlierRemoval
RadiusOutlierRemoval(bool extract_removed_indices=false)
Constructor.
Definition: radius_outlier_removal.h:88
pcl::PCLPointCloud2::Ptr
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
Definition: PCLPointCloud2.h:33
pcl::RadiusOutlierRemoval::applyFilterIndices
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: radius_outlier_removal.hpp:73
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:246
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::min_pts_radius_
int min_pts_radius_
The minimum number of neighbors that a point needs to have in the given search radius to be considere...
Definition: radius_outlier_removal.h:258
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2ConstPtr
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:190
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:620
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:103
pcl::RadiusOutlierRemoval::applyFilter
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.
Definition: radius_outlier_removal.hpp:48
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:136
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2Ptr
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:189
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:220
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:34
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::search_radius_
double search_radius_
The nearest neighbors search radius for each point.
Definition: radius_outlier_removal.h:253
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::getRadiusSearch
double getRadiusSearch()
Get the sphere radius used for determining the k-nearest neighbors.
Definition: radius_outlier_removal.h:227
pcl::Filter::ConstPtr
shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:90
pcl::search::Search::Ptr
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:80
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:74
pcl::Filter
Filter represents the base filter class.
Definition: filter.h:83
pcl::RadiusOutlierRemoval
RadiusOutlierRemoval filters points in a cloud based on the number of neighbors they have.
Definition: radius_outlier_removal.h:71
pcl::PCLPointCloud2
Definition: PCLPointCloud2.h:14
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:237
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:164
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
pcl::RadiusOutlierRemoval::SearcherPtr
typename pcl::search::Search< PointT >::Ptr SearcherPtr
Definition: radius_outlier_removal.h:77
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
pcl::RadiusOutlierRemoval::applyFilter
void applyFilter(std::vector< int > &indices) override
Filtered results are indexed by an indices array.
Definition: radius_outlier_removal.h:162
pcl::RadiusOutlierRemoval< pcl::PCLPointCloud2 >::RadiusOutlierRemoval
RadiusOutlierRemoval(bool extract_removed_indices=false)
Empty constructor.
Definition: radius_outlier_removal.h:209
pcl::RadiusOutlierRemoval::getRadiusSearch
double getRadiusSearch()
Get the radius of the sphere that will determine which points are neighbors.
Definition: radius_outlier_removal.h:114
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:271
pcl::shared_ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:108