Point Cloud Library (PCL)  1.7.1
correspondence_rejection_sample_consensus_2d.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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_2D_HPP_
39 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_2D_HPP_
40 
41 #include <pcl/sample_consensus/sac_model_registration_2d.h>
42 #include <pcl/sample_consensus/ransac.h>
43 
44 ///////////////////////////////////////////////////////////////////////////////////////////
45 template <typename PointT> void
47  const pcl::Correspondences& original_correspondences,
48  pcl::Correspondences& remaining_correspondences)
49 {
50  if (!input_)
51  {
52  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input cloud dataset was given!\n", getClassName ().c_str ());
53  return;
54  }
55 
56  if (!target_)
57  {
58  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input target dataset was given!\n", getClassName ().c_str ());
59  return;
60  }
61 
62  if (projection_matrix_ == Eigen::Matrix3f::Identity ())
63  {
64  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] Intrinsic camera parameters not given!\n", getClassName ().c_str ());
65  return;
66  }
67 
68  int nr_correspondences = static_cast<int> (original_correspondences.size ());
69  std::vector<int> source_indices (nr_correspondences);
70  std::vector<int> target_indices (nr_correspondences);
71 
72  // Copy the query-match indices
73  for (size_t i = 0; i < original_correspondences.size (); ++i)
74  {
75  source_indices[i] = original_correspondences[i].index_query;
76  target_indices[i] = original_correspondences[i].index_match;
77  }
78 
79  // from pcl/registration/icp.hpp:
80  std::vector<int> source_indices_good;
81  std::vector<int> target_indices_good;
82 
83  // From the set of correspondences found, attempt to remove outliers
85  // Pass the target_indices
86  model->setInputTarget (target_, target_indices);
87  model->setProjectionMatrix (projection_matrix_);
88 
89  // Create a RANSAC model
90  pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_);
91  sac.setMaxIterations (max_iterations_);
92 
93  // Compute the set of inliers
94  if (!sac.computeModel ())
95  {
96  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] Error computing model! Returning the original correspondences...\n", getClassName ().c_str ());
97  remaining_correspondences = original_correspondences;
98  best_transformation_.setIdentity ();
99  return;
100  }
101  else
102  {
103  if (refine_ && !sac.refineModel (2.0))
104  PCL_WARN ("[pcl::registration::%s::getRemainingCorrespondences] Error refining model!\n", getClassName ().c_str ());
105 
106  std::vector<int> inliers;
107  sac.getInliers (inliers);
108 
109  if (inliers.size () < 3)
110  {
111  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] Less than 3 correspondences found!\n", getClassName ().c_str ());
112  remaining_correspondences = original_correspondences;
113  best_transformation_.setIdentity ();
114  return;
115  }
116 
117  boost::unordered_map<int, int> index_to_correspondence;
118  for (int i = 0; i < nr_correspondences; ++i)
119  index_to_correspondence[original_correspondences[i].index_query] = i;
120 
121  remaining_correspondences.resize (inliers.size ());
122  for (size_t i = 0; i < inliers.size (); ++i)
123  remaining_correspondences[i] = original_correspondences[index_to_correspondence[inliers[i]]];
124 
125  // get best transformation
126  Eigen::VectorXf model_coefficients;
127  sac.getModelCoefficients (model_coefficients);
128  best_transformation_.row (0) = model_coefficients.segment<4>(0);
129  best_transformation_.row (1) = model_coefficients.segment<4>(4);
130  best_transformation_.row (2) = model_coefficients.segment<4>(8);
131  best_transformation_.row (3) = model_coefficients.segment<4>(12);
132  }
133 }
134 
135 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_2D_HPP_
136