Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
correspondence_rejection_sample_consensus.hpp
Go to the documentation of this file.
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  *
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 Willow Garage, Inc. 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  *
37  */
38 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
39 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
40 
41 #include <boost/unordered_map.hpp>
42 
44 template <typename PointT> void
46  const pcl::Correspondences& original_correspondences,
47  pcl::Correspondences& remaining_correspondences)
48 {
49  int nr_correspondences = static_cast<int> (original_correspondences.size ());
50  std::vector<int> source_indices (nr_correspondences);
51  std::vector<int> target_indices (nr_correspondences);
52 
53  // Copy the query-match indices
54  for (size_t i = 0; i < original_correspondences.size (); ++i)
55  {
56  source_indices[i] = original_correspondences[i].index_query;
57  target_indices[i] = original_correspondences[i].index_match;
58  }
59 
60  // from pcl/registration/icp.hpp:
61  std::vector<int> source_indices_good;
62  std::vector<int> target_indices_good;
63  {
64  // From the set of correspondences found, attempt to remove outliers
65  // Create the registration model
66  typedef typename pcl::SampleConsensusModelRegistration<PointT>::Ptr SampleConsensusModelRegistrationPtr;
67  SampleConsensusModelRegistrationPtr model;
68  model.reset (new pcl::SampleConsensusModelRegistration<PointT> (input_, source_indices));
69  // Pass the target_indices
70  model->setInputTarget (target_, target_indices);
71  // Create a RANSAC model
72  pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_);
73  sac.setMaxIterations (max_iterations_);
74 
75  // Compute the set of inliers
76  if (!sac.computeModel ())
77  {
78  remaining_correspondences = original_correspondences;
79  best_transformation_.setIdentity ();
80  return;
81  }
82  else
83  {
84  std::vector<int> inliers;
85  sac.getInliers (inliers);
86 
87  if (inliers.size () < 3)
88  {
89  remaining_correspondences = original_correspondences;
90  best_transformation_.setIdentity ();
91  return;
92  }
93  boost::unordered_map<int, int> index_to_correspondence;
94  for (int i = 0; i < nr_correspondences; ++i)
95  index_to_correspondence[original_correspondences[i].index_query] = i;
96 
97  remaining_correspondences.resize (inliers.size ());
98  for (size_t i = 0; i < inliers.size (); ++i)
99  remaining_correspondences[i] = original_correspondences[index_to_correspondence[inliers[i]]];
100 
101  // get best transformation
102  Eigen::VectorXf model_coefficients;
103  sac.getModelCoefficients (model_coefficients);
104  best_transformation_.row (0) = model_coefficients.segment<4>(0);
105  best_transformation_.row (1) = model_coefficients.segment<4>(4);
106  best_transformation_.row (2) = model_coefficients.segment<4>(8);
107  best_transformation_.row (3) = model_coefficients.segment<4>(12);
108  }
109  }
110 }
111 
112 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_ */