42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpColVector.h>
44 #include <visp3/core/vpMath.h>
45 #include <visp3/core/vpUniRand.h>
66 template <
class vpTransformation>
class vpRansac
70 vpColVector &inliers,
int consensus = 1000,
double not_used = 0.0,
int maxNbumbersOfTrials = 10000,
71 double *residual =
nullptr);
106 template <
class vpTransformation>
109 int maxNbumbersOfTrials,
double *residual)
121 int maxTrials = maxNbumbersOfTrials;
122 int maxDataTrials = 1000;
124 const unsigned int magic_4 = 4;
130 bool solutionFind =
false;
136 vpUniRand random(
static_cast<long>(time(
nullptr)));
138 unsigned int *ind =
new unsigned int[s];
141 while ((N > trialcount) && (consensus > bestscore) && (trialcount > maxTrials)) {
146 bool degenerate =
true;
149 while (degenerate ==
true) {
151 for (
unsigned int i = 0; i < s; ++i) {
152 ind[i] =
static_cast<unsigned int>(ceil(random() * npts)) - 1;
156 degenerate = vpTransformation::degenerateConfiguration(x, ind);
163 if (count > maxDataTrials) {
169 vpTransformation::computeTransformation(x, ind, M);
173 vpTransformation::computeResidual(x, M, d);
176 if (residual !=
nullptr) {
180 for (
unsigned int i = 0; i < npts; ++i) {
181 double resid = fabs(d[i]);
185 if (residual !=
nullptr) {
186 *residual += fabs(d[i]);
194 if (ninliers > bestscore)
196 bestscore = ninliers;
197 bestinliers = inliers;
204 double fracinliers =
static_cast<double>(ninliers) /
static_cast<double>(npts);
206 double pNoOutliers = 1 - pow(fracinliers,
static_cast<int>(s));
210 N = (log(1 - p) / log(pNoOutliers));
216 if (trialcount > maxTrials) {
217 std::cout <<
"Warning: ransac reached the maximum number of " << maxTrials <<
" trials" << std::endl;
219 if (solutionFind ==
true) {
221 inliers = bestinliers;
224 std::cout <<
"Warning: ransac was unable to find a useful solution" << std::endl;
228 if (residual !=
nullptr) {
230 *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.