Visual Servoing Platform  version 3.6.1 under development (2024-04-22)
vpTemplateTracker.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Template tracker.
33  *
34  * Authors:
35  * Amaury Dame
36  * Aurelien Yol
37  *
38 *****************************************************************************/
44 #ifndef vpTemplateTracker_hh
45 #define vpTemplateTracker_hh
46 
47 #include <math.h>
48 
49 #include <visp3/core/vpImageFilter.h>
50 #include <visp3/tt/vpTemplateTrackerHeader.h>
51 #include <visp3/tt/vpTemplateTrackerWarp.h>
52 #include <visp3/tt/vpTemplateTrackerZone.h>
53 
61 class VISP_EXPORT vpTemplateTracker
62 {
63 protected:
64  // traitement pyramidal
65  unsigned int nbLvlPyr; // If = 1, disable pyramidal usage
66  unsigned int l0Pyr;
68  // For evolRMS computation
69  double evolRMS;
70  std::vector<double> x_pos;
71  std::vector<double> y_pos;
72  double evolRMS_eps;
73 
77  unsigned int templateSize;
78  unsigned int *templateSizePyr;
82  unsigned int templateSelectSize;
83 
84 #ifndef DOXYGEN_SHOULD_SKIP_THIS
85  vpTemplateTrackerPointSuppMIInv *ptTemplateSupp; // pour inverse et compo
86  vpTemplateTrackerPointSuppMIInv **ptTemplateSuppPyr; // pour inverse et compo
87 #endif
88 
93 
95 
105 
106  double gain;
109  bool blur;
110  bool useBrent;
111  unsigned int nbIterBrent;
112  unsigned int taillef;
113  double *fgG;
114  double *fgdG;
115  double ratioPixelIn;
116  int mod_i;
117  int mod_j; // variable de sampling de zone de reference
118  unsigned int nbParam;
119  double lambdaDep;
120  unsigned int iterationMax;
121  // pour BFGS
122  unsigned int iterationGlobale;
123  // diverge is set to true if there is no more point in the tracked area
124  bool diverge;
125  unsigned int nbIteration;
128 
130  // Parametre de deplacement
133 
134  // temporary values for warping
137  // temporary derivative matrix
139 
143  vpTemplateTrackerZone zoneRef_; // Reference zone
144 
145 public:
148  : nbLvlPyr(0), l0Pyr(0), pyrInitialised(false), ptTemplate(nullptr), ptTemplatePyr(nullptr), ptTemplateInit(false),
149  templateSize(0), templateSizePyr(nullptr), ptTemplateSelect(nullptr), ptTemplateSelectPyr(nullptr),
150  ptTemplateSelectInit(false), templateSelectSize(0), ptTemplateSupp(nullptr), ptTemplateSuppPyr(nullptr),
151  ptTemplateCompo(nullptr), ptTemplateCompoPyr(nullptr), zoneTracked(nullptr), zoneTrackedPyr(nullptr), pyr_IDes(nullptr), H(),
152  Hdesire(), HdesirePyr(nullptr), HLM(), HLMdesire(), HLMdesirePyr(nullptr), HLMdesireInverse(),
153  HLMdesireInversePyr(nullptr), G(), gain(0), thresholdGradient(0), costFunctionVerification(false), blur(false),
154  useBrent(false), nbIterBrent(0), taillef(0), fgG(nullptr), fgdG(nullptr), ratioPixelIn(0), mod_i(0), mod_j(0),
155  nbParam(), lambdaDep(0), iterationMax(0), iterationGlobale(0), diverge(false), nbIteration(0),
156  useCompositionnal(false), useInverse(false), Warp(nullptr), p(), dp(), X1(), X2(), dW(), BI(), dIx(), dIy(),
157  zoneRef_()
158  {
159  }
160  explicit vpTemplateTracker(vpTemplateTrackerWarp *_warp);
161  virtual ~vpTemplateTracker();
162 
163  void display(const vpImage<unsigned char> &I, const vpColor &col = vpColor::green, unsigned int thickness = 3);
164  void display(const vpImage<vpRGBa> &I, const vpColor &col = vpColor::green, unsigned int thickness = 3);
165 
166  bool getDiverge() const { return diverge; }
167  vpColVector getdp() { return dp; }
168  vpColVector getG() const { return G; }
169  vpMatrix getH() const { return H; }
170  unsigned int getNbParam() const { return nbParam; }
171  unsigned int getNbIteration() const { return nbIteration; }
172  vpColVector getp() const { return p; }
173  double getRatioPixelIn() const { return ratioPixelIn; }
174 
179  vpTemplateTrackerWarp *getWarp() const { return Warp; }
180 
184  vpTemplateTrackerZone getZoneRef() const { return zoneRef_; }
185 
186  void initClick(const vpImage<unsigned char> &I, bool delaunay = false);
187  void initFromPoints(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &v_ip, bool delaunay = false);
188  void initFromZone(const vpImage<unsigned char> &I, const vpTemplateTrackerZone &zone);
189 
190  void resetTracker();
191 
192  void setBlur(bool b) { blur = b; }
193  void setCostFunctionVerification(bool b) { costFunctionVerification = b; }
194  void setGain(double g) { gain = g; }
195  void setGaussianFilterSize(unsigned int new_taill);
196  void setHDes(vpMatrix &tH)
197  {
198  Hdesire = tH;
199  vpMatrix::computeHLM(Hdesire, lambdaDep, HLMdesire);
200  HLMdesireInverse = HLMdesire.inverseByLU();
201  }
207  void setIterationMax(const unsigned int &n) { iterationMax = n; }
212  void setLambda(double l) { lambdaDep = l; }
213  void setNbIterBrent(const unsigned int &b) { nbIterBrent = b; }
214  void setp(const vpColVector &tp)
215  {
216  p = tp;
217  diverge = false;
218  iterationGlobale = 0;
219  }
229  void setPyramidal(unsigned int nlevels = 2, unsigned int level_to_stop = 1)
230  {
231  nbLvlPyr = nlevels;
232  l0Pyr = level_to_stop;
233  if (l0Pyr >= nlevels) {
234  std::cout << "Warning: level_to_stop: " << level_to_stop << " higher than level_to_start: " << nlevels - 1
235  << " (nlevels-1)" << std::endl;
236  std::cout << "Level to stop put to: " << nlevels - 1 << std::endl;
237  l0Pyr = nlevels - 1;
238  }
239  }
248  void setSampling(int sample_i, int sample_j)
249  {
250  mod_i = sample_i;
251  mod_j = sample_j;
252  }
253  void setThresholdGradient(double threshold) { thresholdGradient = threshold; }
263  void setThresholdResidualDifference(double threshold) { evolRMS_eps = threshold; }
264 
266  void setUseBrent(bool b) { useBrent = b; }
267 
268  void track(const vpImage<unsigned char> &I);
269  void trackRobust(const vpImage<unsigned char> &I);
270 
271 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
281  vp_deprecated void setThresholdRMS(double threshold) { (void)threshold; }
283 #endif
284 
285 protected:
286  void computeEvalRMS(const vpColVector &p);
287  void computeOptimalBrentGain(const vpImage<unsigned char> &I, vpColVector &tp, double tMI, vpColVector &direction,
288  double &alpha);
289  virtual double getCost(const vpImage<unsigned char> &I, const vpColVector &tp) = 0;
290  void getGaussianBluredImage(const vpImage<unsigned char> &I) { vpImageFilter::filter(I, BI, fgG, taillef); }
291  virtual void initHessienDesired(const vpImage<unsigned char> &I) = 0;
292  virtual void initHessienDesiredPyr(const vpImage<unsigned char> &I);
293  void initPosEvalRMS(const vpColVector &p);
294  virtual void initPyramidal(unsigned int nbLvl, unsigned int l0);
295  void initTracking(const vpImage<unsigned char> &I, vpTemplateTrackerZone &zone);
296  virtual void initTrackingPyr(const vpImage<unsigned char> &I, vpTemplateTrackerZone &zone);
297  virtual void trackNoPyr(const vpImage<unsigned char> &I) = 0;
298  virtual void trackPyr(const vpImage<unsigned char> &I);
299 };
300 #endif
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
static const vpColor green
Definition: vpColor.h:214
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:6493
vpColVector getG() const
vpImage< double > dIx
unsigned int nbIterBrent
void setThresholdGradient(double threshold)
unsigned int getNbIteration() const
void setCostFunctionVerification(bool b)
void setThresholdResidualDifference(double threshold)
vpImage< double > dIy
vpTemplateTracker()
Default constructor.
unsigned int templateSelectSize
vpTemplateTrackerPoint ** ptTemplatePyr
void setUseBrent(bool b)
unsigned int nbLvlPyr
virtual void trackNoPyr(const vpImage< unsigned char > &I)=0
unsigned int * templateSizePyr
void setIterationMax(const unsigned int &n)
vpTemplateTrackerZone * zoneTrackedPyr
vpTemplateTrackerZone zoneRef_
void setPyramidal(unsigned int nlevels=2, unsigned int level_to_stop=1)
vpImage< unsigned char > * pyr_IDes
std::vector< double > y_pos
unsigned int getNbParam() const
vpMatrix * HLMdesireInversePyr
vpMatrix getH() const
std::vector< double > x_pos
unsigned int iterationMax
virtual double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)=0
vpColVector getp() const
vpTemplateTrackerPointCompo ** ptTemplateCompoPyr
void getGaussianBluredImage(const vpImage< unsigned char > &I)
void setp(const vpColVector &tp)
virtual void initHessienDesired(const vpImage< unsigned char > &I)=0
unsigned int nbIteration
vpTemplateTrackerPoint * ptTemplate
void setNbIterBrent(const unsigned int &b)
vp_deprecated void setThresholdRMS(double threshold)
void setLambda(double l)
vpTemplateTrackerWarp * Warp
vpTemplateTrackerZone getZoneRef() const
double getRatioPixelIn() const
unsigned int iterationGlobale
bool getDiverge() const
void setSampling(int sample_i, int sample_j)
vpTemplateTrackerWarp * getWarp() const
vpImage< double > BI
void setGain(double g)
void setHDes(vpMatrix &tH)
unsigned int templateSize
vpTemplateTrackerPointCompo * ptTemplateCompo
vpTemplateTrackerZone * zoneTracked