Visual Servoing Platform  version 3.6.1 under development (2023-12-02)
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/vpPoint.h>
43 #include <visp3/mbt/vpMbTracker.h>
44 #include <visp3/mbt/vpMbtDistanceCircle.h>
45 #include <visp3/mbt/vpMbtDistanceCylinder.h>
46 #include <visp3/mbt/vpMbtDistanceLine.h>
47 #include <visp3/mbt/vpMbtMeLine.h>
48 #include <visp3/me/vpMe.h>
49 
50 #include <fstream>
51 #include <iostream>
52 #include <list>
53 #include <vector>
54 
55 #if defined(VISP_HAVE_COIN3D)
56 // Inventor includes
57 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
58 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
59 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
60 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
61 #include <Inventor/VRMLnodes/SoVRMLShape.h>
62 #include <Inventor/actions/SoGetMatrixAction.h>
63 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
64 #include <Inventor/actions/SoSearchAction.h>
65 #include <Inventor/actions/SoToVRML2Action.h>
66 #include <Inventor/actions/SoWriteAction.h>
67 #include <Inventor/misc/SoChildList.h>
68 #include <Inventor/nodes/SoSeparator.h>
69 #endif
70 
71 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
72 #include <opencv2/imgproc/imgproc.hpp>
73 #include <opencv2/imgproc/imgproc_c.h>
74 #endif
75 
237 class VISP_EXPORT vpMbEdgeTracker : public virtual vpMbTracker
238 {
239 protected:
245  std::vector<std::list<vpMbtDistanceLine *> > lines;
246 
248  std::vector<std::list<vpMbtDistanceCircle *> > circles;
249 
251  std::vector<std::list<vpMbtDistanceCylinder *> > cylinders;
252 
255  unsigned int nline;
256 
259  unsigned int ncircle;
260 
263  unsigned int ncylinder;
264 
266  unsigned int nbvisiblepolygone;
267 
271 
273  std::vector<bool> scales;
274 
277  std::vector<const vpImage<unsigned char> *> Ipyramid;
278 
282  unsigned int scaleLevel;
283 
286 
309  std::vector<std::vector<double> > m_featuresToBeDisplayedEdge;
310 
311 public:
312  vpMbEdgeTracker();
313  virtual ~vpMbEdgeTracker() override;
314 
317 
318  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
319  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false) override;
320  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
321  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false) override;
322 
323  void getLline(std::list<vpMbtDistanceLine *> &linesList, unsigned int level = 0) const;
324  void getLcircle(std::list<vpMbtDistanceCircle *> &circlesList, unsigned int level = 0) const;
325  void getLcylinder(std::list<vpMbtDistanceCylinder *> &cylindersList, unsigned int level = 0) const;
326 
327  virtual std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
328  const vpHomogeneousMatrix &cMo,
329  const vpCameraParameters &cam,
330  bool displayFullModel = false) override;
331 
338  virtual inline void getMovingEdge(vpMe &p_me) const { p_me = this->me; }
344  virtual inline vpMe getMovingEdge() const { return this->me; }
345 
346  virtual unsigned int getNbPoints(unsigned int level = 0) const;
347 
353  std::vector<bool> getScales() const { return scales; }
363  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt; }
364 
365  virtual inline vpColVector getError() const override { return m_error_edge; }
366 
367  virtual inline vpColVector getRobustWeights() const override { return m_w_edge; }
368 
369  virtual void loadConfigFile(const std::string &configFile, bool verbose = true) override;
370 
371  virtual void reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo,
372  bool verbose = false, const vpHomogeneousMatrix &T = vpHomogeneousMatrix());
373  void resetTracker() override;
374 
380  virtual void setCameraParameters(const vpCameraParameters &cam) override
381  {
382  m_cam = cam;
383 
384  for (unsigned int i = 0; i < scales.size(); i += 1) {
385  if (scales[i]) {
386  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
387  (*it)->setCameraParameters(m_cam);
388  }
389 
390  for (std::list<vpMbtDistanceCylinder *>::const_iterator it = cylinders[i].begin(); it != cylinders[i].end();
391  ++it) {
392  (*it)->setCameraParameters(m_cam);
393  }
394 
395  for (std::list<vpMbtDistanceCircle *>::const_iterator it = circles[i].begin(); it != circles[i].end(); ++it) {
396  (*it)->setCameraParameters(m_cam);
397  }
398  }
399  }
400  }
401 
402  virtual void setClipping(const unsigned int &flags) override;
403 
404  virtual void setFarClippingDistance(const double &dist) override;
405 
406  virtual void setNearClippingDistance(const double &dist) override;
407 
416  virtual void setOgreVisibilityTest(const bool &v) override
417  {
419 #ifdef VISP_HAVE_OGRE
420  faces.getOgreContext()->setWindowName("MBT Edge");
421 #endif
422  }
423 
429  virtual void setScanLineVisibilityTest(const bool &v) override
430  {
432 
433  for (unsigned int i = 0; i < scales.size(); i += 1) {
434  if (scales[i]) {
435  for (std::list<vpMbtDistanceLine *>::const_iterator it = lines[i].begin(); it != lines[i].end(); ++it) {
436  (*it)->useScanLine = v;
437  }
438  }
439  }
440  }
441 
455  void setGoodMovingEdgesRatioThreshold(double threshold) { percentageGdPt = threshold; }
456 
457  void setMovingEdge(const vpMe &me);
458 
459  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cdMo) override;
460  virtual void setPose(const vpImage<vpRGBa> &I_color, const vpHomogeneousMatrix &cdMo) override;
461 
462  void setScales(const std::vector<bool> &_scales);
463 
464  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
465 
466  virtual void track(const vpImage<unsigned char> &I) override;
467  virtual void track(const vpImage<vpRGBa> &I) override;
469 
470 protected:
473  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, double r, int idFace = -1,
474  const std::string &name = "");
475  void addCylinder(const vpPoint &P1, const vpPoint &P2, double r, int idFace = -1, const std::string &name = "");
476  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
477  void addPolygon(vpMbtPolygon &p);
478 
479  void cleanPyramid(std::vector<const vpImage<unsigned char> *> &_pyramid);
480  void computeProjectionError(const vpImage<unsigned char> &_I);
481 
482  void computeVVS(const vpImage<unsigned char> &_I, unsigned int lvl);
483  void computeVVSFirstPhase(const vpImage<unsigned char> &I, unsigned int iter, double &count, unsigned int lvl = 0);
484  void computeVVSFirstPhaseFactor(const vpImage<unsigned char> &I, unsigned int lvl = 0);
485  void computeVVSFirstPhasePoseEstimation(unsigned int iter, bool &isoJoIdentity);
486  virtual void computeVVSInit() override;
487  virtual void computeVVSInteractionMatrixAndResidu() override;
489  virtual void computeVVSWeights();
491 
492  void displayFeaturesOnImage(const vpImage<unsigned char> &I);
493  void displayFeaturesOnImage(const vpImage<vpRGBa> &I);
494  void downScale(const unsigned int _scale);
495  virtual std::vector<std::vector<double> > getFeaturesForDisplayEdge();
496  virtual void init(const vpImage<unsigned char> &I) override;
497  virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius, int idFace = 0,
498  const std::string &name = "") override;
499  virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace = 0,
500  const std::string &name = "") override;
501  virtual void initFaceFromCorners(vpMbtPolygon &polygon) override;
502  virtual void initFaceFromLines(vpMbtPolygon &polygon) override;
503  unsigned int initMbtTracking(unsigned int &nberrors_lines, unsigned int &nberrors_cylinders,
504  unsigned int &nberrors_circles);
505  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
506  void initPyramid(const vpImage<unsigned char> &_I, std::vector<const vpImage<unsigned char> *> &_pyramid);
507  void reInitLevel(const unsigned int _lvl);
508  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
509  void removeCircle(const std::string &name);
510  void removeCylinder(const std::string &name);
511  void removeLine(const std::string &name);
512  void resetMovingEdge();
513  virtual void testTracking() override;
514  void trackMovingEdge(const vpImage<unsigned char> &I);
515  void updateMovingEdge(const vpImage<unsigned char> &I);
516  void updateMovingEdgeWeights();
517  void upScale(const unsigned int _scale);
518  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
520 };
521 
522 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
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) 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.
virtual void setCameraParameters(const vpCameraParameters &cam) override
vpRobust m_robustCircles
virtual void setOgreVisibilityTest(const bool &v) override
virtual vpColVector getRobustWeights() const override
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
unsigned int scaleLevel
virtual vpColVector getError() const override
std::vector< const vpImage< unsigned char > * > Ipyramid
vpColVector m_weightedError_edge
Weighted error.
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)
virtual void getMovingEdge(vpMe &p_me) const
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.
vpColVector m_errorLines
vpColVector m_wCircles
unsigned int nline
vpColVector m_errorCylinders
Main methods for a model-based tracker.
Definition: vpMbTracker.h:105
virtual void track(const vpImage< unsigned char > &I)=0
virtual void resetTracker()=0
virtual void init(const vpImage< unsigned char > &I)=0
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 initFaceFromLines(vpMbtPolygon &polygon)=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 initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, double radius, int idFace=0, const std::string &name="")=0
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, double radius, int idFace=0, const std::string &name="")=0
virtual void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:608
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 computeVVSInteractionMatrixAndResidu()=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
virtual void computeVVSInit()=0
virtual void testTracking()=0
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:58
Definition: vpMe.h:120
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
Contains an M-estimator and various influence function.
Definition: vpRobust.h:83