53 #include <visp3/core/vpColVector.h> 54 #include <visp3/core/vpMath.h> 55 #include <visp3/core/vpRansac.h> 56 #include <visp3/vision/vpPose.h> 57 #include <visp3/vision/vpPoseException.h> 59 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 68 struct CompareObjectPointDegenerate {
69 bool operator()(
const vpPoint &point1,
const vpPoint &point2)
const 71 if (point1.
oP[0] - point2.
oP[0] < -eps)
73 if (point1.
oP[0] - point2.
oP[0] > eps)
76 if (point1.
oP[1] - point2.
oP[1] < -eps)
78 if (point1.
oP[1] - point2.
oP[1] > eps)
81 if (point1.
oP[2] - point2.
oP[2] < -eps)
83 if (point1.
oP[2] - point2.
oP[2] > eps)
91 struct CompareImagePointDegenerate {
92 bool operator()(
const vpPoint &point1,
const vpPoint &point2)
const 94 if (point1.
p[0] - point2.
p[0] < -eps)
96 if (point1.
p[0] - point2.
p[0] > eps)
99 if (point1.
p[1] - point2.
p[1] < -eps)
101 if (point1.
p[1] - point2.
p[1] > eps)
109 struct FindDegeneratePoint {
110 explicit FindDegeneratePoint(
const vpPoint &pt) : m_pt(pt) {}
112 bool operator()(
const vpPoint &pt)
114 return ((std::fabs(m_pt.oP[0] - pt.
oP[0]) < eps && std::fabs(m_pt.oP[1] - pt.
oP[1]) < eps &&
115 std::fabs(m_pt.oP[2] - pt.
oP[2]) < eps) ||
116 (std::fabs(m_pt.p[0] - pt.
p[0]) < eps && std::fabs(m_pt.p[1] - pt.
p[1]) < eps));
123 bool vpPose::RansacFunctor::poseRansacImpl()
125 const unsigned int size = (
unsigned int)m_listOfUniquePoints.size();
126 unsigned int nbMinRandom = 4;
131 bool foundSolution =
false;
132 while (nbTrials < m_ransacMaxTrials && m_nbInliers < m_ransacNbInlierConsensus) {
134 std::vector<unsigned int> cur_consensus;
136 std::vector<unsigned int> cur_outliers;
138 std::vector<unsigned int> cur_randoms;
141 std::vector<vpPoint> cur_inliers;
152 std::vector<bool> usedPt(size,
false);
155 for (
unsigned int i = 0; i < nbMinRandom;) {
156 if ((
size_t)std::count(usedPt.begin(), usedPt.end(),
true) == usedPt.size()) {
162 unsigned int r_ = m_uniRand.uniform(0, size);
166 r_ = m_uniRand.uniform(0, size);
170 vpPoint pt = m_listOfUniquePoints[r_];
172 bool degenerate =
false;
173 if (m_checkDegeneratePoints) {
174 if (std::find_if(poseMin.listOfPoints.begin(), poseMin.listOfPoints.end(), FindDegeneratePoint(pt)) !=
175 poseMin.listOfPoints.end()) {
182 cur_randoms.push_back(r_);
188 if (poseMin.
npt < nbMinRandom) {
194 bool is_valid_lagrange =
false;
195 bool is_valid_dementhon =
false;
198 double r_lagrange = DBL_MAX;
199 double r_dementhon = DBL_MAX;
204 is_valid_lagrange =
true;
210 is_valid_dementhon =
true;
215 is_valid_lagrange =
false;
216 r_lagrange = DBL_MAX;
220 is_valid_dementhon =
false;
221 r_dementhon = DBL_MAX;
226 if (is_valid_lagrange || is_valid_dementhon) {
228 if (r_lagrange < r_dementhon) {
230 cMo_tmp = cMo_lagrange;
233 cMo_tmp = cMo_dementhon;
235 r = sqrt(r) / (double)nbMinRandom;
238 bool isPoseValid =
true;
239 if (m_func != NULL) {
240 isPoseValid = m_func(cMo_tmp);
249 if (isPoseValid && r < m_ransacThreshold) {
250 unsigned int nbInliersCur = 0;
251 unsigned int iter = 0;
252 for (std::vector<vpPoint>::const_iterator it = m_listOfUniquePoints.begin(); it != m_listOfUniquePoints.end();
258 if (error < m_ransacThreshold) {
259 bool degenerate =
false;
260 if (m_checkDegeneratePoints) {
261 if (std::find_if(cur_inliers.begin(), cur_inliers.end(), FindDegeneratePoint(*it)) != cur_inliers.end()) {
270 cur_consensus.push_back(iter);
271 cur_inliers.push_back(*it);
273 cur_outliers.push_back(iter);
276 cur_outliers.push_back(iter);
280 if (nbInliersCur > m_nbInliers) {
281 foundSolution =
true;
282 m_best_consensus = cur_consensus;
283 m_nbInliers = nbInliersCur;
288 if (nbTrials >= m_ransacMaxTrials) {
289 foundSolution =
true;
299 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 300 if (m_nbInliers >= m_ransacNbInlierConsensus)
304 return foundSolution;
324 if (listP.size() != listOfPoints.size()) {
325 std::cerr <<
"You should not modify vpPose::listP!" << std::endl;
326 listOfPoints = std::vector<vpPoint>(listP.begin(), listP.end());
329 ransacInliers.clear();
330 ransacInlierIndex.clear();
332 std::vector<unsigned int> best_consensus;
333 unsigned int nbInliers = 0;
337 if (listOfPoints.size() < 4) {
341 std::vector<vpPoint> listOfUniquePoints;
342 std::map<size_t, size_t> mapOfUniquePointIndex;
345 bool prefilterDegeneratePoints = ransacFlag == PREFILTER_DEGENERATE_POINTS;
346 bool checkDegeneratePoints = ransacFlag == CHECK_DEGENERATE_POINTS;
348 if (prefilterDegeneratePoints) {
350 std::map<vpPoint, size_t, CompareObjectPointDegenerate> filterObjectPointMap;
352 for (std::vector<vpPoint>::const_iterator it_pt = listOfPoints.begin(); it_pt != listOfPoints.end();
353 ++it_pt, index_pt++) {
354 if (filterObjectPointMap.find(*it_pt) == filterObjectPointMap.end()) {
355 filterObjectPointMap[*it_pt] = index_pt;
359 std::map<vpPoint, size_t, CompareImagePointDegenerate> filterImagePointMap;
360 for (std::map<vpPoint, size_t, CompareObjectPointDegenerate>::const_iterator it = filterObjectPointMap.begin();
361 it != filterObjectPointMap.end(); ++it) {
362 if (filterImagePointMap.find(it->first) == filterImagePointMap.end()) {
363 filterImagePointMap[it->first] = it->second;
365 listOfUniquePoints.push_back(it->first);
366 mapOfUniquePointIndex[listOfUniquePoints.size() - 1] = it->second;
371 listOfUniquePoints = listOfPoints;
374 for (std::vector<vpPoint>::const_iterator it_pt = listOfPoints.begin(); it_pt != listOfPoints.end();
375 ++it_pt, index_pt++) {
376 mapOfUniquePointIndex[index_pt] = index_pt;
380 if (listOfUniquePoints.size() < 4) {
384 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 385 unsigned int nbThreads = 1;
386 bool executeParallelVersion = useParallelRansac;
388 bool executeParallelVersion =
false;
391 if (executeParallelVersion) {
392 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 393 if (nbParallelRansacThreads <= 0) {
395 nbThreads = std::thread::hardware_concurrency();
396 if (nbThreads <= 1) {
398 executeParallelVersion =
false;
404 bool foundSolution =
false;
406 if (executeParallelVersion) {
407 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 408 std::vector<std::thread> threadpool;
409 std::vector<RansacFunctor> ransacWorkers;
410 unsigned int nthreads = std::thread::hardware_concurrency();
412 int splitTrials = ransacMaxTrials / nthreads;
413 std::atomic<bool> abort{
false};
414 for (
size_t i = 0; i < (size_t)nthreads; i++) {
415 unsigned int initial_seed = (
unsigned int)i;
416 if (i < (
size_t)nthreads - 1) {
417 ransacWorkers.emplace_back(cMo, ransacNbInlierConsensus, splitTrials, ransacThreshold, initial_seed,
418 checkDegeneratePoints, listOfUniquePoints, func, abort);
420 int maxTrialsRemainder = ransacMaxTrials - splitTrials * (nbThreads - 1);
421 ransacWorkers.emplace_back(cMo, ransacNbInlierConsensus, maxTrialsRemainder, ransacThreshold, initial_seed,
422 checkDegeneratePoints, listOfUniquePoints, func, abort);
426 for (
auto& worker : ransacWorkers) {
427 threadpool.emplace_back(&RansacFunctor::operator(), &worker);
430 for (
auto& th : threadpool) {
434 bool successRansac =
false;
435 size_t best_consensus_size = 0;
436 for (
auto &worker : ransacWorkers) {
437 if (worker.getResult()) {
438 successRansac =
true;
440 if (worker.getBestConsensus().size() > best_consensus_size) {
441 nbInliers = worker.getNbInliers();
442 best_consensus = worker.getBestConsensus();
443 best_consensus_size = worker.getBestConsensus().size();
448 foundSolution = successRansac;
452 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 453 std::atomic<bool> abort{
false};
455 RansacFunctor sequentialRansac(cMo, ransacNbInlierConsensus, ransacMaxTrials, ransacThreshold, 0,
456 checkDegeneratePoints, listOfUniquePoints, func
457 #
if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
462 foundSolution = sequentialRansac.getResult();
465 nbInliers = sequentialRansac.getNbInliers();
466 best_consensus = sequentialRansac.getBestConsensus();
471 unsigned int nbMinRandom = 4;
495 if (nbInliers >= nbMinRandom)
500 for (
size_t i = 0; i < best_consensus.size(); i++) {
501 vpPoint pt = listOfUniquePoints[best_consensus[i]];
504 ransacInliers.push_back(pt);
508 for (std::vector<unsigned int>::const_iterator it_index = best_consensus.begin();
509 it_index != best_consensus.end(); ++it_index) {
510 ransacInlierIndex.push_back((
unsigned int)mapOfUniquePointIndex[*it_index]);
514 bool is_valid_lagrange =
false;
515 bool is_valid_dementhon =
false;
518 double r_lagrange = DBL_MAX;
519 double r_dementhon = DBL_MAX;
524 is_valid_lagrange =
true;
530 is_valid_dementhon =
true;
535 is_valid_lagrange =
false;
536 r_lagrange = DBL_MAX;
540 is_valid_dementhon =
false;
541 r_dementhon = DBL_MAX;
544 if (is_valid_lagrange || is_valid_dementhon) {
545 if (r_lagrange < r_dementhon) {
557 if (func != NULL && !func(cMo)) {
561 if (computeCovariance) {
562 covarianceMatrix = pose.covarianceMatrix;
570 return foundSolution;
594 probability = (std::max)(probability, 0.0);
595 probability = (std::min)(probability, 1.0);
596 epsilon = (std::max)(epsilon, 0.0);
597 epsilon = (std::min)(epsilon, 1.0);
604 if (maxIterations <= 0) {
605 maxIterations = std::numeric_limits<int>::max();
608 double logarg, logval, N;
609 logarg = -std::pow(1.0 - epsilon, sampleSize);
610 #ifdef VISP_HAVE_FUNC_LOG1P 611 logval = log1p(logarg);
613 logval = log(1.0 + logarg);
615 if (
vpMath::nul(logval, std::numeric_limits<double>::epsilon())) {
616 std::cerr <<
"vpMath::nul(log(1.0 - std::pow(1.0 - epsilon, " 617 "sampleSize)), std::numeric_limits<double>::epsilon())" 622 N = log((std::max)(1.0 - probability, std::numeric_limits<double>::epsilon())) / logval;
623 if (logval < 0.0 && N < maxIterations) {
627 return maxIterations;
661 const unsigned int &numberOfInlierToReachAConsensus,
const double &threshold,
663 const int &maxNbTrials,
664 bool useParallelRansac,
unsigned int nthreads,
670 for (
unsigned int i = 0; i < p2D.size(); i++) {
671 for (
unsigned int j = 0; j < p3D.size(); j++) {
672 vpPoint pt(p3D[j].getWorldCoordinates());
673 pt.
set_x(p2D[i].get_x());
674 pt.
set_y(p2D[i].get_y());
680 if (pose.
listP.size() < 4) {
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
void setWorldCoordinates(double oX, double oY, double oZ)
static void findMatch(std::vector< vpPoint > &p2D, std::vector< vpPoint > &p3D, const unsigned int &numberOfInlierToReachAConsensus, const double &threshold, unsigned int &ninliers, std::vector< vpPoint > &listInliers, vpHomogeneousMatrix &cMo, const int &maxNbTrials=10000, bool useParallelRansac=true, unsigned int nthreads=0, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setRansacThreshold(const double &t)
void track(const vpHomogeneousMatrix &cMo)
something is not initialized
std::list< vpPoint > listP
Array of point (use here class vpPoint)
std::vector< vpPoint > getRansacInliers() const
Class that defines what is a point.
void set_x(double x)
Set the point x coordinate in the image plane.
void set_y(double y)
Set the point y coordinate in the image plane.
void setNbParallelRansacThreads(int nb)
static bool nul(double x, double s=0.001)
static double sqr(double x)
void setUseParallelRansac(bool use)
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
static bool isNaN(double value)
unsigned int npt
Number of point used in pose computation.
void setRansacMaxTrials(const int &rM)
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Error that can be emited by the vpPose class and its derivates.
unsigned int getRansacNbInliers() const
double get_x() const
Get the point x coordinate in the image plane.
void setRansacFilterFlag(const RANSAC_FILTER_FLAGS &flag)
double get_y() const
Get the point y coordinate in the image plane.
bool poseRansac(vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo...
void addPoint(const vpPoint &P)
static int computeRansacIterations(double probability, double epsilon, const int sampleSize=4, int maxIterations=2000)
void setCovarianceComputation(const bool &flag)