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 73 if (dist1 - dist2 < -3*eps*eps)
75 if (dist1 - dist2 > 3*eps*eps)
78 if (point1.
oP[0] - point2.
oP[0] < -eps)
80 if (point1.
oP[0] - point2.
oP[0] > eps)
83 if (point1.
oP[1] - point2.
oP[1] < -eps)
85 if (point1.
oP[1] - point2.
oP[1] > eps)
88 if (point1.
oP[2] - point2.
oP[2] < -eps)
90 if (point1.
oP[2] - point2.
oP[2] > eps)
98 struct CompareImagePointDegenerate {
99 bool operator()(
const vpPoint &point1,
const vpPoint &point2)
const 103 if (dist1 - dist2 < -2*eps*eps)
105 if (dist1 - dist2 > 2*eps*eps)
108 if (point1.
p[0] - point2.
p[0] < -eps)
110 if (point1.
p[0] - point2.
p[0] > eps)
113 if (point1.
p[1] - point2.
p[1] < -eps)
115 if (point1.
p[1] - point2.
p[1] > eps)
123 struct FindDegeneratePoint {
124 explicit FindDegeneratePoint(
const vpPoint &pt) : m_pt(pt) {}
126 bool operator()(
const vpPoint &pt)
128 return ((std::fabs(m_pt.oP[0] - pt.
oP[0]) < eps && std::fabs(m_pt.oP[1] - pt.
oP[1]) < eps &&
129 std::fabs(m_pt.oP[2] - pt.
oP[2]) < eps) ||
130 (std::fabs(m_pt.p[0] - pt.
p[0]) < eps && std::fabs(m_pt.p[1] - pt.
p[1]) < eps));
137 bool vpPose::RansacFunctor::poseRansacImpl()
139 const unsigned int size = (
unsigned int)m_listOfUniquePoints.size();
140 unsigned int nbMinRandom = 4;
145 bool foundSolution =
false;
146 while (nbTrials < m_ransacMaxTrials && m_nbInliers < m_ransacNbInlierConsensus) {
148 std::vector<unsigned int> cur_consensus;
150 std::vector<unsigned int> cur_outliers;
152 std::vector<unsigned int> cur_randoms;
155 std::vector<vpPoint> cur_inliers;
166 std::vector<bool> usedPt(size,
false);
169 for (
unsigned int i = 0; i < nbMinRandom;) {
170 if ((
size_t)std::count(usedPt.begin(), usedPt.end(),
true) == usedPt.size()) {
176 unsigned int r_ = m_uniRand.uniform(0, size);
180 r_ = m_uniRand.uniform(0, size);
184 vpPoint pt = m_listOfUniquePoints[r_];
186 bool degenerate =
false;
187 if (m_checkDegeneratePoints) {
188 if (std::find_if(poseMin.listOfPoints.begin(), poseMin.listOfPoints.end(), FindDegeneratePoint(pt)) !=
189 poseMin.listOfPoints.end()) {
196 cur_randoms.push_back(r_);
202 if (poseMin.
npt < nbMinRandom) {
208 bool is_valid_lagrange =
false;
209 bool is_valid_dementhon =
false;
212 double r_lagrange = DBL_MAX;
213 double r_dementhon = DBL_MAX;
218 is_valid_lagrange =
true;
224 is_valid_dementhon =
true;
229 is_valid_lagrange =
false;
230 r_lagrange = DBL_MAX;
234 is_valid_dementhon =
false;
235 r_dementhon = DBL_MAX;
240 if (is_valid_lagrange || is_valid_dementhon) {
242 if (r_lagrange < r_dementhon) {
244 cMo_tmp = cMo_lagrange;
247 cMo_tmp = cMo_dementhon;
249 r = sqrt(r) / (double)nbMinRandom;
252 bool isPoseValid =
true;
253 if (m_func != NULL) {
254 isPoseValid = m_func(cMo_tmp);
263 if (isPoseValid && r < m_ransacThreshold) {
264 unsigned int nbInliersCur = 0;
265 unsigned int iter = 0;
266 for (std::vector<vpPoint>::const_iterator it = m_listOfUniquePoints.begin(); it != m_listOfUniquePoints.end();
272 if (error < m_ransacThreshold) {
273 bool degenerate =
false;
274 if (m_checkDegeneratePoints) {
275 if (std::find_if(cur_inliers.begin(), cur_inliers.end(), FindDegeneratePoint(*it)) != cur_inliers.end()) {
284 cur_consensus.push_back(iter);
285 cur_inliers.push_back(*it);
287 cur_outliers.push_back(iter);
290 cur_outliers.push_back(iter);
294 if (nbInliersCur > m_nbInliers) {
295 foundSolution =
true;
296 m_best_consensus = cur_consensus;
297 m_nbInliers = nbInliersCur;
302 if (nbTrials >= m_ransacMaxTrials) {
303 foundSolution =
true;
313 return foundSolution;
333 if (listP.size() != listOfPoints.size()) {
334 std::cerr <<
"You should not modify vpPose::listP!" << std::endl;
335 listOfPoints = std::vector<vpPoint>(listP.begin(), listP.end());
338 ransacInliers.clear();
339 ransacInlierIndex.clear();
341 std::vector<unsigned int> best_consensus;
342 unsigned int nbInliers = 0;
346 if (listOfPoints.size() < 4) {
350 std::vector<vpPoint> listOfUniquePoints;
351 std::map<size_t, size_t> mapOfUniquePointIndex;
354 bool prefilterDegeneratePoints = ransacFlag == PREFILTER_DEGENERATE_POINTS;
355 bool checkDegeneratePoints = ransacFlag == CHECK_DEGENERATE_POINTS;
357 if (prefilterDegeneratePoints) {
359 std::map<vpPoint, size_t, CompareObjectPointDegenerate> filterObjectPointMap;
361 for (std::vector<vpPoint>::const_iterator it_pt = listOfPoints.begin(); it_pt != listOfPoints.end();
362 ++it_pt, index_pt++) {
363 if (filterObjectPointMap.find(*it_pt) == filterObjectPointMap.end()) {
364 filterObjectPointMap[*it_pt] = index_pt;
368 std::map<vpPoint, size_t, CompareImagePointDegenerate> filterImagePointMap;
369 for (std::map<vpPoint, size_t, CompareObjectPointDegenerate>::const_iterator it = filterObjectPointMap.begin();
370 it != filterObjectPointMap.end(); ++it) {
371 if (filterImagePointMap.find(it->first) == filterImagePointMap.end()) {
372 filterImagePointMap[it->first] = it->second;
374 listOfUniquePoints.push_back(it->first);
375 mapOfUniquePointIndex[listOfUniquePoints.size() - 1] = it->second;
380 listOfUniquePoints = listOfPoints;
383 for (std::vector<vpPoint>::const_iterator it_pt = listOfPoints.begin(); it_pt != listOfPoints.end();
384 ++it_pt, index_pt++) {
385 mapOfUniquePointIndex[index_pt] = index_pt;
389 if (listOfUniquePoints.size() < 4) {
393 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 394 unsigned int nbThreads = 1;
395 bool executeParallelVersion = useParallelRansac;
397 bool executeParallelVersion =
false;
400 if (executeParallelVersion) {
401 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 402 if (nbParallelRansacThreads <= 0) {
404 nbThreads = std::thread::hardware_concurrency();
405 if (nbThreads <= 1) {
407 executeParallelVersion =
false;
410 nbThreads = nbParallelRansacThreads;
415 bool foundSolution =
false;
417 if (executeParallelVersion) {
418 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 419 std::vector<std::thread> threadpool;
420 std::vector<RansacFunctor> ransacWorkers;
422 int splitTrials = ransacMaxTrials / nbThreads;
423 for (
size_t i = 0; i < (size_t)nbThreads; i++) {
424 unsigned int initial_seed = (
unsigned int)i;
425 if (i < (
size_t)nbThreads - 1) {
426 ransacWorkers.emplace_back(cMo, ransacNbInlierConsensus, splitTrials, ransacThreshold, initial_seed,
427 checkDegeneratePoints, listOfUniquePoints, func);
429 int maxTrialsRemainder = ransacMaxTrials - splitTrials * (nbThreads - 1);
430 ransacWorkers.emplace_back(cMo, ransacNbInlierConsensus, maxTrialsRemainder, ransacThreshold, initial_seed,
431 checkDegeneratePoints, listOfUniquePoints, func);
435 for (
auto& worker : ransacWorkers) {
436 threadpool.emplace_back(&RansacFunctor::operator(), &worker);
439 for (
auto& th : threadpool) {
443 bool successRansac =
false;
444 size_t best_consensus_size = 0;
445 for (
auto &worker : ransacWorkers) {
446 if (worker.getResult()) {
447 successRansac =
true;
449 if (worker.getBestConsensus().size() > best_consensus_size) {
450 nbInliers = worker.getNbInliers();
451 best_consensus = worker.getBestConsensus();
452 best_consensus_size = worker.getBestConsensus().size();
457 foundSolution = successRansac;
461 RansacFunctor sequentialRansac(cMo, ransacNbInlierConsensus, ransacMaxTrials, ransacThreshold, 0,
462 checkDegeneratePoints, listOfUniquePoints, func);
464 foundSolution = sequentialRansac.getResult();
467 nbInliers = sequentialRansac.getNbInliers();
468 best_consensus = sequentialRansac.getBestConsensus();
473 unsigned int nbMinRandom = 4;
497 if (nbInliers >= nbMinRandom)
502 for (
size_t i = 0; i < best_consensus.size(); i++) {
503 vpPoint pt = listOfUniquePoints[best_consensus[i]];
506 ransacInliers.push_back(pt);
510 for (std::vector<unsigned int>::const_iterator it_index = best_consensus.begin();
511 it_index != best_consensus.end(); ++it_index) {
512 ransacInlierIndex.push_back((
unsigned int)mapOfUniquePointIndex[*it_index]);
516 bool is_valid_lagrange =
false;
517 bool is_valid_dementhon =
false;
520 double r_lagrange = DBL_MAX;
521 double r_dementhon = DBL_MAX;
526 is_valid_lagrange =
true;
532 is_valid_dementhon =
true;
537 is_valid_lagrange =
false;
538 r_lagrange = DBL_MAX;
542 is_valid_dementhon =
false;
543 r_dementhon = DBL_MAX;
546 if (is_valid_lagrange || is_valid_dementhon) {
547 if (r_lagrange < r_dementhon) {
559 if (func != NULL && !func(cMo)) {
563 if (computeCovariance) {
564 covarianceMatrix = pose.covarianceMatrix;
572 return foundSolution;
596 probability = (std::max)(probability, 0.0);
597 probability = (std::min)(probability, 1.0);
598 epsilon = (std::max)(epsilon, 0.0);
599 epsilon = (std::min)(epsilon, 1.0);
606 if (maxIterations <= 0) {
607 maxIterations = std::numeric_limits<int>::max();
610 double logarg, logval, N;
611 logarg = -std::pow(1.0 - epsilon, sampleSize);
612 #ifdef VISP_HAVE_FUNC_LOG1P 613 logval = log1p(logarg);
615 logval = log(1.0 + logarg);
617 if (
vpMath::nul(logval, std::numeric_limits<double>::epsilon())) {
618 std::cerr <<
"vpMath::nul(log(1.0 - std::pow(1.0 - epsilon, " 619 "sampleSize)), std::numeric_limits<double>::epsilon())" 624 N = log((std::max)(1.0 - probability, std::numeric_limits<double>::epsilon())) / logval;
625 if (logval < 0.0 && N < maxIterations) {
629 return maxIterations;
663 const unsigned int &numberOfInlierToReachAConsensus,
const double &threshold,
665 const int &maxNbTrials,
666 bool useParallelRansac,
unsigned int nthreads,
672 for (
unsigned int i = 0; i < p2D.size(); i++) {
673 for (
unsigned int j = 0; j < p3D.size(); j++) {
674 vpPoint pt(p3D[j].getWorldCoordinates());
675 pt.
set_x(p2D[i].get_x());
676 pt.
set_y(p2D[i].get_y());
682 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.
double get_oY() const
Get the point oY coordinate in the object frame.
void setRansacThreshold(const double &t)
void track(const vpHomogeneousMatrix &cMo)
something is not initialized
double get_y() const
Get the point y coordinate in the image plane.
std::list< vpPoint > listP
Array of point (use here class vpPoint)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
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)
unsigned int getRansacNbInliers() const
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...
double get_oZ() const
Get the point oZ coordinate in the object frame.
double get_x() const
Get the point x coordinate in the image plane.
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)
double get_oX() const
Get the point oX coordinate in the object frame.
Error that can be emited by the vpPose class and its derivates.
void setRansacFilterFlag(const RANSAC_FILTER_FLAGS &flag)
bool poseRansac(vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
void addPoint(const vpPoint &P)
static int computeRansacIterations(double probability, double epsilon, const int sampleSize=4, int maxIterations=2000)
std::vector< vpPoint > getRansacInliers() const
void setCovarianceComputation(const bool &flag)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo...