42 #include <visp3/core/vpColVector.h>
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpMath.h>
45 #include <visp3/core/vpUniRand.h>
65 template <
class vpTransformation>
class vpRansac
69 vpColVector &inliers,
int consensus = 1000,
double not_used = 0.0,
int maxNbumbersOfTrials = 10000,
70 double *residual =
nullptr);
105 template <
class vpTransformation>
108 int maxNbumbersOfTrials,
double *residual)
118 int maxTrials = maxNbumbersOfTrials;
119 int maxDataTrials = 1000;
126 bool solutionFind =
false;
132 vpUniRand random((
const long)time(
nullptr));
134 unsigned int *ind =
new unsigned int[s];
137 while ((N > trialcount) && (consensus > bestscore)) {
142 bool degenerate =
true;
145 while (degenerate ==
true) {
147 for (
unsigned int i = 0; i < s; i++)
148 ind[i] = (
unsigned int)ceil(random() * npts) - 1;
151 degenerate = vpTransformation::degenerateConfiguration(x, ind);
157 if (count > maxDataTrials) {
165 vpTransformation::computeTransformation(x, ind, M);
169 vpTransformation::computeResidual(x, M, d);
172 if (residual !=
nullptr) {
176 for (
unsigned int i = 0; i < npts; i++) {
177 double resid = fabs(d[i]);
181 if (residual !=
nullptr) {
182 *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;
222 vpTRACE(
"ransac was unable to find a useful solution");
226 if (residual !=
nullptr) {
228 *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=nullptr)
RANSAC - Robustly fits a model to data with the RANSAC algorithm.
Class for generating random numbers with uniform probability density.