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_
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::PointCloud::Ptr
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
pcl::registration::CorrespondenceRejectorSampleConsensus::getRemainingCorrespondences
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.
Definition: correspondence_rejection_sample_consensus.hpp:47
pcl::registration::CorrespondenceRejectorSampleConsensus::getInlierThreshold
double getInlierThreshold()
Get the maximum distance between corresponding points.
Definition: correspondence_rejection_sample_consensus.h:163
pcl::registration::CorrespondenceRejector::getClassName
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: correspondence_rejection.h:135
pcl::registration::CorrespondenceRejectorSampleConsensus::~CorrespondenceRejectorSampleConsensus
virtual ~CorrespondenceRejectorSampleConsensus()
Empty destructor.
Definition: correspondence_rejection_sample_consensus.h:90
pcl::registration::CorrespondenceRejectorSampleConsensus::target_
PointCloudConstPtr target_
Definition: correspondence_rejection_sample_consensus.h:233
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:53
pcl::registration::CorrespondenceRejectorSampleConsensus::input_transformed_
PointCloudPtr input_transformed_
Definition: correspondence_rejection_sample_consensus.h:232
pcl::registration::CorrespondenceRejectorSampleConsensus::getMaximumIterations
int getMaximumIterations()
Get the maximum number of iterations.
Definition: correspondence_rejection_sample_consensus.h:175
pcl::registration::CorrespondenceRejectorSampleConsensus::setTargetPoints
void setTargetPoints(pcl::PCLPointCloud2::ConstPtr cloud2)
Method for setting the target cloud.
Definition: correspondence_rejection_sample_consensus.h:145
pcl::registration::CorrespondenceRejectorSampleConsensus::setInputTarget
virtual void setInputTarget(const PointCloudConstPtr &cloud)
Provide a target point cloud dataset (must contain XYZ data!)
Definition: correspondence_rejection_sample_consensus.h:117
pcl::registration::CorrespondenceRejectorSampleConsensus::getInliersIndices
void getInliersIndices(std::vector< int > &inlier_indices)
Get the inlier indices found by the correspondence rejector.
Definition: correspondence_rejection_sample_consensus.h:203
pcl::registration::CorrespondenceRejectorSampleConsensus::Ptr
boost::shared_ptr< CorrespondenceRejectorSampleConsensus > Ptr
Definition: correspondence_rejection_sample_consensus.h:70
pcl::registration::CorrespondenceRejectorSampleConsensus::getBestTransformation
Eigen::Matrix4f getBestTransformation()
Get the best transformation after RANSAC rejection.
Definition: correspondence_rejection_sample_consensus.h:181
pcl::registration::CorrespondenceRejectorSampleConsensus::setMaximumIterations
void setMaximumIterations(int max_iterations)
Set the maximum number of iterations.
Definition: correspondence_rejection_sample_consensus.h:169
pcl::registration::CorrespondenceRejectorSampleConsensus::refine_
bool refine_
Definition: correspondence_rejection_sample_consensus.h:237
pcl::registration::CorrespondenceRejectorSampleConsensus::CorrespondenceRejectorSampleConsensus
CorrespondenceRejectorSampleConsensus()
Empty constructor.
Definition: correspondence_rejection_sample_consensus.h:76
pcl::registration::CorrespondenceRejectorSampleConsensus::getRefineModel
bool getRefineModel() const
Get the internal refine parameter value as set by the user using setRefineModel.
Definition: correspondence_rejection_sample_consensus.h:194
pcl::registration::CorrespondenceRejectorSampleConsensus::setInlierThreshold
void setInlierThreshold(double threshold)
Set the maximum distance between corresponding points.
Definition: correspondence_rejection_sample_consensus.h:157
pcl::registration::CorrespondenceRejectorSampleConsensus::getInputSource
const PointCloudConstPtr getInputSource()
Get a pointer to the input point cloud dataset target.
Definition: correspondence_rejection_sample_consensus.h:111
pcl::registration::CorrespondenceRejectorSampleConsensus::requiresTargetPoints
bool requiresTargetPoints() const
See if this rejector requires a target cloud.
Definition: correspondence_rejection_sample_consensus.h:140
pcl::registration::CorrespondenceRejectorSampleConsensus
CorrespondenceRejectorSampleConsensus implements a correspondence rejection using Random Sample Conse...
Definition: correspondence_rejection_sample_consensus.h:59
pcl::registration::CorrespondenceRejectorSampleConsensus::getSaveInliers
bool getSaveInliers()
Get whether the rejector is configured to save inliers.
Definition: correspondence_rejection_sample_consensus.h:213
pcl::registration::CorrespondenceRejectorSampleConsensus::best_transformation_
Eigen::Matrix4f best_transformation_
Definition: correspondence_rejection_sample_consensus.h:235
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:92
pcl::registration::CorrespondenceRejectorSampleConsensus::setSourcePoints
void setSourcePoints(pcl::PCLPointCloud2::ConstPtr cloud2)
Blob method for setting the source cloud.
Definition: correspondence_rejection_sample_consensus.h:131
pcl::registration::CorrespondenceRejectorSampleConsensus::setSaveInliers
void setSaveInliers(bool s)
Set whether to save inliers or not.
Definition: correspondence_rejection_sample_consensus.h:209
pcl::PointCloud::ConstPtr
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
pcl::registration::CorrespondenceRejectorSampleConsensus::ConstPtr
boost::shared_ptr< const CorrespondenceRejectorSampleConsensus > ConstPtr
Definition: correspondence_rejection_sample_consensus.h:71
pcl::registration::CorrespondenceRejectorSampleConsensus::inlier_indices_
std::vector< int > inlier_indices_
Definition: correspondence_rejection_sample_consensus.h:238
pcl::registration::CorrespondenceRejectorSampleConsensus::applyRejection
void applyRejection(pcl::Correspondences &correspondences)
Apply the rejection algorithm.
Definition: correspondence_rejection_sample_consensus.h:222
pcl::registration::CorrespondenceRejectorSampleConsensus::max_iterations_
int max_iterations_
Definition: correspondence_rejection_sample_consensus.h:229
pcl::registration::CorrespondenceRejectorSampleConsensus::input_
PointCloudConstPtr input_
Definition: correspondence_rejection_sample_consensus.h:231
pcl::PCLPointCloud2::ConstPtr
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
Definition: PCLPointCloud2.h:52
pcl::registration::CorrespondenceRejectorSampleConsensus::save_inliers_
bool save_inliers_
Definition: correspondence_rejection_sample_consensus.h:239
pcl::registration::CorrespondenceRejectorSampleConsensus::getInputTarget
const PointCloudConstPtr getInputTarget()
Get a pointer to the input point cloud dataset target.
Definition: correspondence_rejection_sample_consensus.h:121
pcl::registration::CorrespondenceRejectorSampleConsensus::setRefineModel
void setRefineModel(const bool refine)
Specify whether the model should be refined internally using the variance of the inliers.
Definition: correspondence_rejection_sample_consensus.h:187
pcl::registration::CorrespondenceRejectorSampleConsensus::setInputSource
virtual void setInputSource(const PointCloudConstPtr &cloud)
Provide a source point cloud dataset (must contain XYZ data!)
Definition: correspondence_rejection_sample_consensus.h:104
pcl::registration::CorrespondenceRejectorSampleConsensus::requiresSourcePoints
bool requiresSourcePoints() const
See if this rejector requires source points.
Definition: correspondence_rejection_sample_consensus.h:126
pcl::registration::CorrespondenceRejectorSampleConsensus::inlier_threshold_
double inlier_threshold_
Definition: correspondence_rejection_sample_consensus.h:227
pcl::registration::CorrespondenceRejector::input_correspondences_
CorrespondencesConstPtr input_correspondences_
The input correspondences.
Definition: correspondence_rejection.h:191
pcl::registration::CorrespondenceRejector::rejection_name_
std::string rejection_name_
The name of the rejection method.
Definition: correspondence_rejection.h:188
pcl::registration::CorrespondenceRejector
CorrespondenceRejector represents the base class for correspondence rejection methods
Definition: correspondence_rejection.h:59
pcl::fromPCLPointCloud2
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