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