50 #include <visp3/core/vpUniRand.h>
51 #include <visp3/core/vpDebug.h>
52 #include <visp3/core/vpColVector.h>
53 #include <visp3/core/vpMath.h>
74 template <
class vpTransformation>
78 static bool ransac(
unsigned int npts,
80 unsigned int s,
double t,
84 double not_used = 0.0,
85 const int maxNbumbersOfTrials = 10000);
118 template <
class vpTransformation>
121 unsigned int s,
double t,
126 const int maxNbumbersOfTrials)
136 int maxTrials = maxNbumbersOfTrials;
137 int maxDataTrials = 1000;
144 bool solutionFind = false ;
150 vpUniRand random((
const long)time(NULL)) ;
152 unsigned int *ind =
new unsigned int [s] ;
155 double residual = 0.0;
156 while(( N > trialcount) && (consensus > bestscore))
162 bool degenerate =
true;
165 while ( degenerate ==
true)
168 for (
unsigned int i=0 ; i < s ; i++)
169 ind[i] = (
unsigned int)ceil(random()*npts) -1;
172 degenerate = vpTransformation::degenerateConfiguration(x,ind) ;
178 if (count > maxDataTrials) {
186 vpTransformation::computeTransformation(x,ind, M);
190 vpTransformation::computeResidual(x, M, d) ;
195 for (
unsigned int i=0 ; i < npts ; i++)
197 double resid = fabs(d[i]);
202 residual += fabs(d[i]);
207 if (ninliers > bestscore)
209 bestscore = ninliers;
210 bestinliers = inliers;
212 solutionFind = true ;
217 double fracinliers = (double)ninliers / (
double)npts;
219 double pNoOutliers = 1 - pow(fracinliers,static_cast<int>(s));
223 N = (log(1-p)/log(pNoOutliers));
226 trialcount = trialcount+1;
228 if (trialcount > maxTrials)
230 vpTRACE(
"ransac reached the maximum number of %d trials", maxTrials);
236 if (solutionFind==
true)
239 inliers = bestinliers;
243 vpTRACE(
"ransac was unable to find a useful solution");
248 residual /= ninliers;
error that can be emited by ViSP classes.
static Type maximum(const Type &a, const Type &b)
static Type minimum(const Type &a, const Type &b)
Implementation of column vector and the associated operations.
static bool ransac(unsigned int npts, vpColVector &x, unsigned int s, double t, vpColVector &model, vpColVector &inliers, int consensus=1000, double not_used=0.0, const int maxNbumbersOfTrials=10000)
RANSAC - Robustly fits a model to data with the RANSAC algorithm.
This class is a generic implementation of the Ransac algorithm. It cannot be used alone...
Class for generating random numbers with uniform probability density.