54 #include <visp/vpNoise.h>
55 #include <visp/vpDebug.h>
56 #include <visp/vpColVector.h>
57 #include <visp/vpMath.h>
78 template <
class vpTransformation>
82 static bool ransac(
unsigned int npts,
84 unsigned int s,
double t,
88 double areaThreshold = 0.0,
89 const int maxNbumbersOfTrials = 10000);
122 template <
class vpTransformation>
125 unsigned int s,
double t,
130 const int maxNbumbersOfTrials)
140 int maxTrials = maxNbumbersOfTrials;
141 int maxDataTrials = 1000;
148 bool solutionFind = false ;
154 vpUniRand random((
const long)time(NULL)) ;
156 unsigned int *ind =
new unsigned int [s] ;
159 double residual = 0.0;
160 while(( N > trialcount) && (consensus > bestscore))
166 bool degenerate =
true;
169 while ( degenerate ==
true)
172 for (
unsigned int i=0 ; i < s ; i++)
173 ind[i] = (
unsigned int)ceil(random()*npts) -1;
176 degenerate = vpTransformation::degenerateConfiguration(x,ind) ;
182 if (count > maxDataTrials) {
190 vpTransformation::computeTransformation(x,ind, M);
194 vpTransformation::computeResidual(x, M, d) ;
199 for (
unsigned int i=0 ; i < npts ; i++)
201 double resid = fabs(d[i]);
206 residual += fabs(d[i]);
211 if (ninliers > bestscore)
213 bestscore = ninliers;
214 bestinliers = inliers;
216 solutionFind = true ;
221 double fracinliers = (double)ninliers / (
double)npts;
223 double pNoOutliers = 1 - pow(fracinliers,static_cast<int>(s));
227 N = (log(1-p)/log(pNoOutliers));
230 trialcount = trialcount+1;
232 if (trialcount > maxTrials)
234 vpTRACE(
"ransac reached the maximum number of %d trials", maxTrials);
240 if (solutionFind==
true)
243 inliers = bestinliers;
247 vpTRACE(
"ransac was unable to find a useful solution");
252 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)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
static bool ransac(unsigned int npts, vpColVector &x, unsigned int s, double t, vpColVector &model, vpColVector &inliers, int consensus=1000, double areaThreshold=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.