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