49 #include <visp3/core/vpColVector.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpUniRand.h>
72 template <
class vpTransformation>
class vpRansac
76 vpColVector &inliers,
int consensus = 1000,
double not_used = 0.0,
int maxNbumbersOfTrials = 10000,
77 double *residual = NULL);
112 template <
class vpTransformation>
115 int maxNbumbersOfTrials,
double *residual)
125 int maxTrials = maxNbumbersOfTrials;
126 int maxDataTrials = 1000;
133 bool solutionFind =
false;
139 vpUniRand random((
const long)time(NULL));
141 unsigned int *ind =
new unsigned int[s];
145 while ((N > trialcount) && (consensus > bestscore)) {
150 bool degenerate =
true;
153 while (degenerate ==
true) {
155 for (
unsigned int i = 0; i < s; i++)
156 ind[i] = (
unsigned int)ceil(random() * npts) - 1;
159 degenerate = vpTransformation::degenerateConfiguration(x, ind);
165 if (count > maxDataTrials) {
173 vpTransformation::computeTransformation(x, ind, M);
177 vpTransformation::computeResidual(x, M, d);
180 if (residual != NULL)
183 for (
unsigned int i = 0; i < npts; i++) {
184 double resid = fabs(d[i]);
188 if (residual != NULL) {
189 *residual += fabs(d[i]);
195 if (ninliers > bestscore)
197 bestscore = ninliers;
198 bestinliers = inliers;
205 double fracinliers = (double)ninliers / (
double)npts;
207 double pNoOutliers = 1 - pow(fracinliers,
static_cast<int>(s));
211 N = (log(1 - p) / log(pNoOutliers));
214 trialcount = trialcount + 1;
216 if (trialcount > maxTrials) {
217 vpTRACE(
"ransac reached the maximum number of %d trials", maxTrials);
223 if (solutionFind ==
true)
226 inliers = bestinliers;
228 vpTRACE(
"ransac was unable to find a useful solution");
232 if (residual != NULL) {
234 *residual /= ninliers;
Implementation of column vector and the associated operations.
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)
This class is a generic implementation of the Ransac algorithm. It cannot be used alone.
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=NULL)
RANSAC - Robustly fits a model to data with the RANSAC algorithm.
Class for generating random numbers with uniform probability density.