Point Cloud Library (PCL)  1.9.1
correspondence_rejection_sample_consensus.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 #ifndef PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_
41 #define PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_
42 
43 #include <pcl/registration/correspondence_rejection.h>
44 
45 #include <pcl/sample_consensus/ransac.h>
46 #include <pcl/sample_consensus/sac_model_registration.h>
47 #include <pcl/common/transforms.h>
48 
49 namespace pcl
50 {
51  namespace registration
52  {
53  /** \brief CorrespondenceRejectorSampleConsensus implements a correspondence rejection
54  * using Random Sample Consensus to identify inliers (and reject outliers)
55  * \author Dirk Holz
56  * \ingroup registration
57  */
58  template <typename PointT>
60  {
62  typedef typename PointCloud::Ptr PointCloudPtr;
63  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
64 
65  public:
69 
70  typedef boost::shared_ptr<CorrespondenceRejectorSampleConsensus> Ptr;
71  typedef boost::shared_ptr<const CorrespondenceRejectorSampleConsensus> ConstPtr;
72 
73  /** \brief Empty constructor. Sets the inlier threshold to 5cm (0.05m),
74  * and the maximum number of iterations to 1000.
75  */
77  : inlier_threshold_ (0.05)
78  , max_iterations_ (1000) // std::numeric_limits<int>::max ()
79  , input_ ()
81  , target_ ()
83  , refine_ (false)
84  , save_inliers_ (false)
85  {
86  rejection_name_ = "CorrespondenceRejectorSampleConsensus";
87  }
88 
89  /** \brief Empty destructor. */
91 
92  /** \brief Get a list of valid correspondences after rejection from the original set of correspondences.
93  * \param[in] original_correspondences the set of initial correspondences given
94  * \param[out] remaining_correspondences the resultant filtered set of remaining correspondences
95  */
96  inline void
97  getRemainingCorrespondences (const pcl::Correspondences& original_correspondences,
98  pcl::Correspondences& remaining_correspondences);
99 
100  /** \brief Provide a source point cloud dataset (must contain XYZ data!)
101  * \param[in] cloud a cloud containing XYZ data
102  */
103  virtual inline void
104  setInputSource (const PointCloudConstPtr &cloud)
105  {
106  input_ = cloud;
107  }
108 
109  /** \brief Get a pointer to the input point cloud dataset target. */
110  inline PointCloudConstPtr const
111  getInputSource () { return (input_); }
112 
113  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
114  * \param[in] cloud a cloud containing XYZ data
115  */
116  virtual inline void
117  setInputTarget (const PointCloudConstPtr &cloud) { target_ = cloud; }
118 
119  /** \brief Get a pointer to the input point cloud dataset target. */
120  inline PointCloudConstPtr const
121  getInputTarget () { return (target_ ); }
122 
123 
124  /** \brief See if this rejector requires source points */
125  bool
127  { return (true); }
128 
129  /** \brief Blob method for setting the source cloud */
130  void
132  {
133  PointCloudPtr cloud (new PointCloud);
134  fromPCLPointCloud2 (*cloud2, *cloud);
135  setInputSource (cloud);
136  }
137 
138  /** \brief See if this rejector requires a target cloud */
139  bool
141  { return (true); }
142 
143  /** \brief Method for setting the target cloud */
144  void
146  {
147  PointCloudPtr cloud (new PointCloud);
148  fromPCLPointCloud2 (*cloud2, *cloud);
149  setInputTarget (cloud);
150  }
151 
152  /** \brief Set the maximum distance between corresponding points.
153  * Correspondences with distances below the threshold are considered as inliers.
154  * \param[in] threshold Distance threshold in the same dimension as source and target data sets.
155  */
156  inline void
157  setInlierThreshold (double threshold) { inlier_threshold_ = threshold; };
158 
159  /** \brief Get the maximum distance between corresponding points.
160  * \return Distance threshold in the same dimension as source and target data sets.
161  */
162  inline double
164 
165  /** \brief Set the maximum number of iterations.
166  * \param[in] max_iterations Maximum number if iterations to run
167  */
168  inline void
169  setMaximumIterations (int max_iterations) { max_iterations_ = std::max (max_iterations, 0); }
170 
171  /** \brief Get the maximum number of iterations.
172  * \return max_iterations Maximum number if iterations to run
173  */
174  inline int
176 
177  /** \brief Get the best transformation after RANSAC rejection.
178  * \return The homogeneous 4x4 transformation yielding the largest number of inliers.
179  */
180  inline Eigen::Matrix4f
182 
183  /** \brief Specify whether the model should be refined internally using the variance of the inliers
184  * \param[in] refine true if the model should be refined, false otherwise
185  */
186  inline void
187  setRefineModel (const bool refine)
188  {
189  refine_ = refine;
190  }
191 
192  /** \brief Get the internal refine parameter value as set by the user using setRefineModel */
193  inline bool
194  getRefineModel () const
195  {
196  return (refine_);
197  }
198 
199  /** \brief Get the inlier indices found by the correspondence rejector. This information is only saved if setSaveInliers(true) was called in advance.
200  * \param[out] inlier_indices Indices for the inliers
201  */
202  inline void
203  getInliersIndices (std::vector<int> &inlier_indices) { inlier_indices = inlier_indices_; }
204 
205  /** \brief Set whether to save inliers or not
206  * \param[in] s True to save inliers / False otherwise
207  */
208  inline void
209  setSaveInliers (bool s) { save_inliers_ = s; }
210 
211  /** \brief Get whether the rejector is configured to save inliers */
212  inline bool
214 
215 
216  protected:
217 
218  /** \brief Apply the rejection algorithm.
219  * \param[out] correspondences the set of resultant correspondences.
220  */
221  inline void
223  {
225  }
226 
228 
230 
231  PointCloudConstPtr input_;
232  PointCloudPtr input_transformed_;
233  PointCloudConstPtr target_;
234 
235  Eigen::Matrix4f best_transformation_;
236 
237  bool refine_;
238  std::vector<int> inlier_indices_;
240 
241  public:
242  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
243  };
244  }
245 }
246 
247 #include <pcl/registration/impl/correspondence_rejection_sample_consensus.hpp>
248 
249 #endif // PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map...
Definition: conversions.h:169
double getInlierThreshold()
Get the maximum distance between corresponding points.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
CorrespondenceRejector represents the base class for correspondence rejection methods ...
void getInliersIndices(std::vector< int > &inlier_indices)
Get the inlier indices found by the correspondence rejector.
const std::string & getClassName() const
Get a string representation of the name of this class.
void setTargetPoints(pcl::PCLPointCloud2::ConstPtr cloud2)
Method for setting the target cloud.
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
boost::shared_ptr< CorrespondenceRejectorSampleConsensus > Ptr
Eigen::Matrix4f getBestTransformation()
Get the best transformation after RANSAC rejection.
virtual void setInputTarget(const PointCloudConstPtr &cloud)
Provide a target point cloud dataset (must contain XYZ data!)
void setMaximumIterations(int max_iterations)
Set the maximum number of iterations.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
PointCloudConstPtr const getInputSource()
Get a pointer to the input point cloud dataset target.
bool requiresTargetPoints() const
See if this rejector requires a target cloud.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
void setInlierThreshold(double threshold)
Set the maximum distance between corresponding points.
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
void setSourcePoints(pcl::PCLPointCloud2::ConstPtr cloud2)
Blob method for setting the source cloud.
bool getRefineModel() const
Get the internal refine parameter value as set by the user using setRefineModel.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
CorrespondenceRejectorSampleConsensus implements a correspondence rejection using Random Sample Conse...
PointCloudConstPtr const getInputTarget()
Get a pointer to the input point cloud dataset target.
void applyRejection(pcl::Correspondences &correspondences)
Apply the rejection algorithm.
CorrespondencesConstPtr input_correspondences_
The input correspondences.
virtual void setInputSource(const PointCloudConstPtr &cloud)
Provide a source point cloud dataset (must contain XYZ data!)
std::string rejection_name_
The name of the rejection method.
bool requiresSourcePoints() const
See if this rejector requires source points.
bool getSaveInliers()
Get whether the rejector is configured to save inliers.
boost::shared_ptr< const CorrespondenceRejectorSampleConsensus > ConstPtr
void getRemainingCorrespondences(const pcl::Correspondences &original_correspondences, pcl::Correspondences &remaining_correspondences)
Get a list of valid correspondences after rejection from the original set of correspondences.
void setRefineModel(const bool refine)
Specify whether the model should be refined internally using the variance of the inliers.