Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMbEdgeTracker.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Make the complete tracking of an object by using its CAD model
32  *
33  * Authors:
34  * Nicolas Melchior
35  * Romain Tallonneau
36  * Eric Marchand
37  *
38  *****************************************************************************/
39 
45 #ifndef vpMbEdgeTracker_HH
46 #define vpMbEdgeTracker_HH
47 
48 #include <visp3/core/vpPoint.h>
49 #include <visp3/mbt/vpMbTracker.h>
50 #include <visp3/me/vpMe.h>
51 #include <visp3/mbt/vpMbtMeLine.h>
52 #include <visp3/mbt/vpMbtDistanceLine.h>
53 #include <visp3/mbt/vpMbtDistanceCircle.h>
54 #include <visp3/mbt/vpMbtDistanceCylinder.h>
55 #include <visp3/core/vpXmlParser.h>
56 #include <visp3/core/vpRobust.h>
57 
58 #include <iostream>
59 #include <fstream>
60 #include <vector>
61 #include <list>
62 
63 #if defined(VISP_HAVE_COIN3D)
64 //Inventor includes
65 #include <Inventor/nodes/SoSeparator.h>
66 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
67 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
68 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
69 #include <Inventor/actions/SoWriteAction.h>
70 #include <Inventor/actions/SoSearchAction.h>
71 #include <Inventor/misc/SoChildList.h>
72 #include <Inventor/actions/SoGetMatrixAction.h>
73 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
74 #include <Inventor/actions/SoToVRML2Action.h>
75 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
76 #include <Inventor/VRMLnodes/SoVRMLShape.h>
77 #endif
78 
79 #ifdef VISP_HAVE_OPENCV
80 # if VISP_HAVE_OPENCV_VERSION >= 0x020101
81 # include <opencv2/core/core.hpp>
82 # include <opencv2/imgproc/imgproc.hpp>
83 # include <opencv2/imgproc/imgproc_c.h>
84 # else
85 # include <cv.h>
86 # endif
87 #endif
88 
261 class VISP_EXPORT vpMbEdgeTracker: virtual public vpMbTracker
262 {
263  friend class vpMbEdgeMultiTracker;
265 
266  protected :
267 
274  double lambda;
275 
279  std::vector< std::list< vpMbtDistanceLine*> > lines;
280 
282  std::vector< std::list< vpMbtDistanceCircle*> > circles;
283 
285  std::vector< std::list< vpMbtDistanceCylinder*> > cylinders;
286 
288  unsigned int nline;
289 
291  unsigned int ncircle;
292 
294  unsigned int ncylinder;
295 
297  unsigned int nbvisiblepolygone;
298 
301 
303  std::vector<bool> scales;
304 
306  std::vector< const vpImage<unsigned char>* > Ipyramid;
307 
309  unsigned int scaleLevel;
310 
313 
314 public:
315 
316  vpMbEdgeTracker();
317  virtual ~vpMbEdgeTracker();
318 
321 
323  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
325  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
326 
332  virtual inline double getLambda() const {return lambda;}
333 
334  void getLline(std::list<vpMbtDistanceLine *>& linesList, const unsigned int level = 0) const;
335  void getLcircle(std::list<vpMbtDistanceCircle *>& circlesList, const unsigned int level = 0) const;
336  void getLcylinder(std::list<vpMbtDistanceCylinder *>& cylindersList, const unsigned int level = 0) const;
337 
343  virtual inline void getMovingEdge(vpMe &p_me ) const { p_me = this->me;}
349  virtual inline vpMe getMovingEdge() const { return this->me;}
350 
351  virtual unsigned int getNbPoints(const unsigned int level=0) const;
352 
358  std::vector<bool> getScales() const {return scales;}
367  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt;}
368 
369  void loadConfigFile(const std::string &configFile);
370  void loadConfigFile(const char* configFile);
371  virtual void reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name, const vpHomogeneousMatrix& cMo_,
372  const bool verbose=false);
373  void reInitModel(const vpImage<unsigned char>& I, const char* cad_name, const vpHomogeneousMatrix& cMo,
374  const bool verbose=false);
375  void resetTracker();
376 
382  virtual void setCameraParameters(const vpCameraParameters& camera) {
383  this->cam = camera;
384 
385  for (unsigned int i = 0; i < scales.size(); i += 1){
386  if(scales[i]){
387  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[i].begin(); it!=lines[i].end(); ++it){
388  (*it)->setCameraParameters(cam);
389  }
390 
391  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[i].begin(); it!=cylinders[i].end(); ++it){
392  (*it)->setCameraParameters(cam);
393  }
394 
395  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[i].begin(); it!=circles[i].end(); ++it){
396  (*it)->setCameraParameters(cam);
397  }
398  }
399  }
400  }
401 
402  virtual void setClipping(const unsigned int &flags);
403 
404  virtual void setFarClippingDistance(const double &dist);
405 
406  virtual void setNearClippingDistance(const double &dist);
407 
415  virtual void setOgreVisibilityTest(const bool &v){
417 #ifdef VISP_HAVE_OGRE
418  faces.getOgreContext()->setWindowName("MBT Edge");
419 #endif
420  }
421 
427  virtual void setScanLineVisibilityTest(const bool &v){
429 
430  for (unsigned int i = 0; i < scales.size(); i += 1){
431  if(scales[i]){
432  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[i].begin(); it!=lines[i].end(); ++it){
433  (*it)->useScanLine = v;
434  }
435  }
436  }
437  }
438 
451  void setGoodMovingEdgesRatioThreshold(const double threshold) {percentageGdPt = threshold;}
452 
458  virtual inline void setLambda(const double gain) {this->lambda = gain;}
459 
460  void setMovingEdge(const vpMe &me);
461 
462  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo);
463 
464  void setScales(const std::vector<bool>& _scales);
465 
466  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
467 
468  void track(const vpImage<unsigned char> &I);
470 
471 protected:
474  bool samePoint(const vpPoint &P1, const vpPoint &P2) const;
475  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, int idFace = -1, const std::string& name = "");
476  void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace = -1, const std::string& name = "");
477  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
478  void addPolygon(vpMbtPolygon &p) ;
479 
480  void cleanPyramid(std::vector<const vpImage<unsigned char>* >& _pyramid);
482 
483  void computeVVS(const vpImage<unsigned char>& _I, const unsigned int lvl);
484  void computeVVSFirstPhase(const vpImage<unsigned char>& I, const unsigned int iter,
485  vpMatrix &L, vpColVector &factor, double &count, vpColVector &error, vpColVector &w_mbt, const unsigned int lvl = 0);
486  void computeVVSFirstPhaseFactor(const vpImage<unsigned char>& I, vpColVector &factor, const unsigned int lvl = 0);
487  void computeVVSFirstPhasePoseEstimation(const unsigned int nerror, const unsigned int iter, const vpColVector &factor,
488  vpColVector &weighted_error, vpMatrix &L, bool &isoJoIdentity_);
489  void computeVVSSecondPhase(const vpImage<unsigned char>& I, vpMatrix &L, vpColVector &error_lines,
490  vpColVector &error_cylinders, vpColVector &error_circles, vpColVector &error, const unsigned int lvl);
491  void computeVVSSecondPhaseCheckLevenbergMarquardt(const unsigned int iter, const unsigned int nbrow,
492  const vpColVector &m_error_prev, const vpColVector &m_w_prev, const vpHomogeneousMatrix &cMoPrev,
493  double &mu, bool &reStartFromLastIncrement);
494  void computeVVSSecondPhasePoseEstimation(const unsigned int nerror, vpMatrix &L, vpMatrix &L_true, vpMatrix &LVJ_true,
495  vpColVector &W_true, const vpColVector &factor, const unsigned int iter, const bool isoJoIdentity_,
496  vpColVector &weighted_error, double &mu, vpColVector &m_error_prev, vpColVector &m_w_prev,
497  vpHomogeneousMatrix &cMoPrev, double &residu_1, double &r);
498  void computeVVSSecondPhaseWeights(const unsigned int iter, const unsigned int nerror,
499  const unsigned int nbrow, vpColVector &weighted_error,
500  vpRobust &robust_lines, vpRobust &robust_cylinders, vpRobust &robust_circles,
501  vpColVector &w_lines, vpColVector &w_cylinders, vpColVector &w_circles,
502  vpColVector &error_lines, vpColVector &error_cylinders, vpColVector &error_circles,
503  const unsigned int nberrors_lines, const unsigned int nberrors_cylinders, const unsigned int nberrors_circles);
504 
505  void displayFeaturesOnImage(const vpImage<unsigned char>& I, const unsigned int lvl);
506  void downScale(const unsigned int _scale);
507  void init(const vpImage<unsigned char>& I);
508  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
509  const int idFace=0, const std::string &name="");
510  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
511  const std::string &name="");
512  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
513  virtual void initFaceFromLines(vpMbtPolygon &polygon);
514  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders, unsigned int &nberrors_circles);
515  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo) ;
516  void initPyramid(const vpImage<unsigned char>& _I, std::vector<const vpImage<unsigned char>* >& _pyramid);
517  void reInitLevel(const unsigned int _lvl);
518  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
519  void removeCircle(const std::string& name);
520  void removeCylinder(const std::string& name);
521  void removeLine(const std::string& name);
522  void resetMovingEdge();
523  void testTracking();
524  void trackMovingEdge(const vpImage<unsigned char> &I) ;
527  void upScale(const unsigned int _scale);
528  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline) ;
530 };
531 
532 #endif
533 
std::vector< bool > getScales() const
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:266
unsigned int ncylinder
Index of the cylinder to add, and total number of cylinders extracted so far.
void computeVVSFirstPhasePoseEstimation(const unsigned int nerror, const unsigned int iter, const vpColVector &factor, vpColVector &weighted_error, vpMatrix &L, bool &isoJoIdentity_)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
virtual void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking)
double lambda
The gain of the virtual visual servoing stage.
virtual void track(const vpImage< unsigned char > &I)=0
virtual void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name="")
void upScale(const unsigned int _scale)
void addLine(vpPoint &p1, vpPoint &p2, int polygon=-1, std::string name="")
virtual void computeVVS(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, std::map< std::string, unsigned int > &mapOfNumberOfRows, std::map< std::string, unsigned int > &mapOfNbInfos, vpColVector &w_mbt, vpColVector &w_klt, const unsigned int lvl=0)
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:144
virtual void computeProjectionError()
Implementation of an homogeneous matrix and operations on such kind of matrices.
double lambda
The gain of the virtual visual servoing stage.
unsigned int scaleLevel
Current scale level used. This attribute must not be modified outside of the downScale() and upScale(...
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:115
virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=0, const std::string &name="")=0
virtual ~vpMbEdgeTracker()
unsigned int ncircle
Index of the circle to add, and total number of circles extracted so far.
virtual void cleanPyramid(std::map< std::string, std::vector< const vpImage< unsigned char > * > > &pyramid)
void reInitLevel(const unsigned int _lvl)
Hybrid stereo (or more) tracker based on moving-edges and keypoints tracked using KLT tracker...
Definition: vpMe.h:59
virtual void getMovingEdge(vpMe &p_me) const
Make the complete tracking of an object by using its CAD model.
bool samePoint(const vpPoint &P1, const vpPoint &P2) const
std::vector< const vpImage< unsigned char > * > Ipyramid
Pyramid of image associated to the current image. This pyramid is computed in the init() and in the t...
void removeCircle(const std::string &name)
virtual void setCameraParameters(const vpCameraParameters &camera)
void displayFeaturesOnImage(const vpImage< unsigned char > &I, const unsigned int lvl)
void removeCylinder(const std::string &name)
vpMe me
The moving edges parameters.
void updateMovingEdge(const vpImage< unsigned char > &I)
virtual void resetTracker()=0
std::vector< std::list< vpMbtDistanceLine * > > lines
Vector of list of all the lines tracked (each line is linked to a list of moving edges). Each element of the vector is for a scale (element 0 = level 0 = no subsampling).
void setGoodMovingEdgesRatioThreshold(const double threshold)
void downScale(const unsigned int _scale)
void computeVVSSecondPhasePoseEstimation(const unsigned int nerror, vpMatrix &L, vpMatrix &L_true, vpMatrix &LVJ_true, vpColVector &W_true, const vpColVector &factor, const unsigned int iter, const bool isoJoIdentity_, vpColVector &weighted_error, double &mu, vpColVector &m_error_prev, vpColVector &m_w_prev, vpHomogeneousMatrix &cMoPrev, double &residu_1, double &r)
Class that defines what is a point.
Definition: vpPoint.h:59
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
virtual void init(const vpImage< unsigned char > &I)=0
virtual void setOgreVisibilityTest(const bool &v)
vpAROgre * getOgreContext()
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)=0
std::vector< bool > scales
Vector of scale level to use for the multi-scale tracking.
virtual void setScanLineVisibilityTest(const bool &v)
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:64
void initMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
void computeVVSSecondPhaseCheckLevenbergMarquardt(const unsigned int iter, const unsigned int nbrow, const vpColVector &m_error_prev, const vpColVector &m_w_prev, const vpHomogeneousMatrix &cMoPrev, double &mu, bool &reStartFromLastIncrement)
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
unsigned int nbFeaturesForProjErrorComputation
Number of features used in the computation of the projection error.
void getLcircle(std::list< vpMbtDistanceCircle * > &circlesList, const unsigned int level=0) const
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
double percentageGdPt
Percentage of good points over total number of points below which tracking is supposed to have failed...
void getLline(std::list< vpMbtDistanceLine * > &linesList, const unsigned int level=0) const
virtual unsigned int initMbtTracking(std::vector< FeatureType > &indexOfFeatures, std::map< std::string, unsigned int > &mapOfNumberOfRows, std::map< std::string, unsigned int > &mapOfNumberOfLines, std::map< std::string, unsigned int > &mapOfNumberOfCylinders, std::map< std::string, unsigned int > &mapOfNumberOfCircles)
virtual void loadConfigFile(const std::string &configFile)=0
void computeVVSFirstPhase(const vpImage< unsigned char > &I, const unsigned int iter, vpMatrix &L, vpColVector &factor, double &count, vpColVector &error, vpColVector &w_mbt, const unsigned int lvl=0)
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
virtual vpMe getMovingEdge() const
void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace=-1, const std::string &name="")
virtual void setMovingEdge(const vpMe &me)
virtual void setScales(const std::vector< bool > &scales)
virtual double getLambda() const
virtual void setOgreVisibilityTest(const bool &v)
double getGoodMovingEdgesRatioThreshold() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
std::vector< std::list< vpMbtDistanceCylinder * > > cylinders
Vector of the tracked cylinders.
virtual void initFaceFromLines(vpMbtPolygon &polygon)=0
virtual void testTracking()=0
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:60
unsigned int nbvisiblepolygone
Number of polygon (face) currently visible.
void getLcylinder(std::list< vpMbtDistanceCylinder * > &cylindersList, const unsigned int level=0) const
void visibleFace(const vpImage< unsigned char > &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline)
virtual unsigned int getNbPoints(const unsigned int level=0) const
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)=0
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:439
virtual void setClipping(const unsigned int &flags)
void addPolygon(const std::vector< vpPoint > &corners, const int idFace=-1, const std::string &polygonName="", const bool useLod=false, const double minPolygonAreaThreshold=2500.0, const double minLineLengthThreshold=50.0)
unsigned int nline
Index of the polygon to add, and total number of polygon extracted so far.
void trackMovingEdge(const vpImage< unsigned char > &I)
void computeVVSFirstPhaseFactor(const vpImage< unsigned char > &I, vpColVector &factor, const unsigned int lvl=0)
virtual void setFarClippingDistance(const double &dist)
void removeLine(const std::string &name)
Make the complete stereo (or more) tracking of an object by using its CAD model.
void computeVVSSecondPhase(const vpImage< unsigned char > &I, vpMatrix &L, vpColVector &error_lines, vpColVector &error_cylinders, vpColVector &error_circles, vpColVector &error, const unsigned int lvl)
virtual void computeVVSSecondPhaseWeights(const unsigned int iter, const unsigned int nerror, vpColVector &weighted_error, vpColVector &w_lines, vpColVector &w_cylinders, vpColVector &w_circles, std::map< std::string, unsigned int > &mapOfNumberOfLines, std::map< std::string, unsigned int > &mapOfNumberOfCylinders, std::map< std::string, unsigned int > &mapOfNumberOfCircles, std::map< std::string, vpColVector > &mapOfWeightLines, std::map< std::string, vpColVector > &mapOfWeightCylinders, std::map< std::string, vpColVector > &mapOfWeightCircles, std::map< std::string, vpColVector > &mapOfErrorLines, std::map< std::string, vpColVector > &mapOfErrorCylinders, std::map< std::string, vpColVector > &mapOfErrorCircles, std::map< std::string, vpRobust > &mapOfRobustLines, std::map< std::string, vpRobust > &mapOfRobustCylinders, std::map< std::string, vpRobust > &mapOfRobustCircles, double threshold)
virtual void initPyramid(const std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, std::map< std::string, std::vector< const vpImage< unsigned char > * > > &pyramid)
virtual void setLambda(const double gain)
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, const double radius, const int idFace=0, const std::string &name="")=0
virtual void setNearClippingDistance(const double &dist)