Point Cloud Library (PCL) 1.12.0
Loading...
Searching...
No Matches
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
47namespace pcl {
48
49namespace registration {
50
51template <typename PointT>
52void
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());
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
91 // Pass the target_indices
92 model->setInputTarget(target_, target_indices);
93 model->setProjectionMatrix(projection_matrix_);
94
95 // Create a RANSAC model
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());
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
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());
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)
128
130 for (std::size_t i = 0; i < inliers.size(); ++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_
Iterator class for point clouds with or without given indices.
std::size_t size() const
Size of the range the iterator is going through.
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.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences