53 #include <visp3/vision/vpPose.h>
54 #include <visp3/core/vpColVector.h>
55 #include <visp3/core/vpRansac.h>
56 #include <visp3/core/vpTime.h>
57 #include <visp3/core/vpList.h>
58 #include <visp3/vision/vpPoseException.h>
59 #include <visp3/core/vpMath.h>
74 ransacInliers.clear();
75 ransacInlierIndex.clear();
78 std::vector<unsigned int> best_consensus;
79 std::vector<unsigned int> cur_consensus;
80 std::vector<unsigned int> cur_outliers;
81 std::vector<unsigned int> cur_randoms;
82 unsigned int size = (
unsigned int)
listP.size();
84 unsigned int nbMinRandom = 4 ;
85 unsigned int nbInliers = 0;
86 double r, r_lagrange, r_dementhon;
93 "Not enough point to compute the pose")) ;
96 bool foundSolution =
false;
98 while (nbTrials < ransacMaxTrials && nbInliers < (
unsigned)ransacNbInlierConsensus)
101 cur_consensus.clear();
107 cur_outliers.clear();
111 std::vector<bool> usedPt(size,
false);
114 for(
unsigned int i = 0; i < nbMinRandom;)
116 if((
size_t) std::count(usedPt.begin(), usedPt.end(),
true) == usedPt.size()) {
122 unsigned int r_ = (
unsigned int) rand() % size;
125 r_ = (
unsigned int) rand() % size;
130 std::list<vpPoint>::const_iterator iter =
listP.begin();
131 std::advance(iter, r_);
134 bool degenerate =
false;
135 for(std::list<vpPoint>::const_iterator it = poseMin.
listP.begin(); it != poseMin.
listP.end(); ++it){
145 cur_randoms.push_back(r_);
151 if(poseMin.
npt < nbMinRandom) {
157 bool is_valid_lagrange =
false;
158 bool is_valid_dementhon =
false;
161 r_lagrange = DBL_MAX;
162 r_dementhon = DBL_MAX;
167 is_valid_lagrange =
true;
175 is_valid_dementhon =
true;
182 is_valid_lagrange =
false;
183 r_lagrange = DBL_MAX;
187 is_valid_dementhon =
false;
188 r_dementhon = DBL_MAX;
193 if(is_valid_lagrange || is_valid_dementhon) {
194 if (r_lagrange < r_dementhon) {
197 cMo_tmp = cMo_lagrange;
202 cMo_tmp = cMo_dementhon;
204 r = sqrt(r) / (double) nbMinRandom;
207 bool isPoseValid =
true;
209 isPoseValid = func(&cMo_tmp);
218 if (isPoseValid && r < ransacThreshold)
220 unsigned int nbInliersCur = 0;
221 unsigned int iter = 0;
222 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it)
229 double error = sqrt(d) ;
230 if(error < ransacThreshold) {
233 bool degenerate =
false;
235 for(
unsigned int it_inlier_index = 0; it_inlier_index< cur_consensus.size(); it_inlier_index++){
236 std::list<vpPoint>::const_iterator it_point =
listP.begin();
237 std::advance(it_point, cur_consensus[it_inlier_index]);
250 cur_consensus.push_back(iter);
253 cur_outliers.push_back(iter);
257 cur_outliers.push_back(iter);
263 if(nbInliersCur > nbInliers)
265 foundSolution =
true;
266 best_consensus = cur_consensus;
267 nbInliers = nbInliersCur;
272 if(nbTrials >= ransacMaxTrials) {
274 foundSolution =
true;
280 if(nbTrials >= ransacMaxTrials) {
287 if(nbTrials >= ransacMaxTrials) {
315 if(nbInliers >= nbMinRandom)
319 for(
unsigned i = 0 ; i < best_consensus.size(); i++)
321 std::list<vpPoint>::const_iterator iter =
listP.begin();
322 std::advance(iter, best_consensus[i]);
326 ransacInliers.push_back(pt);
330 ransacInlierIndex = best_consensus;
333 bool is_valid_lagrange =
false;
334 bool is_valid_dementhon =
false;
337 r_lagrange = DBL_MAX;
338 r_dementhon = DBL_MAX;
343 is_valid_lagrange =
true;
351 is_valid_dementhon =
true;
358 is_valid_lagrange =
false;
359 r_lagrange = DBL_MAX;
363 is_valid_dementhon =
false;
364 r_dementhon = DBL_MAX;
367 if(is_valid_lagrange || is_valid_dementhon) {
368 if (r_lagrange < r_dementhon) {
380 if(func != NULL && !func(&cMo)) {
384 if(computeCovariance) {
385 covarianceMatrix = pose.covarianceMatrix;
393 return foundSolution;
421 std::vector<vpPoint> &p3D,
422 const unsigned int &numberOfInlierToReachAConsensus,
423 const double &threshold,
424 unsigned int &ninliers,
425 std::vector<vpPoint> &listInliers,
427 const int &maxNbTrials )
432 for(
unsigned int i = 0 ; i < p2D.size() ; i++)
434 for(
unsigned int j = 0 ; j < p3D.size() ; j++)
436 vpPoint pt(p3D[j].getWorldCoordinates());
437 pt.
set_x(p2D[i].get_x());
438 pt.
set_y(p2D[i].get_y());
444 if (pose.
listP.size() < 4)
446 vpERROR_TRACE(
"Ransac method cannot be used in that case ") ;
450 "Not enough points ")) ;
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool isNaN(const double value)
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)
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 what is a point.
bool poseRansac(vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
compute the pose using the Ransac approach
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)
bool computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
compute the pose for a given method
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.
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 X coordinate in the object frame.
Error that can be emited by the vpPose class and its derivates.
void addPoint(const vpPoint &P)
Add a new point in this array.
std::vector< vpPoint > getRansacInliers() const
void setCovarianceComputation(const bool &flag)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.