44 #include <visp3/core/vpColVector.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpUniRand.h>
67 template <
class vpTransformation>
class vpRansac
71 vpColVector &inliers,
int consensus = 1000,
double not_used = 0.0,
int maxNbumbersOfTrials = 10000,
72 double *residual = NULL);
107 template <
class vpTransformation>
110 int maxNbumbersOfTrials,
double *residual)
120 int maxTrials = maxNbumbersOfTrials;
121 int maxDataTrials = 1000;
128 bool solutionFind =
false;
134 vpUniRand random((
const long)time(NULL));
136 unsigned int *ind =
new unsigned int[s];
139 while ((N > trialcount) && (consensus > bestscore)) {
144 bool degenerate =
true;
147 while (degenerate ==
true) {
149 for (
unsigned int i = 0; i < s; i++)
150 ind[i] = (
unsigned int)ceil(random() * npts) - 1;
153 degenerate = vpTransformation::degenerateConfiguration(x, ind);
159 if (count > maxDataTrials) {
167 vpTransformation::computeTransformation(x, ind, M);
171 vpTransformation::computeResidual(x, M, d);
174 if (residual != NULL)
177 for (
unsigned int i = 0; i < npts; i++) {
178 double resid = fabs(d[i]);
182 if (residual != NULL) {
183 *residual += fabs(d[i]);
189 if (ninliers > bestscore)
191 bestscore = ninliers;
192 bestinliers = inliers;
199 double fracinliers = (double)ninliers / (
double)npts;
201 double pNoOutliers = 1 - pow(fracinliers,
static_cast<int>(s));
205 N = (log(1 - p) / log(pNoOutliers));
208 trialcount = trialcount + 1;
210 if (trialcount > maxTrials) {
211 vpTRACE(
"ransac reached the maximum number of %d trials", maxTrials);
216 if (solutionFind ==
true)
219 inliers = bestinliers;
221 vpTRACE(
"ransac was unable to find a useful solution");
225 if (residual != NULL) {
227 *residual /= ninliers;
Implementation of column vector and the associated operations.
error that can be emitted 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.