Point Cloud Library (PCL)  1.13.1-dev
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/search.h> // for Search, Search<>::Ptr
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 
81  using Ptr = shared_ptr<RadiusOutlierRemoval<PointT> >;
82  using ConstPtr = shared_ptr<const RadiusOutlierRemoval<PointT> >;
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> (extract_removed_indices),
90  searcher_ ()
91  {
92  filter_name_ = "RadiusOutlierRemoval";
93  }
94 
95  /** \brief Set the radius of the sphere that will determine which points are neighbors.
96  * \details The number of points within this distance from the query point will need to be equal or greater
97  * than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
98  * \param[in] radius The radius of the sphere for nearest neighbor searching.
99  */
100  inline void
101  setRadiusSearch (double radius)
102  {
103  search_radius_ = radius;
104  }
105 
106  /** \brief Get the radius of the sphere that will determine which points are neighbors.
107  * \details The number of points within this distance from the query point will need to be equal or greater
108  * than setMinNeighborsInRadius() in order to be classified as an inlier point (i.e. will not be filtered).
109  * \return The radius of the sphere for nearest neighbor searching.
110  */
111  inline double
113  {
114  return (search_radius_);
115  }
116 
117  /** \brief Set the number of neighbors that need to be present in order to be classified as an inlier.
118  * \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
119  * than this number in order to be classified as an inlier point (i.e. will not be filtered).
120  * \param min_pts The minimum number of neighbors (default = 1).
121  */
122  inline void
124  {
125  min_pts_radius_ = min_pts;
126  }
127 
128  /** \brief Get the number of neighbors that need to be present in order to be classified as an inlier.
129  * \details The number of points within setRadiusSearch() from the query point will need to be equal or greater
130  * than this number in order to be classified as an inlier point (i.e. will not be filtered).
131  * \return The minimum number of neighbors (default = 1).
132  */
133  inline int
135  {
136  return (min_pts_radius_);
137  }
138 
139  protected:
149 
150  /** \brief Filtered results are indexed by an indices array.
151  * \param[out] indices The resultant indices.
152  */
153  void
154  applyFilter (Indices &indices) override
155  {
156  applyFilterIndices (indices);
157  }
158 
159  /** \brief Filtered results are indexed by an indices array.
160  * \param[out] indices The resultant indices.
161  */
162  void
163  applyFilterIndices (Indices &indices);
164 
165  private:
166  /** \brief A pointer to the spatial search object. */
167  SearcherPtr searcher_;
168 
169  /** \brief The nearest neighbors search radius for each point. */
170  double search_radius_{0.0};
171 
172  /** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered an inlier. */
173  int min_pts_radius_{1};
174  };
175 
176  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
177  /** \brief @b RadiusOutlierRemoval is a simple filter that removes outliers if the number of neighbors in a certain
178  * search radius is smaller than a given K.
179  * \author Radu Bogdan Rusu
180  * \ingroup filters
181  */
182  template<>
183  class PCL_EXPORTS RadiusOutlierRemoval<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
184  {
187 
190 
192  using KdTreePtr = pcl::search::Search<pcl::PointXYZ>::Ptr;
193 
197 
198  public:
199  /** \brief Empty constructor. */
200  RadiusOutlierRemoval (bool extract_removed_indices = false) :
201  FilterIndices<pcl::PCLPointCloud2>::FilterIndices (extract_removed_indices)
202  {
203  filter_name_ = "RadiusOutlierRemoval";
204  }
205 
206  /** \brief Set the sphere radius that is to be used for determining the k-nearest neighbors for filtering.
207  * \param radius the sphere radius that is to contain all k-nearest neighbors
208  */
209  inline void
210  setRadiusSearch (double radius)
211  {
212  search_radius_ = radius;
213  }
214 
215  /** \brief Get the sphere radius used for determining the k-nearest neighbors. */
216  inline double
218  {
219  return (search_radius_);
220  }
221 
222  /** \brief Set the minimum number of neighbors that a point needs to have in the given search radius in order to
223  * be considered an inlier (i.e., valid).
224  * \param min_pts the minimum number of neighbors
225  */
226  inline void
228  {
229  min_pts_radius_ = min_pts;
230  }
231 
232  /** \brief Get the minimum number of neighbors that a point needs to have in the given search radius to be
233  * considered an inlier and avoid being filtered.
234  */
235  inline double
237  {
238  return (min_pts_radius_);
239  }
240 
241  protected:
242  /** \brief The nearest neighbors search radius for each point. */
243  double search_radius_{0.0};
244 
245  /** \brief The minimum number of neighbors that a point needs to have in the given search radius to be considered
246  * an inlier.
247  */
248  int min_pts_radius_{1};
249 
250  /** \brief A pointer to the spatial search object. */
251  KdTreePtr searcher_;
252 
253  void
254  applyFilter (PCLPointCloud2 &output) override;
255 
256  void
257  applyFilter (Indices &indices) override;
258  };
259 }
260 
261 #ifdef PCL_NO_PRECOMPILE
262 #include <pcl/filters/impl/radius_outlier_removal.hpp>
263 #endif
Filter represents the base filter class.
Definition: filter.h:81
shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:83
shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:84
std::string filter_name_
The filter name.
Definition: filter.h:158
FilterIndices represents the base class for filters that are about binary point removal.
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:185
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:186
PCL base class.
Definition: pcl_base.h:70
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
void applyFilter(Indices &indices) override
Abstract filter method for point cloud indices.
double getRadiusSearch()
Get the sphere radius used for determining the k-nearest neighbors.
double getMinNeighborsInRadius()
Get the minimum number of neighbors that a point needs to have in the given search radius to be consi...
void setRadiusSearch(double radius)
Set the sphere radius that is to be used for determining the k-nearest neighbors for filtering.
KdTreePtr searcher_
A pointer to the spatial search object.
RadiusOutlierRemoval(bool extract_removed_indices=false)
Empty constructor.
void applyFilter(PCLPointCloud2 &output) override
Abstract filter method for point cloud.
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...
RadiusOutlierRemoval filters points in a cloud based on the number of neighbors they have.
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
int getMinNeighborsInRadius()
Get the number of neighbors that need to be present in order to be classified as an inlier.
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
void setMinNeighborsInRadius(int min_pts)
Set the number of neighbors that need to be present in order to be classified as an inlier.
void setRadiusSearch(double radius)
Set the radius of the sphere that will determine which points are neighbors.
RadiusOutlierRemoval(bool extract_removed_indices=false)
Constructor.
typename pcl::search::Search< PointT >::Ptr SearcherPtr
double getRadiusSearch()
Get the radius of the sphere that will determine which points are neighbors.
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define PCL_EXPORTS
Definition: pcl_macros.h:323
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.