ransac.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Radu Bogdan Rusu <rusu -=- cs.tum.edu>
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: ransac.cpp 16379 2009-05-29 19:20:46Z hsujohnhsu $
28  *
29  */
30 
33 #include <limits>
34 #include <ransac.h>
35 
36 namespace sample_consensus
37 {
39 
43  RANSAC::RANSAC (SACModel *model, double threshold) : SAC (model)
44  {
45  this->threshold_ = threshold;
46  // Desired probability of choosing at least one sample free from outliers
47  this->probability_ = 0.99;
48  // Maximum number of trials before we give up.
49  this->max_iterations_ = 10000;
50 
51  this->iterations_ = 0;
52  }
53 
55 
58  RANSAC::RANSAC (SACModel* model) : SAC (model) { }
59 
61 
64  bool
66  {
67  iterations_ = 0;
68  int n_best_inliers_count = -INT_MAX;
69  double k = 1.0;
70 
71  std::vector<int> best_model;
72  std::vector<int> best_inliers, inliers;
73  std::vector<int> selection;
74 
75  int n_inliers_count = 0;
76 
77  // Iterate
78  while (iterations_ < k)
79  {
80  // Get X samples which satisfy the model criteria
81  sac_model_->getSamples (iterations_, selection);
82 
83  if (selection.size () == 0) break;
84 
85  // Search for inliers in the point cloud for the current plane model M
87 
89  n_inliers_count = inliers.size ();
90 
91  // Better match ?
92  if (n_inliers_count > n_best_inliers_count)
93  {
94  n_best_inliers_count = n_inliers_count;
95  best_inliers = inliers;
96  //inliers.clear ();
97  best_model = selection;
98 
99  // Compute the k parameter (k=log(z)/log(1-w^n))
100  double w = (double)((double)n_inliers_count / (double)sac_model_->getIndices ()->size ());
101  double p_no_outliers = 1 - pow (w, (double)selection.size ());
102  p_no_outliers = std::max (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
103  p_no_outliers = std::min (1 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
104  k = log (1 - probability_) / log (p_no_outliers);
105  }
106 
107  iterations_ += 1;
108  if (debug > 1)
109  std::cerr << "[RANSAC::computeModel] Trial " << iterations_ << " out of " << ceil (k) << ": " << n_inliers_count << " inliers (best is: " << n_best_inliers_count << " so far)." << std::endl;
111  {
112  if (debug > 0)
113  std::cerr << "[RANSAC::computeModel] RANSAC reached the maximum number of trials." << std::endl;
114  break;
115  }
116  }
117 
118  if (best_model.size () != 0)
119  {
120  if (debug > 0)
121  std::cerr << "[RANSAC::computeModel] Model found: " << n_best_inliers_count << " inliers." << std::endl;
122  sac_model_->setBestModel (best_model);
123  sac_model_->setBestInliers (best_inliers);
124  return (true);
125  }
126  else
127  if (debug > 0)
128  std::cerr << "[RANSAC::computeModel] Unable to find a solution!" << std::endl;
129  return (false);
130  }
131 }
int max_iterations_
Maximum number of iterations before giving up.
Definition: sac.h:197
double threshold_
Distance to model threshold.
Definition: sac.h:200
virtual bool computeModelCoefficients(const std::vector< int > &samples)=0
Check whether the given index samples can form a valid model, compute the model coefficients from the...
SACModel * sac_model_
The underlying data model used (i.e. what is it that we attempt to search for).
Definition: sac.h:188
virtual void selectWithinDistance(const std::vector< double > &model_coefficients, double threshold, std::vector< int > &inliers)=0
Select all the points which respect the given model coefficients as inliers. Pure virtual...
std::vector< int > * getIndices()
Return a pointer to the point cloud data indices.
Definition: sac_model.h:186
int iterations_
Total number of internal loop iterations that we&#39;ve done so far.
Definition: sac.h:194
bool computeModel(int debug=0)
Compute the actual model and find the inliers.
Definition: ransac.cpp:65
RANSAC(SACModel *model)
RANSAC (RAndom SAmple Consensus) main constructor.
Definition: ransac.cpp:58
TFSIMD_FORCE_INLINE const tfScalar & w() const
virtual void getSamples(int &iterations, std::vector< int > &samples)=0
Get a set of random data samples and return them as point indices. Pure virtual.
double probability_
Desired probability of choosing at least one sample free from outliers.
Definition: sac.h:191
std::vector< double > getModelCoefficients()
Return the model coefficients of the best model found so far.
Definition: sac_model.h:180
void setBestModel(std::vector< int > best_model)
Set the best model. Used by SAC methods. Do not call this except if you know what you&#39;re doing...
Definition: sac_model.h:172
void setBestInliers(const std::vector< int > &best_inliers)
Set the best set of inliers. Used by SAC methods. Do not call this except if you know what you&#39;re doi...
Definition: sac_model.h:163


semantic_point_annotator
Author(s): Radu Bogdan Rusu
autogenerated on Mon Jun 10 2019 14:29:03