49 #include <visp/vpPose.h>
50 #include <visp/vpColVector.h>
51 #include <visp/vpRansac.h>
52 #include <visp/vpTime.h>
53 #include <visp/vpList.h>
54 #include <visp/vpPoseException.h>
72 std::vector<unsigned int> best_consensus;
73 std::vector<unsigned int> cur_consensus;
74 std::vector<unsigned int> cur_outliers;
75 std::vector<unsigned int> cur_randoms;
76 unsigned int size = (
unsigned int)
listP.size();
78 unsigned int nbMinRandom = 4 ;
79 unsigned int nbInliers = 0;
80 double r, r_lagrange, r_dementhon;
87 "Not enough point to compute the pose")) ;
90 bool foundSolution =
false;
92 while (nbTrials < ransacMaxTrials && nbInliers < (
unsigned)ransacNbInlierConsensus)
97 std::vector<bool> usedPt(size,
false);
100 for(
unsigned int i = 0; i < nbMinRandom; i++)
102 unsigned int r_ = (
unsigned int)rand()%size;
103 while(usedPt[r_] ) r_ = (
unsigned int)rand()%size;
106 std::list<vpPoint>::const_iterator iter =
listP.begin();
107 std::advance(iter, r_);
110 bool degenerate =
false;
111 for(std::list<vpPoint>::const_iterator it = poseMin.
listP.begin(); it != poseMin.
listP.end(); ++it){
121 cur_randoms.push_back(r_);
132 if (r_lagrange < r_dementhon) {
140 r = sqrt(r)/(double)nbMinRandom;
142 if (r < ransacThreshold)
144 unsigned int nbInliersCur = 0;
145 unsigned int iter = 0;
146 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it)
153 double error = sqrt(d) ;
154 if(error < ransacThreshold){
157 bool degenerate =
false;
159 for(
unsigned int it_inlier_index = 0; it_inlier_index< cur_consensus.size(); it_inlier_index++){
160 std::list<vpPoint>::const_iterator it_point =
listP.begin();
161 std::advance(it_point, cur_consensus[it_inlier_index]);
174 cur_consensus.push_back(iter);
177 cur_outliers.push_back(iter);
181 cur_outliers.push_back(iter);
186 if(nbInliersCur > nbInliers)
188 foundSolution =
true;
189 best_consensus = cur_consensus;
190 nbInliers = nbInliersCur;
194 cur_consensus.clear();
196 if(nbTrials >= ransacMaxTrials){
197 vpERROR_TRACE(
"Ransac reached the maximum number of trials");
198 foundSolution =
true;
222 if(nbInliers >= (
unsigned)ransacNbInlierConsensus)
225 for(
unsigned i = 0 ; i < best_consensus.size(); i++)
227 std::list<vpPoint>::const_iterator iter =
listP.begin();
228 std::advance(iter, best_consensus[i]);
232 ransacInliers.push_back(pt);
240 if (r_lagrange < r_dementhon) {
277 std::vector<vpPoint> &p3D,
278 const unsigned int &numberOfInlierToReachAConsensus,
279 const double &threshold,
280 unsigned int &ninliers,
281 std::vector<vpPoint> &listInliers,
283 const int &maxNbTrials )
288 for(
unsigned int i = 0 ; i < p2D.size() ; i++)
290 for(
unsigned int j = 0 ; j < p3D.size() ; j++)
293 pt.
set_x(p2D[i].get_x());
294 pt.
set_y(p2D[i].get_y());
301 if (pose.
listP.size() < 4)
303 vpERROR_TRACE(
"Ransac method cannot be used in that case ") ;
307 "Not enough points ")) ;
320 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
338 for (
int i=1 ; i < 4 ; i++)
339 for (
int j=0 ; j<i ; j++)
341 unsigned int indi = 5*ind[i] ;
342 unsigned int indj = 5*ind[j] ;
344 if ((fabs(x[indi] - x[indj]) < 1e-6) && (fabs(x[indi+1] - x[indj+1]) < 1e-6))
347 if ((fabs(x[indi+2] - x[indj+2]) < 1e-6) && (fabs(x[indi+3] - x[indj+3]) < 1e-6) && (fabs(x[indi+4] - x[indj+4]) < 1e-6))
366 for(i=0 ; i < 4 ; i++)
369 unsigned int index = 5*ind[i] ;
371 p[i].
set_x(x[index]) ;
372 p[i].
set_y(x[index+1]) ;
391 for (i=0 ; i <16 ; i++)
409 unsigned int n = x.
getRows()/5 ;
415 for( i=0 ; i < n ; i++)
422 for (i=0 ; i <16 ; i++)
431 for( i=0 ; i < n ; i++)
444 vpPose::initRansac(
const unsigned int n,
445 const double *x,
const double *y,
446 const unsigned int m,
447 const double *X,
const double *Y,
const double *Z,
452 for (
unsigned int i=0 ; i < n ; i++)
454 for (
unsigned int j=0 ; j < m ; j++)
502 const double *x,
const double *y,
503 const unsigned int m,
504 const double *X,
const double *Y,
const double *Z,
505 const int numberOfInlierToReachAConsensus,
506 const double threshold,
507 unsigned int &ninliers,
511 const int maxNbTrials)
518 vpPose::initRansac(n,x,y,m,X,Y,Z, data) ;
523 threshold, M,inliers,
524 numberOfInlierToReachAConsensus, 0.0, maxNbTrials) ;
529 for(i=0 ; i < n*m ; i++)
532 if (std::fabs(inliers[i]-1) <= std::fabs(
vpMath::maximum(inliers[i], 1.)) * std::numeric_limits<double>::epsilon())
545 for(i=0 ; i < n*m ; i++)
548 if (std::fabs(inliers[i]-1) <= std::fabs(
vpMath::maximum(inliers[i], 1.)) * std::numeric_limits<double>::epsilon())
551 yi[k] = data[5*i+1] ;
552 Xi[k] = data[5*i+2] ;
553 Yi[k] = data[5*i+3] ;
554 Zi[k] = data[5*i+4] ;
559 for (i=0 ; i <16 ; i++)
596 const unsigned int m,
598 const int numberOfInlierToReachAConsensus,
599 const double threshold,
600 unsigned int &ninliers,
601 std::list<vpPoint> &lPi,
603 const int maxNbTrials)
608 for (
unsigned int i=0 ; i < n ; i++)
610 x[i] = p[i].
get_x() ;
611 y[i] = p[i].
get_y() ;
617 for (
unsigned int i=0 ; i < m ; i++)
627 m,X,Y,Z, numberOfInlierToReachAConsensus,
633 for(
unsigned int i=0 ; i < ninliers ; i++)
674 std::list<vpPoint> &lP,
675 const int numberOfInlierToReachAConsensus,
676 const double threshold,
677 unsigned int &ninliers,
678 std::list<vpPoint> &lPi,
680 const int maxNbTrials)
683 unsigned int n = (
unsigned int)lp.size() ;
684 unsigned int m = (
unsigned int)lP.size() ;
692 for (std::list<vpPoint>::const_iterator it = lp.begin(); it != lp.end(); ++it)
705 for (std::list<vpPoint>::const_iterator it = lP.begin(); it != lP.end(); ++it)
717 m,X,Y,Z, numberOfInlierToReachAConsensus,
723 for( i=0 ; i < ninliers ; i++)
739 #endif // VISP_BUILD_DEPRECATED_FUNCTIONS
void projection(const vpColVector &_cP, vpColVector &_p)
Projection onto the image plane of a point. Input: the 3D coordinates in the camera frame _cP...
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void setIdentity()
Basic initialisation (identity)
double get_oY() const
Get the point Y coordinate in the object frame.
void setRansacThreshold(const double &t)
void set_x(const double x)
Set the point x coordinate in the image plane.
void track(const vpHomogeneousMatrix &cMo)
static double measureTimeMs()
something is not initialized
double get_y() const
Get the point y coordinate in the image plane.
static void computeTransformation(vpColVector &x, unsigned int *ind, vpColVector &M)
std::list< vpPoint > listP
array of point (use here class vpPoint)
Class that defines what is a point.
static Type maximum(const Type &a, const Type &b)
double * data
address of the first element of the data array
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)
unsigned int getRansacNbInliers() const
static double sqr(double x)
Class used for pose computation from N points (pose from point only).
double get_oZ() const
Get the point Z coordinate in the object frame.
void set_y(const double y)
Set the point y coordinate in the image plane.
double get_x() const
Get the point x coordinate in the image plane.
void poseRansac(vpHomogeneousMatrix &cMo)
compute the pose using the Ransac approach
void setRansacMaxTrials(const int &rM)
static void ransac(const unsigned int n, const double *x, const double *y, const unsigned int m, const double *X, const double *Y, const double *Z, const int numberOfInlierToReachAConsensus, const double threshold, unsigned int &ninliers, vpColVector &xi, vpColVector &yi, vpColVector &Xi, vpColVector &Yi, vpColVector &Zi, vpHomogeneousMatrix &cMo, const int maxNbTrials=10000)
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
static bool degenerateConfiguration(vpColVector &x, unsigned int *ind)
double get_oX() const
Get the point X coordinate in the object frame.
Error that can be emited by the vpPose class and its derivates.
Class that provides a data structure for the column vectors as well as a set of operations on these v...
static bool ransac(unsigned int npts, vpColVector &x, unsigned int s, double t, vpColVector &model, vpColVector &inliers, int consensus=1000, double areaThreshold=0.0, const int maxNbumbersOfTrials=10000)
RANSAC - Robustly fits a model to data with the RANSAC algorithm.
void computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
unsigned int getRows() const
Return the number of rows of the matrix.
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
void addPoint(const vpPoint &P)
Add a new point in this array.
std::vector< vpPoint > getRansacInliers() const
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
void resize(const unsigned int i, const bool flagNullify=true)
void clearPoint()
suppress all the point in the array of point