Point Cloud Library (PCL)  1.9.1
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
53 template<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 size_t N = sac_model_->indices_->size ();
66  const 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  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  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  std::vector<int> inliers;
86  std::vector<int> selection;
87  Eigen::VectorXf model_coefficients;
88 
89  // We will increase the pool so the indices_ vector can only contain m elements at first
90  std::vector<int> 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 += ceilf (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  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  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  size_t I_possible_n_star = I_N;
167  for (std::vector<int>::const_reverse_iterator last_inlier = inliers.rbegin (),
168  inliers_end = inliers.rend ();
169  last_inlier != inliers_end;
170  ++last_inlier, --I_possible_n_star)
171  {
172  // The best possible_n_star for a given I_possible_n_star is the index of the last inlier
173  unsigned int possible_n_star = (*last_inlier) + 1;
174  if (possible_n_star <= m)
175  break;
176 
177  // If we find a better epsilon_n_star
178  float epsilon_possible_n_star = static_cast<float>(I_possible_n_star) / static_cast<float>(possible_n_star);
179  // Make sure we have a better epsilon_possible_n_star
180  if ((epsilon_possible_n_star > epsilon_n_star) && (epsilon_possible_n_star > epsilon_possible_n_star_best))
181  {
182  // Typo in Equation 7, not (n-m choose i-m) but (n choose i-m)
183  size_t I_possible_n_star_min = m
184  + static_cast<size_t> (ceil (boost::math::quantile (boost::math::complement (boost::math::binomial_distribution<float>(static_cast<float> (possible_n_star), 0.1f), 0.05))));
185  // If Equation 9 is not verified, exit
186  if (I_possible_n_star < I_possible_n_star_min)
187  break;
188 
189  possible_n_star_best = possible_n_star;
190  I_possible_n_star_best = I_possible_n_star;
191  epsilon_possible_n_star_best = epsilon_possible_n_star;
192  }
193  }
194 
195  // Check if we get a better epsilon
196  if (epsilon_possible_n_star_best > epsilon_n_star)
197  {
198  // update the best value
199  epsilon_n_star = epsilon_possible_n_star_best;
200 
201  // Compute the new k_n_star
202  float bottom_log = 1 - std::pow (epsilon_n_star, static_cast<float>(m));
203  if (bottom_log == 0)
204  k_n_star = 1;
205  else if (bottom_log == 1)
206  k_n_star = T_N;
207  else
208  k_n_star = static_cast<int> (ceil (log (0.05) / log (bottom_log)));
209  // It seems weird to have very few iterations, so do have a few (totally empirical)
210  k_n_star = (std::max)(k_n_star, 2 * m);
211  }
212  }
213 
214  ++iterations_;
215  if (debug_verbosity_level > 1)
216  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);
217  if (iterations_ > max_iterations_)
218  {
219  if (debug_verbosity_level > 0)
220  PCL_DEBUG ("[pcl::ProgressiveSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
221  break;
222  }
223  }
224 
225  if (debug_verbosity_level > 0)
226  PCL_DEBUG ("[pcl::ProgressiveSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), I_N_best);
227 
228  if (model_.empty ())
229  {
230  inliers_.clear ();
231  return (false);
232  }
233 
234  return (true);
235 }
236 
237 #define PCL_INSTANTIATE_ProgressiveSampleConsensus(T) template class PCL_EXPORTS pcl::ProgressiveSampleConsensus<T>;
238 
239 #endif // PCL_SAMPLE_CONSENSUS_IMPL_PROSAC_H_
pcl::ProgressiveSampleConsensus::computeModel
bool computeModel(int debug_verbosity_level=0)
Compute the actual model and find the inliers.
Definition: prosac.hpp:54