Point Cloud Library (PCL)  1.10.0-dev
correspondence_rejection_sample_consensus.hpp
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_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
41 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
42 
43 #include <unordered_map>
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> void
48  const pcl::Correspondences& original_correspondences,
49  pcl::Correspondences& remaining_correspondences)
50 {
51  if (!input_)
52  {
53  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input cloud dataset was given!\n", getClassName ().c_str ());
54  return;
55  }
56 
57  if (!target_)
58  {
59  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input target dataset was given!\n", getClassName ().c_str ());
60  return;
61  }
62 
63  if (save_inliers_)
64  inlier_indices_.clear ();
65 
66  int nr_correspondences = static_cast<int> (original_correspondences.size ());
67  std::vector<int> source_indices (nr_correspondences);
68  std::vector<int> target_indices (nr_correspondences);
69 
70  // Copy the query-match indices
71  for (std::size_t i = 0; i < original_correspondences.size (); ++i)
72  {
73  source_indices[i] = original_correspondences[i].index_query;
74  target_indices[i] = original_correspondences[i].index_match;
75  }
76 
77  {
78  // From the set of correspondences found, attempt to remove outliers
79  // Create the registration model
80  using SampleConsensusModelRegistrationPtr = typename pcl::SampleConsensusModelRegistration<PointT>::Ptr;
81  SampleConsensusModelRegistrationPtr model;
82  model.reset (new pcl::SampleConsensusModelRegistration<PointT> (input_, source_indices));
83  // Pass the target_indices
84  model->setInputTarget (target_, target_indices);
85  // Create a RANSAC model
86  pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_);
87  sac.setMaxIterations (max_iterations_);
88 
89  // Compute the set of inliers
90  if (!sac.computeModel ())
91  {
92  remaining_correspondences = original_correspondences;
93  best_transformation_.setIdentity ();
94  return;
95  }
96  if (refine_ && !sac.refineModel ())
97  {
98  PCL_ERROR ("[pcl::registration::CorrespondenceRejectorSampleConsensus::getRemainingCorrespondences] Could not refine the model! Returning an empty solution.\n");
99  return;
100  }
101 
102  std::vector<int> inliers;
103  sac.getInliers (inliers);
104 
105  if (inliers.size () < 3)
106  {
107  remaining_correspondences = original_correspondences;
108  best_transformation_.setIdentity ();
109  return;
110  }
111  std::unordered_map<int, int> index_to_correspondence;
112  for (int i = 0; i < nr_correspondences; ++i)
113  index_to_correspondence[original_correspondences[i].index_query] = i;
114 
115  remaining_correspondences.resize (inliers.size ());
116  for (std::size_t i = 0; i < inliers.size (); ++i)
117  remaining_correspondences[i] = original_correspondences[index_to_correspondence[inliers[i]]];
118 
119  if (save_inliers_)
120  {
121  inlier_indices_.reserve (inliers.size ());
122  for (const int &inlier : inliers)
123  inlier_indices_.push_back (index_to_correspondence[inlier]);
124  }
125 
126  // get best transformation
127  Eigen::VectorXf model_coefficients;
128  sac.getModelCoefficients (model_coefficients);
129  best_transformation_.row (0) = model_coefficients.segment<4>(0);
130  best_transformation_.row (1) = model_coefficients.segment<4>(4);
131  best_transformation_.row (2) = model_coefficients.segment<4>(8);
132  best_transformation_.row (3) = model_coefficients.segment<4>(12);
133  }
134 }
135 
136 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
SampleConsensusModelRegistration defines a model for Point-To-Point registration outlier rejection...
shared_ptr< SampleConsensusModelRegistration< PointT > > Ptr
void getInliers(std::vector< int > &inliers) const
Return the best set of inliers found so far for this model.
Definition: sac.h:308
void setMaxIterations(int max_iterations)
Set the maximum number of iterations.
Definition: sac.h:147
virtual bool refineModel(const double sigma=3.0, const unsigned int max_iterations=1000)
Refine the model found.
Definition: sac.h:187
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: ransac.hpp:57
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
void getRemainingCorrespondences(const pcl::Correspondences &original_correspondences, pcl::Correspondences &remaining_correspondences) override
Get a list of valid correspondences after rejection from the original set of correspondences.
RandomSampleConsensus represents an implementation of the RANSAC (RANdom SAmple Consensus) algorithm...
Definition: ransac.h:56
void getModelCoefficients(Eigen::VectorXf &model_coefficients) const
Return the model coefficients of the best model found so far.
Definition: sac.h:314