Visual Servoing Platform  version 3.6.1 under development (2025-02-01)
vpMbEdgeTracker.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
39 #ifndef vpMbEdgeTracker_HH
40 #define vpMbEdgeTracker_HH
41 
42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpPoint.h>
44 #include <visp3/mbt/vpMbTracker.h>
45 #include <visp3/mbt/vpMbtDistanceCircle.h>
46 #include <visp3/mbt/vpMbtDistanceCylinder.h>
47 #include <visp3/mbt/vpMbtDistanceLine.h>
48 #include <visp3/mbt/vpMbtMeLine.h>
49 #include <visp3/me/vpMe.h>
50 
51 #include <fstream>
52 #include <iostream>
53 #include <list>
54 #include <vector>
55 
56 #if defined(VISP_HAVE_COIN3D)
57 // Inventor includes
58 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
59 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
60 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
61 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
62 #include <Inventor/VRMLnodes/SoVRMLShape.h>
63 #include <Inventor/actions/SoGetMatrixAction.h>
64 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
65 #include <Inventor/actions/SoSearchAction.h>
66 #include <Inventor/actions/SoToVRML2Action.h>
67 #include <Inventor/actions/SoWriteAction.h>
68 #include <Inventor/misc/SoChildList.h>
69 #include <Inventor/nodes/SoSeparator.h>
70 #endif
71 
72 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
73 #include <opencv2/imgproc/imgproc.hpp>
74 #if (VISP_HAVE_OPENCV_VERSION < 0x050000)
75 #include <opencv2/imgproc/imgproc_c.h>
76 #endif
77 #endif
78 
79 BEGIN_VISP_NAMESPACE
80 
250 class VISP_EXPORT vpMbEdgeTracker : public virtual vpMbTracker
251 {
252 protected:
258  std::vector<std::list<vpMbtDistanceLine *> > lines;
259 
261  std::vector<std::list<vpMbtDistanceCircle *> > circles;
262 
264  std::vector<std::list<vpMbtDistanceCylinder *> > cylinders;
265 
268  unsigned int nline;
269 
272  unsigned int ncircle;
273 
276  unsigned int ncylinder;
277 
279  unsigned int nbvisiblepolygone;
280 
284 
286  std::vector<bool> scales;
287 
290  std::vector<const vpImage<unsigned char> *> Ipyramid;
291 
295  unsigned int scaleLevel;
296 
299 
322  std::vector<std::vector<double> > m_featuresToBeDisplayedEdge;
323 
324 public:
325  vpMbEdgeTracker();
326  virtual ~vpMbEdgeTracker() VP_OVERRIDE;
327 
330 
331  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
332  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false) VP_OVERRIDE;
333  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
334  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false) VP_OVERRIDE;
335 
336  void getLline(std::list<vpMbtDistanceLine *> &linesList, unsigned int level = 0) const;
337  void getLcircle(std::list<vpMbtDistanceCircle *> &circlesList, unsigned int level = 0) const;
338  void getLcylinder(std::list<vpMbtDistanceCylinder *> &cylindersList, unsigned int level = 0) const;
339 
340  virtual std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
341  const vpHomogeneousMatrix &cMo,
342  const vpCameraParameters &cam,
343  bool displayFullModel = false) VP_OVERRIDE;
344 
351  virtual inline void getMovingEdge(vpMe &p_me) const { p_me = this->me; }
357  virtual inline vpMe getMovingEdge() const { return this->me; }
358 
359  virtual unsigned int getNbPoints(unsigned int level = 0) const;
360 
366  std::vector<bool> getScales() const { return scales; }
376  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt; }
377 
378  virtual inline vpColVector getError() const VP_OVERRIDE { return m_error_edge; }
379 
380  virtual inline vpColVector getRobustWeights() const VP_OVERRIDE { return m_w_edge; }
381 
382  virtual void loadConfigFile(const std::string &configFile, bool verbose = true) VP_OVERRIDE;
383 
384  virtual void reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo,
385  bool verbose = false, const vpHomogeneousMatrix &T = vpHomogeneousMatrix());
386  void resetTracker() VP_OVERRIDE;
387 
393  virtual void setCameraParameters(const vpCameraParameters &cam) VP_OVERRIDE
394  {
395  m_cam = cam;
396 
397  for (unsigned int i = 0; i < scales.size(); i += 1) {
398  if (scales[i]) {
399  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
400  (*it)->setCameraParameters(m_cam);
401  }
402 
403  for (std::list<vpMbtDistanceCylinder *>::const_iterator it = cylinders[i].begin(); it != cylinders[i].end();
404  ++it) {
405  (*it)->setCameraParameters(m_cam);
406  }
407 
408  for (std::list<vpMbtDistanceCircle *>::const_iterator it = circles[i].begin(); it != circles[i].end(); ++it) {
409  (*it)->setCameraParameters(m_cam);
410  }
411  }
412  }
413  }
414 
415  virtual void setClipping(const unsigned int &flags) VP_OVERRIDE;
416 
417  virtual void setFarClippingDistance(const double &dist) VP_OVERRIDE;
418 
419  virtual void setNearClippingDistance(const double &dist) VP_OVERRIDE;
420 
429  virtual void setOgreVisibilityTest(const bool &v) VP_OVERRIDE
430  {
432 #ifdef VISP_HAVE_OGRE
433  faces.getOgreContext()->setWindowName("MBT Edge");
434 #endif
435  }
436 
442  virtual void setScanLineVisibilityTest(const bool &v) VP_OVERRIDE
443  {
445 
446  for (unsigned int i = 0; i < scales.size(); i += 1) {
447  if (scales[i]) {
448  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
449  (*it)->useScanLine = v;
450  }
451  }
452  }
453  }
454 
468  void setGoodMovingEdgesRatioThreshold(double threshold) { percentageGdPt = threshold; }
469 
470  void setMovingEdge(const vpMe &me);
471 
472  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cdMo) VP_OVERRIDE;
473  virtual void setPose(const vpImage<vpRGBa> &I_color, const vpHomogeneousMatrix &cdMo) VP_OVERRIDE;
474 
475  void setScales(const std::vector<bool> &_scales);
476 
477  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
478 
479  virtual void track(const vpImage<unsigned char> &I) VP_OVERRIDE;
480  virtual void track(const vpImage<vpRGBa> &I) VP_OVERRIDE;
482 
483 protected:
486  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, double r, int idFace = -1,
487  const std::string &name = "");
488  void addCylinder(const vpPoint &P1, const vpPoint &P2, double r, int idFace = -1, const std::string &name = "");
489  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
490  void addPolygon(vpMbtPolygon &p);
491 
492  void cleanPyramid(std::vector<const vpImage<unsigned char> *> &_pyramid);
493  void computeProjectionError(const vpImage<unsigned char> &_I);
494 
495  void computeVVS(const vpImage<unsigned char> &_I, unsigned int lvl);
496  void computeVVSFirstPhase(const vpImage<unsigned char> &I, unsigned int iter, double &count, unsigned int lvl = 0);
497  void computeVVSFirstPhaseFactor(const vpImage<unsigned char> &I, unsigned int lvl = 0);
498  void computeVVSFirstPhasePoseEstimation(unsigned int iter, bool &isoJoIdentity);
499  virtual void computeVVSInit() VP_OVERRIDE;
500  virtual void computeVVSInteractionMatrixAndResidu() VP_OVERRIDE;
501  virtual void computeVVSInteractionMatrixAndResidu(const vpImage<unsigned char> &I);
502  virtual void computeVVSWeights();
503  using vpMbTracker::computeVVSWeights;
504 
505  void displayFeaturesOnImage(const vpImage<unsigned char> &I);
506  void displayFeaturesOnImage(const vpImage<vpRGBa> &I);
507  void downScale(const unsigned int _scale);
508  virtual std::vector<std::vector<double> > getFeaturesForDisplayEdge();
509  virtual void init(const vpImage<unsigned char> &I) VP_OVERRIDE;
510  virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius, int idFace = 0,
511  const std::string &name = "") VP_OVERRIDE;
512  virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace = 0,
513  const std::string &name = "") VP_OVERRIDE;
514  virtual void initFaceFromCorners(vpMbtPolygon &polygon) VP_OVERRIDE;
515  virtual void initFaceFromLines(vpMbtPolygon &polygon) VP_OVERRIDE;
516  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders,
517  unsigned int &nberrors_circles);
518  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
519  void initPyramid(const vpImage<unsigned char> &_I, std::vector<const vpImage<unsigned char> *> &_pyramid);
520  void reInitLevel(const unsigned int _lvl);
521  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
522  void removeCircle(const std::string &name);
523  void removeCylinder(const std::string &name);
524  void removeLine(const std::string &name);
525  void resetMovingEdge();
526  virtual void testTracking() VP_OVERRIDE;
527  void trackMovingEdge(const vpImage<unsigned char> &I);
528  void updateMovingEdge(const vpImage<unsigned char> &I);
529  void updateMovingEdgeWeights();
530  void upScale(const unsigned int _scale);
531  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
533 };
534 END_VISP_NAMESPACE
535 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition: vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Make the complete tracking of an object by using its CAD model.
double getGoodMovingEdgesRatioThreshold() const
vpColVector m_errorCircles
vpColVector m_w_edge
Robust weights.
vpRobust m_robust_edge
Robust.
std::vector< std::list< vpMbtDistanceLine * > > lines
vpMe me
The moving edges parameters.
std::vector< bool > getScales() const
virtual void setScanLineVisibilityTest(const bool &v) VP_OVERRIDE
vpColVector m_wLines
vpColVector m_error_edge
(s - s*)
unsigned int ncylinder
std::vector< std::list< vpMbtDistanceCylinder * > > cylinders
Vector of the tracked cylinders.
std::vector< std::vector< double > > m_featuresToBeDisplayedEdge
Display features.
unsigned int nbvisiblepolygone
Number of polygon (face) currently visible.
vpRobust m_robustCircles
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
unsigned int scaleLevel
std::vector< const vpImage< unsigned char > * > Ipyramid
vpColVector m_weightedError_edge
Weighted error.
virtual void setOgreVisibilityTest(const bool &v) VP_OVERRIDE
unsigned int ncircle
std::vector< bool > scales
Vector of scale level to use for the multi-scale tracking.
vpMatrix m_L_edge
Interaction matrix.
vpRobust m_robustCylinders
void setGoodMovingEdgesRatioThreshold(double threshold)
vpRobust m_robustLines
virtual vpMe getMovingEdge() const
vpColVector m_wCylinders
unsigned int nbFeaturesForProjErrorComputation
Number of features used in the computation of the projection error.
vpColVector m_factor
Edge VVS variables.
virtual vpColVector getRobustWeights() const VP_OVERRIDE
vpColVector m_errorLines
vpColVector m_wCircles
unsigned int nline
virtual vpColVector getError() const VP_OVERRIDE
vpColVector m_errorCylinders
Main methods for a model-based tracker.
Definition: vpMbTracker.h:107
virtual void track(const vpImage< unsigned char > &I)=0
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)
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:610
virtual void setOgreVisibilityTest(const bool &v)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)=0
virtual void setNearClippingDistance(const double &dist)
virtual void setFarClippingDistance(const double &dist)
virtual void setClipping(const unsigned int &flags)
virtual void computeVVSInit()=0
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
Manage a circle used in the model-based tracker.
Manage a cylinder used in the model-based tracker.
Manage the line of a polygon used in the model-based tracker.
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:60
Definition: vpMe.h:134
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Definition: vpRGBa.h:70
Contains an M-estimator and various influence function.
Definition: vpRobust.h:84