Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
vpRansac.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Ransac robust algorithm.
32  */
33 
38 #ifndef vpRANSAC_HH
39 #define vpRANSAC_HH
40 
41 #include <ctime>
42 #include <visp3/core/vpColVector.h>
43 #include <visp3/core/vpDebug.h> // debug and trace
44 #include <visp3/core/vpMath.h>
45 #include <visp3/core/vpUniRand.h> // random number generation
46 
65 template <class vpTransformation> class vpRansac
66 {
67 public:
68  static bool ransac(unsigned int npts, const vpColVector &x, unsigned int s, double t, vpColVector &model,
69  vpColVector &inliers, int consensus = 1000, double not_used = 0.0, int maxNbumbersOfTrials = 10000,
70  double *residual = nullptr);
71 };
72 
105 template <class vpTransformation>
106 bool vpRansac<vpTransformation>::ransac(unsigned int npts, const vpColVector &x, unsigned int s, double t,
107  vpColVector &M, vpColVector &inliers, int consensus, double not_used,
108  int maxNbumbersOfTrials, double *residual)
109 {
110  /* bool isplanar; */
111  /* if (s == 4) isplanar = true; */
112  /* else isplanar = false; */
113  (void)not_used;
114  double eps = 1e-6;
115  double p = 0.99; // Desired probability of choosing at least one sample
116  // free from outliers
117 
118  int maxTrials = maxNbumbersOfTrials; // Maximum number of trials before we give up.
119  int maxDataTrials = 1000; // Max number of attempts to select a non-degenerate
120  // data set.
121 
122  if (s < 4)
123  s = 4;
124 
125  // Sentinel value allowing detection of solution failure.
126  bool solutionFind = false;
127  vpColVector bestM;
128  int trialcount = 0;
129  int bestscore = -1;
130  double N = 1; // Dummy initialisation for number of trials.
131 
132  vpUniRand random((const long)time(nullptr));
133  vpColVector bestinliers;
134  unsigned int *ind = new unsigned int[s];
135  int ninliers = 0;
136 
137  while ((N > trialcount) && (consensus > bestscore)) {
138  // Select at random s data points to form a trial model, M.
139  // In selecting these points we have to check that they are not in
140  // a degenerate configuration.
141 
142  bool degenerate = true;
143  int count = 1;
144 
145  while (degenerate == true) {
146  // Generate s random indicies in the range 1..npts
147  for (unsigned int i = 0; i < s; ++i) {
148  ind[i] = static_cast<unsigned int>(ceil(random() * npts)) - 1;
149  }
150 
151  // Test that these points are not a degenerate configuration.
152  degenerate = vpTransformation::degenerateConfiguration(x, ind);
153  // degenerate = feval(degenfn, x(:,ind));
154 
155  // Safeguard against being stuck in this loop forever
156  count = count + 1;
157 
158  if (count > maxDataTrials) {
159  delete[] ind;
160  vpERROR_TRACE("Unable to select a nondegenerate data set");
161  throw(vpException(vpException::fatalError, "Unable to select a non degenerate data set"));
162  // return false; //Useless after a throw() function
163  }
164  }
165  // Fit model to this random selection of data points.
166  vpTransformation::computeTransformation(x, ind, M);
167 
168  vpColVector d;
169  // Evaluate distances between points and model.
170  vpTransformation::computeResidual(x, M, d);
171 
172  // Find the indices of points that are inliers to this model.
173  if (residual != nullptr) {
174  *residual = 0.0;
175  }
176  ninliers = 0;
177  for (unsigned int i = 0; i < npts; ++i) {
178  double resid = fabs(d[i]);
179  if (resid < t) {
180  inliers[i] = 1;
181  ninliers++;
182  if (residual != nullptr) {
183  *residual += fabs(d[i]);
184  }
185  }
186  else
187  inliers[i] = 0;
188  }
189 
190  if (ninliers > bestscore) // Largest set of inliers so far...
191  {
192  bestscore = ninliers; // Record data for this model
193  bestinliers = inliers;
194  bestM = M;
195  solutionFind = true;
196 
197  // Update estimate of N, the number of trials to ensure we pick,
198  // with probability p, a data set with no outliers.
199 
200  double fracinliers = (double)ninliers / (double)npts;
201 
202  double pNoOutliers = 1 - pow(fracinliers, static_cast<int>(s));
203 
204  pNoOutliers = vpMath::maximum(eps, pNoOutliers); // Avoid division by -Inf
205  pNoOutliers = vpMath::minimum(1 - eps, pNoOutliers); // Avoid division by 0.
206  N = (log(1 - p) / log(pNoOutliers));
207  }
208 
209  trialcount = trialcount + 1;
210  // Safeguard against being stuck in this loop forever
211  if (trialcount > maxTrials) {
212  vpTRACE("ransac reached the maximum number of %d trials", maxTrials);
213  break;
214  }
215  }
216 
217  if (solutionFind == true) // We got a solution
218  {
219  M = bestM;
220  inliers = bestinliers;
221  }
222  else {
223  vpTRACE("ransac was unable to find a useful solution");
224  M = 0;
225  }
226 
227  if (residual != nullptr) {
228  if (ninliers > 0) {
229  *residual /= ninliers;
230  }
231  }
232 
233  delete[] ind;
234 
235  return true;
236 }
237 
238 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:252
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:260
This class is a generic implementation of the Ransac algorithm. It cannot be used alone.
Definition: vpRansac.h:66
static bool ransac(unsigned int npts, const vpColVector &x, unsigned int s, double t, vpColVector &model, vpColVector &inliers, int consensus=1000, double not_used=0.0, int maxNbumbersOfTrials=10000, double *residual=nullptr)
RANSAC - Robustly fits a model to data with the RANSAC algorithm.
Definition: vpRansac.h:106
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:123
#define vpTRACE
Definition: vpDebug.h:405
#define vpERROR_TRACE
Definition: vpDebug.h:382