54 #include <visp/vpNoise.h>
55 #include <visp/vpDebug.h>
56 #include <visp/vpColVector.h>
57 #include <visp/vpMath.h>
92 template <
class vpTransformation>
96 static bool ransac(
unsigned int npts,
98 unsigned int s,
double t,
101 int consensus = 1000,
102 double areaThreshold = 0.0,
103 const int maxNbumbersOfTrials = 10000);
136 template <
class vpTransformation>
139 unsigned int s,
double t,
144 const int maxNbumbersOfTrials
157 int maxTrials = maxNbumbersOfTrials;
158 int maxDataTrials = 1000;
162 bool solutionFind = false ;
168 vpUniRand random((
const long)time(NULL)) ;
170 unsigned int *ind =
new unsigned int [s] ;
173 double residual = 0.0;
174 while(( N > trialcount) && (consensus > bestscore))
180 bool degenerate =
true;
183 while ( degenerate ==
true)
186 for (
unsigned int i=0 ; i < s ; i++)
187 ind[i] = (
unsigned int)ceil(random()*npts) -1;
190 degenerate = vpTransformation::degenerateConfiguration(x,ind) ;
196 if (count > maxDataTrials) {
204 vpTransformation::computeTransformation(x,ind, M);
208 vpTransformation::computeResidual(x, M, d) ;
213 for (
unsigned int i=0 ; i < npts ; i++)
215 double resid = fabs(d[i]);
220 residual += fabs(d[i]);
225 if (ninliers > bestscore)
227 bestscore = ninliers;
228 bestinliers = inliers;
230 solutionFind = true ;
235 double fracinliers = (double)ninliers / (
double)npts;
237 double pNoOutliers = 1 - pow(fracinliers,static_cast<int>(s));
241 N = (log(1-p)/log(pNoOutliers));
244 trialcount = trialcount+1;
246 if (trialcount > maxTrials)
248 vpTRACE(
"ransac reached the maximum number of %d trials", maxTrials);
254 if (solutionFind==
true)
257 inliers = bestinliers;
261 vpTRACE(
"ransac was unable to find a useful solution");
266 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.