Point Cloud Library (PCL) 1.12.0
prosac.hpp
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2009, 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_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
42#define PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
43
44#if defined __GNUC__
45# pragma GCC system_header
46#endif
47
48#include <boost/math/distributions/binomial.hpp>
49#include <pcl/sample_consensus/prosac.h>
50
51//////////////////////////////////////////////////////////////////////////
52// Variable naming uses capital letters to make the comparison with the original paper easier
53template<typename PointT> bool
55{
56 // Warn and exit if no threshold was set
57 if (threshold_ == DBL_MAX)
58 {
59 PCL_ERROR ("[pcl::ProgressiveSampleConsensus::computeModel] No threshold set!\n");
60 return (false);
61 }
62
63 // Initialize some PROSAC constants
64 const int T_N = 200000;
65 const std::size_t N = sac_model_->indices_->size ();
66 const std::size_t m = sac_model_->getSampleSize ();
67 float T_n = static_cast<float> (T_N);
68 for (unsigned int i = 0; i < m; ++i)
69 T_n *= static_cast<float> (m - i) / static_cast<float> (N - i);
70 float T_prime_n = 1.0f;
71 std::size_t I_N_best = 0;
72 float n = static_cast<float> (m);
73
74 // Define the n_Start coefficients from Section 2.2
75 float n_star = static_cast<float> (N);
76 float epsilon_n_star = 0.0;
77 std::size_t k_n_star = T_N;
78
79 // Compute the I_n_star_min of Equation 8
80 std::vector<unsigned int> I_n_star_min (N);
81
82 // Initialize the usual RANSAC parameters
83 iterations_ = 0;
84
85 Indices inliers;
86 Indices selection;
87 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
88
89 // We will increase the pool so the indices_ vector can only contain m elements at first
90 Indices index_pool;
91 index_pool.reserve (N);
92 for (unsigned int i = 0; i < n; ++i)
93 index_pool.push_back (sac_model_->indices_->operator[](i));
94
95 // Iterate
96 while (static_cast<unsigned int> (iterations_) < k_n_star)
97 {
98 // Choose the samples
99
100 // Step 1
101 // According to Equation 5 in the text text, not the algorithm
102 if ((iterations_ == T_prime_n) && (n < n_star))
103 {
104 // Increase the pool
105 ++n;
106 if (n >= N)
107 break;
108 index_pool.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
109 // Update other variables
110 float T_n_minus_1 = T_n;
111 T_n *= (static_cast<float>(n) + 1.0f) / (static_cast<float>(n) + 1.0f - static_cast<float>(m));
112 T_prime_n += std::ceil (T_n - T_n_minus_1);
113 }
114
115 // Step 2
116 sac_model_->indices_->swap (index_pool);
117 selection.clear ();
118 sac_model_->getSamples (iterations_, selection);
119 if (T_prime_n < iterations_)
120 {
121 selection.pop_back ();
122 selection.push_back (sac_model_->indices_->at(static_cast<unsigned int> (n - 1)));
123 }
124
125 // Make sure we use the right indices for testing
126 sac_model_->indices_->swap (index_pool);
127
128 if (selection.empty ())
129 {
130 PCL_ERROR ("[pcl::ProgressiveSampleConsensus::computeModel] No samples could be selected!\n");
131 break;
132 }
133
134 // Search for inliers in the point cloud for the current model
135 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
136 {
137 ++iterations_;
138 continue;
139 }
140
141 // Select the inliers that are within threshold_ from the model
142 inliers.clear ();
143 sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
144
145 std::size_t I_N = inliers.size ();
146
147 // If we find more inliers than before
148 if (I_N > I_N_best)
149 {
150 I_N_best = I_N;
151
152 // Save the current model/inlier/coefficients selection as being the best so far
153 inliers_ = inliers;
154 model_ = selection;
155 model_coefficients_ = model_coefficients;
156
157 // We estimate I_n_star for different possible values of n_star by using the inliers
158 std::sort (inliers.begin (), inliers.end ());
159
160 // Try to find a better n_star
161 // We minimize k_n_star and therefore maximize epsilon_n_star = I_n_star / n_star
162 std::size_t possible_n_star_best = N, I_possible_n_star_best = I_N;
163 float epsilon_possible_n_star_best = static_cast<float>(I_possible_n_star_best) / static_cast<float>(possible_n_star_best);
164
165 // We only need to compute possible better epsilon_n_star for when _n is just about to be removed an inlier
166 std::size_t I_possible_n_star = I_N;
167 for (auto last_inlier = inliers.crbegin (), inliers_end = inliers.crend ();
168 last_inlier != inliers_end;
169 ++last_inlier, --I_possible_n_star)
170 {
171 // The best possible_n_star for a given I_possible_n_star is the index of the last inlier
172 unsigned int possible_n_star = (*last_inlier) + 1;
173 if (possible_n_star <= m)
174 break;
175
176 // If we find a better epsilon_n_star
177 float epsilon_possible_n_star = static_cast<float>(I_possible_n_star) / static_cast<float>(possible_n_star);
178 // Make sure we have a better epsilon_possible_n_star
179 if ((epsilon_possible_n_star > epsilon_n_star) && (epsilon_possible_n_star > epsilon_possible_n_star_best))
180 {
181 // Typo in Equation 7, not (n-m choose i-m) but (n choose i-m)
182 std::size_t I_possible_n_star_min = m
183 + static_cast<std::size_t> (std::ceil (boost::math::quantile (boost::math::complement (boost::math::binomial_distribution<float>(static_cast<float> (possible_n_star), 0.1f), 0.05))));
184 // If Equation 9 is not verified, exit
185 if (I_possible_n_star < I_possible_n_star_min)
186 break;
187
188 possible_n_star_best = possible_n_star;
189 I_possible_n_star_best = I_possible_n_star;
190 epsilon_possible_n_star_best = epsilon_possible_n_star;
191 }
192 }
193
194 // Check if we get a better epsilon
195 if (epsilon_possible_n_star_best > epsilon_n_star)
196 {
197 // update the best value
198 epsilon_n_star = epsilon_possible_n_star_best;
199
200 // Compute the new k_n_star
201 float bottom_log = 1 - std::pow (epsilon_n_star, static_cast<float>(m));
202 if (bottom_log == 0)
203 k_n_star = 1;
204 else if (bottom_log == 1)
205 k_n_star = T_N;
206 else
207 k_n_star = static_cast<int> (std::ceil (std::log (0.05) / std::log (bottom_log)));
208 // It seems weird to have very few iterations, so do have a few (totally empirical)
209 k_n_star = (std::max)(k_n_star, 2 * m);
210 }
211 }
212
213 ++iterations_;
214 if (debug_verbosity_level > 1)
215 PCL_DEBUG ("[pcl::ProgressiveSampleConsensus::computeModel] Trial %d out of %d: %d inliers (best is: %d so far).\n", iterations_, k_n_star, I_N, I_N_best);
216 if (iterations_ > max_iterations_)
217 {
218 if (debug_verbosity_level > 0)
219 PCL_DEBUG ("[pcl::ProgressiveSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
220 break;
221 }
222 }
223
224 if (debug_verbosity_level > 0)
225 PCL_DEBUG ("[pcl::ProgressiveSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), I_N_best);
226
227 if (model_.empty ())
228 {
229 inliers_.clear ();
230 return (false);
231 }
232
233 return (true);
234}
235
236#define PCL_INSTANTIATE_ProgressiveSampleConsensus(T) template class PCL_EXPORTS pcl::ProgressiveSampleConsensus<T>;
237
238#endif // PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: prosac.hpp:54
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133