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