Visual Servoing Platform  version 3.2.1 under development (2019-10-23) under development (2019-10-23)
vpTemplateTracker.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  * Template tracker.
33  *
34  * Authors:
35  * Amaury Dame
36  * Aurelien Yol
37  * Fabien Spindler
38  *
39  *****************************************************************************/
45 #ifndef vpTemplateTracker_hh
46 #define vpTemplateTracker_hh
47 
48 #include <math.h>
49 
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 
62 class VISP_EXPORT vpTemplateTracker
63 {
64 protected:
65  // traitement pyramidal
66  unsigned int nbLvlPyr; // If = 1, disable pyramidal usage
67  unsigned int l0Pyr;
69  // For evolRMS computation
70  double evolRMS;
71  std::vector<double> x_pos;
72  std::vector<double> y_pos;
73  double evolRMS_eps;
74 
78  unsigned int templateSize;
79  unsigned int *templateSizePyr;
83  unsigned int templateSelectSize;
84 
85 #ifndef DOXYGEN_SHOULD_SKIP_THIS
86  vpTemplateTrackerPointSuppMIInv *ptTemplateSupp; // pour inverse et compo
87  vpTemplateTrackerPointSuppMIInv **ptTemplateSuppPyr; // pour inverse et compo
88 #endif
89 
94 
96 
106 
107  double gain;
110  bool blur;
111  bool useBrent;
112  unsigned int nbIterBrent;
113  unsigned int taillef;
114  double *fgG;
115  double *fgdG;
116  double ratioPixelIn;
117  int mod_i;
118  int mod_j; // variable de sampling de zone de reference
119  unsigned int nbParam;
120  double lambdaDep;
121  unsigned int iterationMax;
122  // pour BFGS
123  unsigned int iterationGlobale;
124  // diverge is set to true if there is no more point in the tracked area
125  bool diverge;
126  unsigned int nbIteration;
129 
131  // Parametre de deplacement
134 
135  // temporary values for warping
138  // temporary derivative matrix
140 
144  vpTemplateTrackerZone zoneRef_; // Reference zone
145 
146 public:
149  : nbLvlPyr(0), l0Pyr(0), pyrInitialised(false), ptTemplate(NULL), ptTemplatePyr(NULL), ptTemplateInit(false),
150  templateSize(0), templateSizePyr(NULL), ptTemplateSelect(NULL), ptTemplateSelectPyr(NULL),
151  ptTemplateSelectInit(false), templateSelectSize(0), ptTemplateSupp(NULL), ptTemplateSuppPyr(NULL),
152  ptTemplateCompo(NULL), ptTemplateCompoPyr(NULL), zoneTracked(NULL), zoneTrackedPyr(NULL), pyr_IDes(NULL), H(),
153  Hdesire(), HdesirePyr(NULL), HLM(), HLMdesire(), HLMdesirePyr(NULL), HLMdesireInverse(),
154  HLMdesireInversePyr(NULL), G(), gain(0), thresholdGradient(0), costFunctionVerification(false), blur(false),
155  useBrent(false), nbIterBrent(0), taillef(0), fgG(NULL), fgdG(NULL), ratioPixelIn(0), mod_i(0), mod_j(0),
156  nbParam(), lambdaDep(0), iterationMax(0), iterationGlobale(0), diverge(false), nbIteration(0),
157  useCompositionnal(false), useInverse(false), Warp(NULL), p(), dp(), X1(), X2(), dW(), BI(), dIx(), dIy(),
158  zoneRef_()
159  {
160  }
161  explicit vpTemplateTracker(vpTemplateTrackerWarp *_warp);
162  virtual ~vpTemplateTracker();
163 
164  void display(const vpImage<unsigned char> &I, const vpColor &col = vpColor::green, const unsigned int thickness = 3);
165  void display(const vpImage<vpRGBa> &I, const vpColor &col = vpColor::green, const 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  }
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)
272 
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 a matrix and operations on matrices.
Definition: vpMatrix.h:164
double getRatioPixelIn() const
void setSampling(int sample_i, int sample_j)
void setNbIterBrent(const unsigned int &b)
unsigned int templateSelectSize
vpColVector getG() const
void getGaussianBluredImage(const vpImage< unsigned char > &I)
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerPoint ** ptTemplatePyr
unsigned int getNbIteration() const
void setHDes(vpMatrix &tH)
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
void setGain(double g)
std::vector< double > y_pos
vpMatrix inverseByLU() const
void setIterationMax(const unsigned int &n)
void setUseBrent(bool b)
vpMatrix * HLMdesireInversePyr
vpTemplateTracker()
Default constructor.
vpImage< double > BI
static const vpColor green
Definition: vpColor.h:183
unsigned int templateSize
vp_deprecated void setThresholdRMS(double threshold)
vpColVector getp() const
unsigned int iterationMax
std::vector< double > x_pos
bool getDiverge() const
vpTemplateTrackerZone * zoneTrackedPyr
vpTemplateTrackerZone zoneRef_
vpImage< double > dIx
vpTemplateTrackerWarp * getWarp() const
vpImage< double > dIy
unsigned int iterationGlobale
unsigned int nbIteration
vpMatrix getH() const
unsigned int * templateSizePyr
unsigned int nbLvlPyr
vpTemplateTrackerPointCompo * ptTemplateCompo
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void setLambda(double l)
vpTemplateTrackerZone * zoneTracked
unsigned int getNbParam() const
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
vpImage< unsigned char > * pyr_IDes
void setp(const vpColVector &tp)
void setThresholdResidualDifference(double threshold)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:5453
vpTemplateTrackerZone getZoneRef() const
unsigned int nbIterBrent
vpTemplateTrackerPointCompo ** ptTemplateCompoPyr
void setPyramidal(unsigned int nlevels=2, unsigned int level_to_stop=1)
void setThresholdGradient(double threshold)
void setCostFunctionVerification(bool b)