25 template <
typename NUMTYPE>
28 TRansacFitFunctor fit_func,
29 TRansacDistanceFunctor dist_func,
30 TRansacDegenerateFunctor degen_func,
31 const double distanceThreshold,
32 const unsigned int minimumSizeSamplesToFit,
41 ASSERT_(minimumSizeSamplesToFit>=1)
51 const size_t maxDataTrials = 100;
53 out_best_model.setSize(0,0);
54 out_best_inliers.clear();
56 size_t trialcount = 0;
57 size_t bestscore = std::string::npos;
62 while (N > trialcount)
69 std::vector< CMatrixTemplateNumeric<NUMTYPE> > MODELS;
74 ind.resize( minimumSizeSamplesToFit );
80 degenerate = degen_func(
data, ind);
86 fit_func(
data,ind,MODELS);
92 degenerate = MODELS.empty();
96 if (++
count > maxDataTrials)
109 unsigned int bestModelIdx = 1000;
113 dist_func(
data,MODELS, NUMTYPE(distanceThreshold), bestModelIdx, inliers);
114 ASSERT_( bestModelIdx<MODELS.size() );
118 const size_t ninliers = inliers.size();
119 bool update_estim_num_iters = (trialcount==0);
121 if (ninliers > bestscore || (bestscore==std::string::npos && ninliers!=0))
123 bestscore = ninliers;
125 out_best_model = MODELS[bestModelIdx];
126 out_best_inliers = inliers;
127 update_estim_num_iters=
true;
130 if (update_estim_num_iters)
134 double fracinliers = ninliers/
static_cast<double>(Npts);
135 double pNoOutliers = 1 - pow(fracinliers,static_cast<double>(minimumSizeSamplesToFit));
137 pNoOutliers = std::max( std::numeric_limits<double>::epsilon(), pNoOutliers);
138 pNoOutliers =
std::min(1.0 - std::numeric_limits<double>::epsilon() , pNoOutliers);
140 N =
static_cast<size_t>(log(1 -
p) / log(pNoOutliers));
141 MRPT_LOG_DEBUG(
format(
"Iter #%u Estimated number of iters: %u pNoOutliers = %f #inliers: %u\n", (
unsigned)trialcount ,(
unsigned)N,pNoOutliers, (
unsigned)ninliers));
146 MRPT_LOG_DEBUG(
format(
"trial %u out of %u \r",(
unsigned int)trialcount, (
unsigned int)ceil(static_cast<double>(N))) );
149 if (trialcount > maxIter)
151 MRPT_LOG_WARN(
format(
"Warning: maximum number of trials (%u) reached\n", (
unsigned)maxIter ));
156 if (
size(out_best_model,1)>0)
177 #ifdef HAVE_LONG_DOUBLE A namespace of pseudo-random numbers genrators of diferent distributions.
GLuint GLuint GLsizei count
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
This base provides a set of functions for maths stuff.
#define MRPT_LOG_WARN(_STRING)
#define MRPT_LOG_INFO(_STRING)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
#define MRPT_LOG_DEBUG(_STRING)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A matrix of dynamic size.
std::vector< size_t > vector_size_t
A generic RANSAC implementation with models as matrices.
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
GLsizei GLsizei GLenum GLenum const GLvoid * data
std::vector< size_t > vector_size_t