Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpMbEdgeTracker.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Make the complete tracking of an object by using its CAD model
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Romain Tallonneau
37  * Eric Marchand
38  *
39  *****************************************************************************/
40 
46 #ifndef vpMbEdgeTracker_HH
47 #define vpMbEdgeTracker_HH
48 
49 #include <visp3/core/vpPoint.h>
50 #include <visp3/core/vpXmlParser.h>
51 #include <visp3/mbt/vpMbTracker.h>
52 #include <visp3/mbt/vpMbtDistanceCircle.h>
53 #include <visp3/mbt/vpMbtDistanceCylinder.h>
54 #include <visp3/mbt/vpMbtDistanceLine.h>
55 #include <visp3/mbt/vpMbtMeLine.h>
56 #include <visp3/me/vpMe.h>
57 
58 #include <fstream>
59 #include <iostream>
60 #include <list>
61 #include <vector>
62 
63 #if defined(VISP_HAVE_COIN3D)
64 // Inventor includes
65 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
66 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
67 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
68 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
69 #include <Inventor/VRMLnodes/SoVRMLShape.h>
70 #include <Inventor/actions/SoGetMatrixAction.h>
71 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
72 #include <Inventor/actions/SoSearchAction.h>
73 #include <Inventor/actions/SoToVRML2Action.h>
74 #include <Inventor/actions/SoWriteAction.h>
75 #include <Inventor/misc/SoChildList.h>
76 #include <Inventor/nodes/SoSeparator.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 
271 class VISP_EXPORT vpMbEdgeTracker : public virtual vpMbTracker
272 {
273  friend class vpMbEdgeMultiTracker;
275 
276 protected:
282  std::vector<std::list<vpMbtDistanceLine *> > lines;
283 
285  std::vector<std::list<vpMbtDistanceCircle *> > circles;
286 
288  std::vector<std::list<vpMbtDistanceCylinder *> > cylinders;
289 
292  unsigned int nline;
293 
296  unsigned int ncircle;
297 
300  unsigned int ncylinder;
301 
303  unsigned int nbvisiblepolygone;
304 
308 
310  std::vector<bool> scales;
311 
314  std::vector<const vpImage<unsigned char> *> Ipyramid;
315 
319  unsigned int scaleLevel;
320 
323 
345 
346 public:
347  vpMbEdgeTracker();
348  virtual ~vpMbEdgeTracker();
349 
352 
354  const vpColor &col, const unsigned int thickness = 1, const bool displayFullModel = false);
356  const vpColor &col, const unsigned int thickness = 1, const bool displayFullModel = false);
357 
358  void getLline(std::list<vpMbtDistanceLine *> &linesList, const unsigned int level = 0) const;
359  void getLcircle(std::list<vpMbtDistanceCircle *> &circlesList, const unsigned int level = 0) const;
360  void getLcylinder(std::list<vpMbtDistanceCylinder *> &cylindersList, const unsigned int level = 0) const;
361 
368  virtual inline void getMovingEdge(vpMe &p_me) const { p_me = this->me; }
374  virtual inline vpMe getMovingEdge() const { return this->me; }
375 
376  virtual unsigned int getNbPoints(const unsigned int level = 0) const;
377 
383  std::vector<bool> getScales() const { return scales; }
393  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt; }
394 
395  virtual inline vpColVector getError() const { return m_error_edge; }
396 
397  virtual inline vpColVector getRobustWeights() const { return m_w_edge; }
398 
399  void loadConfigFile(const std::string &configFile);
400 
401  virtual void reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name,
402  const vpHomogeneousMatrix &cMo_, const bool verbose = false,
404  void resetTracker();
405 
411  virtual void setCameraParameters(const vpCameraParameters &camera)
412  {
413  this->cam = camera;
414 
415  for (unsigned int i = 0; i < scales.size(); i += 1) {
416  if (scales[i]) {
417  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
418  (*it)->setCameraParameters(cam);
419  }
420 
421  for (std::list<vpMbtDistanceCylinder *>::const_iterator it = cylinders[i].begin(); it != cylinders[i].end();
422  ++it) {
423  (*it)->setCameraParameters(cam);
424  }
425 
426  for (std::list<vpMbtDistanceCircle *>::const_iterator it = circles[i].begin(); it != circles[i].end(); ++it) {
427  (*it)->setCameraParameters(cam);
428  }
429  }
430  }
431  }
432 
433  virtual void setClipping(const unsigned int &flags);
434 
435  virtual void setFarClippingDistance(const double &dist);
436 
437  virtual void setNearClippingDistance(const double &dist);
438 
447  virtual void setOgreVisibilityTest(const bool &v)
448  {
450 #ifdef VISP_HAVE_OGRE
451  faces.getOgreContext()->setWindowName("MBT Edge");
452 #endif
453  }
454 
460  virtual void setScanLineVisibilityTest(const bool &v)
461  {
463 
464  for (unsigned int i = 0; i < scales.size(); i += 1) {
465  if (scales[i]) {
466  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
467  (*it)->useScanLine = v;
468  }
469  }
470  }
471  }
472 
486  void setGoodMovingEdgesRatioThreshold(const double threshold) { percentageGdPt = threshold; }
487 
488  void setMovingEdge(const vpMe &me);
489 
490  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cdMo);
491 
492  void setScales(const std::vector<bool> &_scales);
493 
494  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
495 
496  void track(const vpImage<unsigned char> &I);
498 
499 protected:
502  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, int idFace = -1,
503  const std::string &name = "");
504  void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace = -1, const std::string &name = "");
505  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
506  void addPolygon(vpMbtPolygon &p);
507 
508  void cleanPyramid(std::vector<const vpImage<unsigned char> *> &_pyramid);
510 
511  void computeVVS(const vpImage<unsigned char> &_I, const unsigned int lvl);
512  void computeVVSFirstPhase(const vpImage<unsigned char> &I, const unsigned int iter, double &count,
513  const unsigned int lvl = 0);
514  void computeVVSFirstPhaseFactor(const vpImage<unsigned char> &I, const unsigned int lvl = 0);
515  void computeVVSFirstPhasePoseEstimation(const unsigned int iter, bool &isoJoIdentity_);
516  virtual void computeVVSInit();
519  virtual void computeVVSWeights();
521 
522  void displayFeaturesOnImage(const vpImage<unsigned char> &I, const unsigned int lvl);
523  void downScale(const unsigned int _scale);
524  void init(const vpImage<unsigned char> &I);
525  virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, const double radius,
526  const int idFace = 0, const std::string &name = "");
527  virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, const double radius, const int idFace = 0,
528  const std::string &name = "");
529  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
530  virtual void initFaceFromLines(vpMbtPolygon &polygon);
531  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders,
532  unsigned int &nberrors_circles);
533  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
534  void initPyramid(const vpImage<unsigned char> &_I, std::vector<const vpImage<unsigned char> *> &_pyramid);
535  void reInitLevel(const unsigned int _lvl);
536  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
537  void removeCircle(const std::string &name);
538  void removeCylinder(const std::string &name);
539  void removeLine(const std::string &name);
540  void resetMovingEdge();
541  void testTracking();
545  void upScale(const unsigned int _scale);
546  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
548 };
549 
550 #endif
std::vector< bool > getScales() const
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:269
unsigned int ncylinder
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
virtual void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking)
virtual void track(const vpImage< unsigned char > &I)=0
virtual void computeVVSInteractionMatrixAndResidu()=0
virtual void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name="")
vpColVector m_error_edge
(s - s*)
virtual void computeVVSInit()=0
void upScale(const unsigned int _scale)
vpColVector m_weightedError_edge
Weighted error.
void addLine(vpPoint &p1, vpPoint &p2, int polygon=-1, std::string name="")
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:149
virtual void computeProjectionError()
Implementation of an homogeneous matrix and operations on such kind of matrices.
std::vector< std::list< vpMbtDistanceCylinder * > > cylinders
Vector of the tracked cylinders.
unsigned int scaleLevel
virtual void loadConfigFile(const std::string &configFile)
virtual vpColVector getRobustWeights() const
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
vpColVector m_factor
Edge VVS variables.
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:119
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
std::vector< const vpImage< unsigned char > * > Ipyramid
virtual void computeVVSFirstPhasePoseEstimation(const unsigned int iter, bool &isoJoIdentity_)
virtual void cleanPyramid(std::map< std::string, std::vector< const vpImage< unsigned char > * > > &pyramid)
vpColVector m_w_edge
Robust weights.
vpColVector m_wCircles
void reInitLevel(const unsigned int _lvl)
Hybrid stereo (or more) tracker based on moving-edges and keypoints tracked using KLT tracker...
Definition: vpMe.h:60
virtual void getMovingEdge(vpMe &p_me) const
Make the complete tracking of an object by using its CAD model.
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
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
void setGoodMovingEdgesRatioThreshold(const double threshold)
void downScale(const unsigned int _scale)
Class that defines what is a point.
Definition: vpPoint.h:58
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:117
virtual void init(const vpImage< unsigned char > &I)=0
virtual void setOgreVisibilityTest(const bool &v)
vpAROgre * getOgreContext()
vpRobust m_robust_edge
Robust.
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:66
void initMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
virtual unsigned int initMbtTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, unsigned int lvl)
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:110
virtual vpColVector getError() const
void getLline(std::list< vpMbtDistanceLine * > &linesList, const unsigned int level=0) const
virtual void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
vpColVector m_errorCylinders
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
vpColVector m_errorCircles
vpColVector m_wCylinders
vpRobust m_robustLines
virtual vpMe getMovingEdge() const
void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace=-1, const std::string &name="")
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
virtual void setMovingEdge(const vpMe &me)
vpRobust m_robustCylinders
virtual void setScales(const std::vector< bool > &scales)
vpRobust m_robustCircles
virtual void setOgreVisibilityTest(const bool &v)
double getGoodMovingEdgesRatioThreshold() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual void initFaceFromLines(vpMbtPolygon &polygon)=0
virtual void testTracking()=0
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:58
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
std::vector< std::list< vpMbtDistanceLine * > > lines
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:587
vpColVector m_wLines
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)
vpMatrix m_L_edge
Interaction matrix.
unsigned int nline
void trackMovingEdge(const vpImage< unsigned char > &I)
virtual void setFarClippingDistance(const double &dist)
void computeVVSFirstPhase(const vpImage< unsigned char > &I, const unsigned int iter, double &count, const unsigned int lvl=0)
vpColVector m_errorLines
void removeLine(const std::string &name)
Make the complete stereo (or more) tracking of an object by using its CAD model.
void computeVVSFirstPhaseFactor(const vpImage< unsigned char > &I, const unsigned int lvl=0)
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 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)