Visual Servoing Platform  version 3.3.1 under development (2020-06-03)
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 protected:
266  std::vector<std::list<vpMbtDistanceLine *> > lines;
267 
269  std::vector<std::list<vpMbtDistanceCircle *> > circles;
270 
272  std::vector<std::list<vpMbtDistanceCylinder *> > cylinders;
273 
276  unsigned int nline;
277 
280  unsigned int ncircle;
281 
284  unsigned int ncylinder;
285 
287  unsigned int nbvisiblepolygone;
288 
292 
294  std::vector<bool> scales;
295 
298  std::vector<const vpImage<unsigned char> *> Ipyramid;
299 
303  unsigned int scaleLevel;
304 
307 
330  std::vector<std::vector<double> > m_featuresToBeDisplayedEdge;
331 
332 public:
333  vpMbEdgeTracker();
334  virtual ~vpMbEdgeTracker();
335 
338 
339  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
340  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
341  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
342  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
343 
344  void getLline(std::list<vpMbtDistanceLine *> &linesList, unsigned int level = 0) const;
345  void getLcircle(std::list<vpMbtDistanceCircle *> &circlesList, unsigned int level = 0) const;
346  void getLcylinder(std::list<vpMbtDistanceCylinder *> &cylindersList, unsigned int level = 0) const;
347 
348  virtual std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
349  const vpHomogeneousMatrix &cMo,
350  const vpCameraParameters &cam,
351  bool displayFullModel=false);
352 
359  virtual inline void getMovingEdge(vpMe &p_me) const { p_me = this->me; }
365  virtual inline vpMe getMovingEdge() const { return this->me; }
366 
367  virtual unsigned int getNbPoints(unsigned int level = 0) const;
368 
374  std::vector<bool> getScales() const { return scales; }
384  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt; }
385 
386  virtual inline vpColVector getError() const { return m_error_edge; }
387 
388  virtual inline vpColVector getRobustWeights() const { return m_w_edge; }
389 
390  void loadConfigFile(const std::string &configFile);
391 
392  virtual void reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name,
393  const vpHomogeneousMatrix &cMo, bool verbose = false,
395  void resetTracker();
396 
402  virtual void setCameraParameters(const vpCameraParameters &cam)
403  {
404  m_cam = cam;
405 
406  for (unsigned int i = 0; i < scales.size(); i += 1) {
407  if (scales[i]) {
408  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
409  (*it)->setCameraParameters(m_cam);
410  }
411 
412  for (std::list<vpMbtDistanceCylinder *>::const_iterator it = cylinders[i].begin(); it != cylinders[i].end();
413  ++it) {
414  (*it)->setCameraParameters(m_cam);
415  }
416 
417  for (std::list<vpMbtDistanceCircle *>::const_iterator it = circles[i].begin(); it != circles[i].end(); ++it) {
418  (*it)->setCameraParameters(m_cam);
419  }
420  }
421  }
422  }
423 
424  virtual void setClipping(const unsigned int &flags);
425 
426  virtual void setFarClippingDistance(const double &dist);
427 
428  virtual void setNearClippingDistance(const double &dist);
429 
438  virtual void setOgreVisibilityTest(const bool &v)
439  {
441 #ifdef VISP_HAVE_OGRE
442  faces.getOgreContext()->setWindowName("MBT Edge");
443 #endif
444  }
445 
451  virtual void setScanLineVisibilityTest(const bool &v)
452  {
454 
455  for (unsigned int i = 0; i < scales.size(); i += 1) {
456  if (scales[i]) {
457  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
458  (*it)->useScanLine = v;
459  }
460  }
461  }
462  }
463 
477  void setGoodMovingEdgesRatioThreshold(double threshold) { percentageGdPt = threshold; }
478 
479  void setMovingEdge(const vpMe &me);
480 
481  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cdMo);
482  virtual void setPose(const vpImage<vpRGBa> &I_color, const vpHomogeneousMatrix &cdMo);
483 
484  void setScales(const std::vector<bool> &_scales);
485 
486  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
487 
488  virtual void track(const vpImage<unsigned char> &I);
489  virtual void track(const vpImage<vpRGBa> &I);
491 
492 protected:
495  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, double r, int idFace = -1,
496  const std::string &name = "");
497  void addCylinder(const vpPoint &P1, const vpPoint &P2, double r, int idFace = -1, const std::string &name = "");
498  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
499  void addPolygon(vpMbtPolygon &p);
500 
501  void cleanPyramid(std::vector<const vpImage<unsigned char> *> &_pyramid);
502  void computeProjectionError(const vpImage<unsigned char> &_I);
503 
504  void computeVVS(const vpImage<unsigned char> &_I, unsigned int lvl);
505  void computeVVSFirstPhase(const vpImage<unsigned char> &I, unsigned int iter, double &count,
506  unsigned int lvl = 0);
507  void computeVVSFirstPhaseFactor(const vpImage<unsigned char> &I, unsigned int lvl = 0);
508  void computeVVSFirstPhasePoseEstimation(unsigned int iter, bool &isoJoIdentity_);
509  virtual void computeVVSInit();
512  virtual void computeVVSWeights();
514 
515  void displayFeaturesOnImage(const vpImage<unsigned char> &I);
516  void displayFeaturesOnImage(const vpImage<vpRGBa> &I);
517  void downScale(const unsigned int _scale);
518  virtual std::vector<std::vector<double> > getFeaturesForDisplayEdge();
519  virtual void init(const vpImage<unsigned char> &I);
520  virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius,
521  int idFace = 0, const std::string &name = "");
522  virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace = 0,
523  const std::string &name = "");
524  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
525  virtual void initFaceFromLines(vpMbtPolygon &polygon);
526  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders,
527  unsigned int &nberrors_circles);
528  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
529  void initPyramid(const vpImage<unsigned char> &_I, std::vector<const vpImage<unsigned char> *> &_pyramid);
530  void reInitLevel(const unsigned int _lvl);
531  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
532  void removeCircle(const std::string &name);
533  void removeCylinder(const std::string &name);
534  void removeLine(const std::string &name);
535  void resetMovingEdge();
536  virtual void testTracking();
537  void trackMovingEdge(const vpImage<unsigned char> &I);
538  void updateMovingEdge(const vpImage<unsigned char> &I);
539  void updateMovingEdgeWeights();
540  void upScale(const unsigned int _scale);
541  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
543 };
544 
545 #endif
unsigned int ncylinder
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:156
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
vpColVector m_weightedError_edge
Weighted error.
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)
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:157
vpColVector m_factor
Edge VVS variables.
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
Definition: vpMe.h:60
Make the complete tracking of an object by using its CAD model.
vpMe me
The moving edges parameters.
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
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
virtual void init(const vpImage< unsigned char > &I)=0
virtual void setOgreVisibilityTest(const bool &v)
vpRobust m_robust_edge
Robust.
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
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 void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
vpColVector m_errorCylinders
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
vpRobust m_robustCircles
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
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
virtual void setFarClippingDistance(const double &dist)
vpColVector m_errorLines
std::vector< bool > getScales() const
std::vector< std::vector< double > > m_featuresToBeDisplayedEdge
Display features.
virtual void setNearClippingDistance(const double &dist)