Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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/mbt/vpMbTracker.h>
51 #include <visp3/mbt/vpMbtDistanceCircle.h>
52 #include <visp3/mbt/vpMbtDistanceCylinder.h>
53 #include <visp3/mbt/vpMbtDistanceLine.h>
54 #include <visp3/mbt/vpMbtMeLine.h>
55 #include <visp3/me/vpMe.h>
56 
57 #include <fstream>
58 #include <iostream>
59 #include <list>
60 #include <vector>
61 
62 #if defined(VISP_HAVE_COIN3D)
63 // Inventor includes
64 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
65 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
66 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
67 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
68 #include <Inventor/VRMLnodes/SoVRMLShape.h>
69 #include <Inventor/actions/SoGetMatrixAction.h>
70 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
71 #include <Inventor/actions/SoSearchAction.h>
72 #include <Inventor/actions/SoToVRML2Action.h>
73 #include <Inventor/actions/SoWriteAction.h>
74 #include <Inventor/misc/SoChildList.h>
75 #include <Inventor/nodes/SoSeparator.h>
76 #endif
77 
78 #ifdef VISP_HAVE_OPENCV
79 #if VISP_HAVE_OPENCV_VERSION >= 0x020101
80 #include <opencv2/core/core.hpp>
81 #include <opencv2/imgproc/imgproc.hpp>
82 #include <opencv2/imgproc/imgproc_c.h>
83 #else
84 #include <cv.h>
85 #endif
86 #endif
87 
258 class VISP_EXPORT vpMbEdgeTracker : public virtual vpMbTracker
259 {
260  friend class vpMbEdgeMultiTracker;
262 
263 protected:
269  std::vector<std::list<vpMbtDistanceLine *> > lines;
270 
272  std::vector<std::list<vpMbtDistanceCircle *> > circles;
273 
275  std::vector<std::list<vpMbtDistanceCylinder *> > cylinders;
276 
279  unsigned int nline;
280 
283  unsigned int ncircle;
284 
287  unsigned int ncylinder;
288 
290  unsigned int nbvisiblepolygone;
291 
295 
297  std::vector<bool> scales;
298 
301  std::vector<const vpImage<unsigned char> *> Ipyramid;
302 
306  unsigned int scaleLevel;
307 
310 
333  std::vector<std::vector<double> > m_featuresToBeDisplayedEdge;
334 
335 public:
336  vpMbEdgeTracker();
337  virtual ~vpMbEdgeTracker();
338 
341 
342  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
343  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
344  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
345  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
346 
347  void getLline(std::list<vpMbtDistanceLine *> &linesList, unsigned int level = 0) const;
348  void getLcircle(std::list<vpMbtDistanceCircle *> &circlesList, unsigned int level = 0) const;
349  void getLcylinder(std::list<vpMbtDistanceCylinder *> &cylindersList, unsigned int level = 0) const;
350 
351  virtual std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
352  const vpHomogeneousMatrix &cMo,
353  const vpCameraParameters &cam,
354  bool displayFullModel=false);
355 
362  virtual inline void getMovingEdge(vpMe &p_me) const { p_me = this->me; }
368  virtual inline vpMe getMovingEdge() const { return this->me; }
369 
370  virtual unsigned int getNbPoints(unsigned int level = 0) const;
371 
377  std::vector<bool> getScales() const { return scales; }
387  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt; }
388 
389  virtual inline vpColVector getError() const { return m_error_edge; }
390 
391  virtual inline vpColVector getRobustWeights() const { return m_w_edge; }
392 
393  void loadConfigFile(const std::string &configFile);
394 
395  virtual void reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name,
396  const vpHomogeneousMatrix &cMo, bool verbose = false,
398  void resetTracker();
399 
405  virtual void setCameraParameters(const vpCameraParameters &cam)
406  {
407  m_cam = cam;
408 
409  for (unsigned int i = 0; i < scales.size(); i += 1) {
410  if (scales[i]) {
411  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
412  (*it)->setCameraParameters(m_cam);
413  }
414 
415  for (std::list<vpMbtDistanceCylinder *>::const_iterator it = cylinders[i].begin(); it != cylinders[i].end();
416  ++it) {
417  (*it)->setCameraParameters(m_cam);
418  }
419 
420  for (std::list<vpMbtDistanceCircle *>::const_iterator it = circles[i].begin(); it != circles[i].end(); ++it) {
421  (*it)->setCameraParameters(m_cam);
422  }
423  }
424  }
425  }
426 
427  virtual void setClipping(const unsigned int &flags);
428 
429  virtual void setFarClippingDistance(const double &dist);
430 
431  virtual void setNearClippingDistance(const double &dist);
432 
441  virtual void setOgreVisibilityTest(const bool &v)
442  {
444 #ifdef VISP_HAVE_OGRE
445  faces.getOgreContext()->setWindowName("MBT Edge");
446 #endif
447  }
448 
454  virtual void setScanLineVisibilityTest(const bool &v)
455  {
457 
458  for (unsigned int i = 0; i < scales.size(); i += 1) {
459  if (scales[i]) {
460  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
461  (*it)->useScanLine = v;
462  }
463  }
464  }
465  }
466 
480  void setGoodMovingEdgesRatioThreshold(double threshold) { percentageGdPt = threshold; }
481 
482  void setMovingEdge(const vpMe &me);
483 
484  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cdMo);
485  virtual void setPose(const vpImage<vpRGBa> &I_color, const vpHomogeneousMatrix &cdMo);
486 
487  void setScales(const std::vector<bool> &_scales);
488 
489  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
490 
491  virtual void track(const vpImage<unsigned char> &I);
492  virtual void track(const vpImage<vpRGBa> &I);
494 
495 protected:
498  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, double r, int idFace = -1,
499  const std::string &name = "");
500  void addCylinder(const vpPoint &P1, const vpPoint &P2, double r, int idFace = -1, const std::string &name = "");
501  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
502  void addPolygon(vpMbtPolygon &p);
503 
504  void cleanPyramid(std::vector<const vpImage<unsigned char> *> &_pyramid);
506 
507  void computeVVS(const vpImage<unsigned char> &_I, unsigned int lvl);
508  void computeVVSFirstPhase(const vpImage<unsigned char> &I, unsigned int iter, double &count,
509  unsigned int lvl = 0);
510  void computeVVSFirstPhaseFactor(const vpImage<unsigned char> &I, unsigned int lvl = 0);
511  void computeVVSFirstPhasePoseEstimation(unsigned int iter, bool &isoJoIdentity_);
512  virtual void computeVVSInit();
515  virtual void computeVVSWeights();
517 
520  void downScale(const unsigned int _scale);
521  virtual std::vector<std::vector<double> > getFeaturesForDisplayEdge();
522  virtual void init(const vpImage<unsigned char> &I);
523  virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius,
524  int idFace = 0, const std::string &name = "");
525  virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace = 0,
526  const std::string &name = "");
527  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
528  virtual void initFaceFromLines(vpMbtPolygon &polygon);
529  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders,
530  unsigned int &nberrors_circles);
531  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
532  void initPyramid(const vpImage<unsigned char> &_I, std::vector<const vpImage<unsigned char> *> &_pyramid);
533  void reInitLevel(const unsigned int _lvl);
534  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
535  void removeCircle(const std::string &name);
536  void removeCylinder(const std::string &name);
537  void removeLine(const std::string &name);
538  void resetMovingEdge();
539  virtual void testTracking();
543  void upScale(const unsigned int _scale);
544  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
546 };
547 
548 #endif
void computeVVSFirstPhase(const vpImage< unsigned char > &I, unsigned int iter, double &count, unsigned int lvl=0)
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:269
unsigned int ncylinder
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
virtual void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking)
void getLline(std::list< vpMbtDistanceLine *> &linesList, unsigned int level=0) const
vpCameraParameters m_cam
The camera parameters.
Definition: vpMbTracker.h:111
virtual std::vector< std::vector< double > > getFeaturesForDisplayEdge()
void setGoodMovingEdgesRatioThreshold(double threshold)
virtual void track(const vpImage< unsigned char > &I)=0
virtual void computeVVSInteractionMatrixAndResidu()=0
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:143
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
void getLcylinder(std::list< vpMbtDistanceCylinder *> &cylindersList, unsigned int level=0) const
virtual void loadConfigFile(const std::string &configFile)
Class to define colors available for display functionnalities.
Definition: vpColor.h:119
vpColVector m_factor
Edge VVS variables.
virtual ~vpMbEdgeTracker()
unsigned int ncircle
virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius, int idFace=0, const std::string &name="")=0
std::vector< const vpImage< unsigned char > * > Ipyramid
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 setMovingEdge(const vpMe &moving_edge)
Make the complete tracking of an object by using its CAD model.
void removeCircle(const std::string &name)
void removeCylinder(const std::string &name)
vpMe me
The moving edges parameters.
void updateMovingEdge(const vpImage< unsigned char > &I)
virtual void resetTracker()=0
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)=0
void downScale(const unsigned int _scale)
Class that defines what is a point.
Definition: vpPoint.h:58
virtual void init(const vpImage< unsigned char > &I)=0
void getLcircle(std::list< vpMbtDistanceCircle *> &circlesList, unsigned int level=0) const
void addCylinder(const vpPoint &P1, const vpPoint &P2, double r, int idFace=-1, const std::string &name="")
virtual void setOgreVisibilityTest(const bool &v)
vpAROgre * getOgreContext()
vpRobust m_robust_edge
Robust.
std::vector< bool > scales
Vector of scale level to use for the multi-scale tracking.
virtual void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, double r, const std::string &name="")
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 void getMovingEdge(vpMe &p_me) const
unsigned int nbFeaturesForProjErrorComputation
Number of features used in the computation of the projection error.
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
Generic class defining intrinsic camera parameters.
double getGoodMovingEdgesRatioThreshold() const
Main methods for a model-based tracker.
Definition: vpMbTracker.h:104
virtual unsigned int initMbtTracking(std::map< std::string, const vpImage< unsigned char > *> &mapOfImages, unsigned int lvl)
virtual void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
virtual void initPyramid(const std::map< std::string, const vpImage< unsigned char > *> &mapOfImages, std::map< std::string, std::vector< const vpImage< unsigned char > *> > &pyramid)
void computeVVSFirstPhaseFactor(const vpImage< unsigned char > &I, unsigned int lvl=0)
vpColVector m_errorCylinders
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
vpColVector m_errorCircles
vpColVector m_wCylinders
vpRobust m_robustLines
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
virtual vpColVector getError() const
void addPolygon(const std::vector< vpPoint > &corners, int idFace=-1, const std::string &polygonName="", bool useLod=false, double minPolygonAreaThreshold=2500.0, double minLineLengthThreshold=50.0)
vpRobust m_robustCylinders
virtual std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)=0
virtual void setScales(const std::vector< bool > &scales)
vpRobust m_robustCircles
virtual void computeVVSFirstPhasePoseEstimation(unsigned int iter, bool &isoJoIdentity_)
virtual void setOgreVisibilityTest(const bool &v)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace=0, const std::string &name="")=0
virtual vpColVector getRobustWeights() const
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.
virtual vpMe getMovingEdge() const
void visibleFace(const vpImage< unsigned char > &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline)
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:597
vpColVector m_wLines
virtual void setClipping(const unsigned int &flags)
virtual void setCameraParameters(const vpCameraParameters &cam)
vpMatrix m_L_edge
Interaction matrix.
unsigned int nline
void trackMovingEdge(const vpImage< unsigned char > &I)
virtual void setFarClippingDistance(const double &dist)
vpColVector m_errorLines
void removeLine(const std::string &name)
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Make the complete stereo (or more) tracking of an object by using its CAD model.
std::vector< bool > getScales() const
void displayFeaturesOnImage(const vpImage< unsigned char > &I)
std::vector< std::vector< double > > m_featuresToBeDisplayedEdge
Display features.
virtual unsigned int getNbPoints(unsigned int level=0) const
virtual void cleanPyramid(std::map< std::string, std::vector< const vpImage< unsigned char > *> > &pyramid)
virtual void setNearClippingDistance(const double &dist)