Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
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] = (unsigned int)ceil(random() * npts) - 1;
149 
150  // Test that these points are not a degenerate configuration.
151  degenerate = vpTransformation::degenerateConfiguration(x, ind);
152  // degenerate = feval(degenfn, x(:,ind));
153 
154  // Safeguard against being stuck in this loop forever
155  count = count + 1;
156 
157  if (count > maxDataTrials) {
158  delete[] ind;
159  vpERROR_TRACE("Unable to select a nondegenerate data set");
160  throw(vpException(vpException::fatalError, "Unable to select a non degenerate data set"));
161  // return false; //Useless after a throw() function
162  }
163  }
164  // Fit model to this random selection of data points.
165  vpTransformation::computeTransformation(x, ind, M);
166 
167  vpColVector d;
168  // Evaluate distances between points and model.
169  vpTransformation::computeResidual(x, M, d);
170 
171  // Find the indices of points that are inliers to this model.
172  if (residual != nullptr) {
173  *residual = 0.0;
174  }
175  ninliers = 0;
176  for (unsigned int i = 0; i < npts; i++) {
177  double resid = fabs(d[i]);
178  if (resid < t) {
179  inliers[i] = 1;
180  ninliers++;
181  if (residual != nullptr) {
182  *residual += fabs(d[i]);
183  }
184  }
185  else
186  inliers[i] = 0;
187  }
188 
189  if (ninliers > bestscore) // Largest set of inliers so far...
190  {
191  bestscore = ninliers; // Record data for this model
192  bestinliers = inliers;
193  bestM = M;
194  solutionFind = true;
195 
196  // Update estimate of N, the number of trials to ensure we pick,
197  // with probability p, a data set with no outliers.
198 
199  double fracinliers = (double)ninliers / (double)npts;
200 
201  double pNoOutliers = 1 - pow(fracinliers, static_cast<int>(s));
202 
203  pNoOutliers = vpMath::maximum(eps, pNoOutliers); // Avoid division by -Inf
204  pNoOutliers = vpMath::minimum(1 - eps, pNoOutliers); // Avoid division by 0.
205  N = (log(1 - p) / log(pNoOutliers));
206  }
207 
208  trialcount = trialcount + 1;
209  // Safeguard against being stuck in this loop forever
210  if (trialcount > maxTrials) {
211  vpTRACE("ransac reached the maximum number of %d trials", maxTrials);
212  break;
213  }
214  }
215 
216  if (solutionFind == true) // We got a solution
217  {
218  M = bestM;
219  inliers = bestinliers;
220  }
221  else {
222  vpTRACE("ransac was unable to find a useful solution");
223  M = 0;
224  }
225 
226  if (residual != nullptr) {
227  if (ninliers > 0) {
228  *residual /= ninliers;
229  }
230  }
231 
232  delete[] ind;
233 
234  return true;
235 }
236 
237 #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