Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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/vpConfig.h>
50 #include <visp3/core/vpImageFilter.h>
51 #include <visp3/tt/vpTemplateTrackerHeader.h>
52 #include <visp3/tt/vpTemplateTrackerWarp.h>
53 #include <visp3/tt/vpTemplateTrackerZone.h>
54 
55 BEGIN_VISP_NAMESPACE
63 class VISP_EXPORT vpTemplateTracker
64 {
65 protected:
66  // traitement pyramidal
67  unsigned int nbLvlPyr; // If = 1, disable pyramidal usage
68  unsigned int l0Pyr;
70  // For evolRMS computation
71  double evolRMS;
72  std::vector<double> x_pos;
73  std::vector<double> y_pos;
74  double evolRMS_eps;
75 
79  unsigned int templateSize;
80  unsigned int *templateSizePyr;
84  unsigned int templateSelectSize;
85 
86 #ifndef DOXYGEN_SHOULD_SKIP_THIS
87  vpTemplateTrackerPointSuppMIInv *ptTemplateSupp; // pour inverse et compo
88  vpTemplateTrackerPointSuppMIInv **ptTemplateSuppPyr; // pour inverse et compo
89 #endif
90 
95 
97 
107 
108  double gain;
111  bool blur;
112  bool useBrent;
113  unsigned int nbIterBrent;
114  unsigned int taillef;
115  double *fgG;
116  double *fgdG;
117  double ratioPixelIn;
118  int mod_i;
119  int mod_j; // variable de sampling de zone de reference
120  unsigned int nbParam;
121  double lambdaDep;
122  unsigned int iterationMax;
123  // pour BFGS
124  unsigned int iterationGlobale;
125  // diverge is set to true if there is no more point in the tracked area
126  bool diverge;
127  unsigned int nbIteration;
130 
132  // Parametre de deplacement
135 
136  // temporary values for warping
139  // temporary derivative matrix
141 
145  vpTemplateTrackerZone zoneRef_; // Reference zone
146 
147 public:
150  : nbLvlPyr(0), l0Pyr(0), pyrInitialised(false), ptTemplate(nullptr), ptTemplatePyr(nullptr), ptTemplateInit(false),
151  templateSize(0), templateSizePyr(nullptr), ptTemplateSelect(nullptr), ptTemplateSelectPyr(nullptr),
152  ptTemplateSelectInit(false), templateSelectSize(0), ptTemplateSupp(nullptr), ptTemplateSuppPyr(nullptr),
153  ptTemplateCompo(nullptr), ptTemplateCompoPyr(nullptr), zoneTracked(nullptr), zoneTrackedPyr(nullptr), pyr_IDes(nullptr), H(),
154  Hdesire(), HdesirePyr(nullptr), HLM(), HLMdesire(), HLMdesirePyr(nullptr), HLMdesireInverse(),
155  HLMdesireInversePyr(nullptr), G(), gain(0), thresholdGradient(0), costFunctionVerification(false), blur(false),
156  useBrent(false), nbIterBrent(0), taillef(0), fgG(nullptr), fgdG(nullptr), ratioPixelIn(0), mod_i(0), mod_j(0),
157  nbParam(), lambdaDep(0), iterationMax(0), iterationGlobale(0), diverge(false), nbIteration(0),
158  useCompositionnal(false), useInverse(false), Warp(nullptr), p(), dp(), X1(), X2(), dW(), BI(), dIx(), dIy(),
159  zoneRef_()
160  { }
161  VP_EXPLICIT vpTemplateTracker(vpTemplateTrackerWarp *_warp);
162  virtual ~vpTemplateTracker();
163 
164  void display(const vpImage<unsigned char> &I, const vpColor &col = vpColor::green, unsigned int thickness = 3);
165  void display(const vpImage<vpRGBa> &I, const vpColor &col = vpColor::green, unsigned int thickness = 3);
166 
167  bool getDiverge() const { return diverge; }
168  vpColVector getdp() { return dp; }
169  vpColVector getG() const { return G; }
170  vpMatrix getH() const { return H; }
171  unsigned int getNbParam() const { return nbParam; }
172  unsigned int getNbIteration() const { return nbIteration; }
173  vpColVector getp() const { return p; }
174  double getRatioPixelIn() const { return ratioPixelIn; }
175 
180  vpTemplateTrackerWarp *getWarp() const { return Warp; }
181 
185  vpTemplateTrackerZone getZoneRef() const { return zoneRef_; }
186 
187  void initClick(const vpImage<unsigned char> &I, bool delaunay = false);
188  void initFromPoints(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &v_ip, bool delaunay = false);
189  void initFromZone(const vpImage<unsigned char> &I, const vpTemplateTrackerZone &zone);
190 
191  void resetTracker();
192 
193  void setBlur(bool b) { blur = b; }
194  void setCostFunctionVerification(bool b) { costFunctionVerification = b; }
195  void setGain(double g) { gain = g; }
196  void setGaussianFilterSize(unsigned int new_taill);
197  void setHDes(vpMatrix &tH)
198  {
199  Hdesire = tH;
200  vpMatrix::computeHLM(Hdesire, lambdaDep, HLMdesire);
201  HLMdesireInverse = HLMdesire.inverseByLU();
202  }
208  void setIterationMax(const unsigned int &n) { iterationMax = n; }
213  void setLambda(double l) { lambdaDep = l; }
214  void setNbIterBrent(const unsigned int &b) { nbIterBrent = b; }
215  void setp(const vpColVector &tp)
216  {
217  p = tp;
218  diverge = false;
219  iterationGlobale = 0;
220  }
230  void setPyramidal(unsigned int nlevels = 2, unsigned int level_to_stop = 1)
231  {
232  nbLvlPyr = nlevels;
233  l0Pyr = level_to_stop;
234  if (l0Pyr >= nlevels) {
235  std::cout << "Warning: level_to_stop: " << level_to_stop << " higher than level_to_start: " << nlevels - 1
236  << " (nlevels-1)" << std::endl;
237  std::cout << "Level to stop put to: " << nlevels - 1 << std::endl;
238  l0Pyr = nlevels - 1;
239  }
240  }
249  void setSampling(int sample_i, int sample_j)
250  {
251  mod_i = sample_i;
252  mod_j = sample_j;
253  }
254  void setThresholdGradient(double threshold) { thresholdGradient = threshold; }
264  void setThresholdResidualDifference(double threshold) { evolRMS_eps = threshold; }
265 
267  void setUseBrent(bool b) { useBrent = b; }
268 
269  void track(const vpImage<unsigned char> &I);
270  void trackRobust(const vpImage<unsigned char> &I);
271 
272 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
282  VP_DEPRECATED void setThresholdRMS(double threshold) { (void)threshold; }
284 #endif
285 
286 protected:
287  void computeEvalRMS(const vpColVector &p);
288  void computeOptimalBrentGain(const vpImage<unsigned char> &I, vpColVector &tp, double tMI, vpColVector &direction,
289  double &alpha);
290  virtual double getCost(const vpImage<unsigned char> &I, const vpColVector &tp) = 0;
291  void getGaussianBluredImage(const vpImage<unsigned char> &I) { vpImageFilter::filter(I, BI, fgG, taillef); }
292  virtual void initHessienDesired(const vpImage<unsigned char> &I) = 0;
293  virtual void initHessienDesiredPyr(const vpImage<unsigned char> &I);
294  void initPosEvalRMS(const vpColVector &p);
295  virtual void initPyramidal(unsigned int nbLvl, unsigned int l0);
296  void initTracking(const vpImage<unsigned char> &I, vpTemplateTrackerZone &zone);
297  virtual void initTrackingPyr(const vpImage<unsigned char> &I, vpTemplateTrackerZone &zone);
298  virtual void trackNoPyr(const vpImage<unsigned char> &I) = 0;
299  virtual void trackPyr(const vpImage<unsigned char> &I);
300 };
301 END_VISP_NAMESPACE
302 #endif
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
static const vpColor green
Definition: vpColor.h:220
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:169
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:1873
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)
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