Point Cloud Library (PCL)
1.15.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
registration
impl
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
Po
int
T>
52
void
53
CorrespondenceRejectorSampleConsensus2D<PointT>::getRemainingCorrespondences
(
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
pcl::Indices
source_indices(nr_correspondences);
80
pcl::Indices
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
89
typename
pcl::SampleConsensusModelRegistration2D<PointT>::Ptr
model(
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
const
auto
& inliers = sac.
getInliers
();
114
115
if
(inliers.size() < 3) {
116
PCL_ERROR(
"[pcl::registration::%s::getRemainingCorrespondences] Less than 3 "
117
"correspondences found!\n"
,
118
getClassName
().c_str());
119
remaining_correspondences = original_correspondences;
120
best_transformation_
.setIdentity();
121
return
;
122
}
123
124
std::unordered_map<int, int> index_to_correspondence;
125
for
(
int
i = 0; i < nr_correspondences; ++i)
126
index_to_correspondence[original_correspondences[i].index_query] = i;
127
128
remaining_correspondences.resize(inliers.size());
129
for
(std::size_t i = 0; i < inliers.size(); ++i)
130
remaining_correspondences[i] =
131
original_correspondences[index_to_correspondence[inliers[i]]];
132
133
// get the best transformation
134
const
Eigen::VectorXf& model_coefficients = sac.
getModelCoefficients
();
135
best_transformation_
.row(0) = model_coefficients.segment<4>(0);
136
best_transformation_
.row(1) = model_coefficients.segment<4>(4);
137
best_transformation_
.row(2) = model_coefficients.segment<4>(8);
138
best_transformation_
.row(3) = model_coefficients.segment<4>(12);
139
}
140
141
}
// namespace registration
142
}
// namespace pcl
143
144
#endif
// PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_2D_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::SampleConsensusModelRegistration2D
SampleConsensusModelRegistration2D defines a model for Point-To-Point registration outlier rejection ...
Definition
sac_model_registration_2d.h:53
pcl::SampleConsensusModelRegistration2D::Ptr
shared_ptr< SampleConsensusModelRegistration2D< PointT > > Ptr
Definition
sac_model_registration_2d.h:70
pcl::SampleConsensusModelRegistration2D::setProjectionMatrix
void setProjectionMatrix(const Eigen::Matrix3f &projection_matrix)
Set the camera projection matrix.
Definition
sac_model_registration_2d.h:142
pcl::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target)
Set the input point cloud target.
Definition
sac_model_registration.h:130
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::registration::CorrespondenceRejectorSampleConsensus2D::projection_matrix_
Eigen::Matrix3f projection_matrix_
Camera projection matrix.
Definition
correspondence_rejection_sample_consensus_2d.h:151
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_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