Point Cloud Library (PCL)  1.10.0
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 #include <unordered_map>
45 
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT> void
49  const pcl::Correspondences& original_correspondences,
50  pcl::Correspondences& remaining_correspondences)
51 {
52  if (!input_)
53  {
54  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input cloud dataset was given!\n", getClassName ().c_str ());
55  return;
56  }
57 
58  if (!target_)
59  {
60  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input target dataset was given!\n", getClassName ().c_str ());
61  return;
62  }
63 
64  if (projection_matrix_ == Eigen::Matrix3f::Identity ())
65  {
66  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] Intrinsic camera parameters not given!\n", getClassName ().c_str ());
67  return;
68  }
69 
70  int nr_correspondences = static_cast<int> (original_correspondences.size ());
71  std::vector<int> source_indices (nr_correspondences);
72  std::vector<int> target_indices (nr_correspondences);
73 
74  // Copy the query-match indices
75  for (std::size_t i = 0; i < original_correspondences.size (); ++i)
76  {
77  source_indices[i] = original_correspondences[i].index_query;
78  target_indices[i] = original_correspondences[i].index_match;
79  }
80 
81  // from pcl/registration/icp.hpp:
82  std::vector<int> source_indices_good;
83  std::vector<int> target_indices_good;
84 
85  // From the set of correspondences found, attempt to remove outliers
87  // Pass the target_indices
88  model->setInputTarget (target_, target_indices);
89  model->setProjectionMatrix (projection_matrix_);
90 
91  // Create a RANSAC model
92  pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_);
93  sac.setMaxIterations (max_iterations_);
94 
95  // Compute the set of inliers
96  if (!sac.computeModel ())
97  {
98  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] Error computing model! Returning the original correspondences...\n", getClassName ().c_str ());
99  remaining_correspondences = original_correspondences;
100  best_transformation_.setIdentity ();
101  return;
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  std::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 (std::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 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_2D_HPP_
135 
pcl::SampleConsensusModelRegistration2D
SampleConsensusModelRegistration2D defines a model for Point-To-Point registration outlier rejection ...
Definition: sac_model_registration_2d.h:51
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::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:48
pcl::SampleConsensusModelRegistration2D::Ptr
shared_ptr< SampleConsensusModelRegistration2D< PointT > > Ptr
Definition: sac_model_registration_2d.h:69
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::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target)
Set the input point cloud target.
Definition: sac_model_registration.h:129
pcl::SampleConsensusModelRegistration2D::setProjectionMatrix
void setProjectionMatrix(const Eigen::Matrix3f &projection_matrix)
Set the camera projection matrix.
Definition: sac_model_registration_2d.h:141
pcl::SampleConsensus< PointT >::getInliers
void getInliers(std::vector< int > &inliers) const
Return the best set of inliers found so far for this model.
Definition: sac.h:308
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:88