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