Visual Servoing Platform  version 3.0.0
vpTemplateTracker.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Template tracker.
32  *
33  * Authors:
34  * Amaury Dame
35  * Aurelien Yol
36  * Fabien Spindler
37  *
38  *****************************************************************************/
44 #ifndef vpTemplateTracker_hh
45 #define vpTemplateTracker_hh
46 
47 #include <math.h>
48 
49 #include <visp3/tt/vpTemplateTrackerHeader.h>
50 #include <visp3/tt/vpTemplateTrackerZone.h>
51 #include <visp3/tt/vpTemplateTrackerWarp.h>
52 #include <visp3/core/vpImageFilter.h>
53 
58 class VISP_EXPORT vpTemplateTracker
59 {
60  protected:
61  //traitement pyramidal
62  unsigned int nbLvlPyr; // If = 1, disable pyramidal usage
63  unsigned int l0Pyr;
65 
69  unsigned int templateSize;
70  unsigned int *templateSizePyr;
74  unsigned int templateSelectSize;
75 
76  #ifndef DOXYGEN_SHOULD_SKIP_THIS
77  vpTemplateTrackerPointSuppMIInv *ptTemplateSupp; //pour inverse et compo
78  vpTemplateTrackerPointSuppMIInv **ptTemplateSuppPyr; //pour inverse et compo
79  #endif
80 
85 
87 
97 
98  double gain;
101  bool blur;
102  bool useBrent;
103  unsigned int nbIterBrent;
104  unsigned int taillef;
105  double *fgG;
106  double *fgdG;
107  double ratioPixelIn;
108  int mod_i;
109  int mod_j;//variable de sampling de zone de reference
110  unsigned int nbParam ;
111  double lambdaDep ;
112  unsigned int iterationMax ;
113  //pour BFGS
114  unsigned int iterationGlobale;
115  //diverge is set to true if there is no more point in the tracked area
116  bool diverge;
117  unsigned int nbIteration;
120 
122  //Parametre de deplacement
125 
126  //temporary values for warping
129  //temporary derivative matrix
131 
135  vpTemplateTrackerZone zoneRef_; // Reference zone
136 
137 //private:
138 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
139 // vpTemplateTracker(const vpTemplateTracker &)
140 // : nbLvlPyr(0), l0Pyr(0), pyrInitialised(false), ptTemplate(NULL), ptTemplatePyr(NULL),
141 // ptTemplateInit(false), templateSize(0), templateSizePyr(NULL), ptTemplateSelect(NULL),
142 // ptTemplateSelectPyr(NULL), ptTemplateSelectInit(false), templateSelectSize(0),
143 // ptTemplateSupp(NULL), ptTemplateSuppPyr(NULL), ptTemplateCompo(NULL), ptTemplateCompoPyr(NULL),
144 // zoneTracked(NULL), zoneTrackedPyr(NULL), pyr_IDes(NULL), H(), Hdesire(), HdesirePyr(NULL),
145 // HLM(), HLMdesire(), HLMdesirePyr(NULL), HLMdesireInverse(), HLMdesireInversePyr(NULL),
146 // G(), gain(0), thresholdGradient(0), costFunctionVerification(false),
147 // blur(false), useBrent(false), nbIterBrent(0), taillef(0), fgG(NULL), fgdG(NULL),
148 // ratioPixelIn(0), mod_i(0), mod_j(0), nbParam(), lambdaDep(0), iterationMax(0),
149 // iterationGlobale(0), diverge(false), nbIteration(0), useCompositionnal(false),
150 // useInverse(false), Warp(NULL), p(), dp(), X1(), X2(), dW(), BI(), dIx(), dIy(), zoneRef_()
151 // {
152 // throw vpException(vpException::functionNotImplementedError, "Not implemented!");
153 // }
154 // vpTemplateTracker &operator=(const vpTemplateTracker &){
155 // throw vpException(vpException::functionNotImplementedError, "Not implemented!");
156 // return *this;
157 // }
158 //#endif
159 
160  public:
163  : nbLvlPyr(0), l0Pyr(0), pyrInitialised(false), ptTemplate(NULL), ptTemplatePyr(NULL),
164  ptTemplateInit(false), templateSize(0), templateSizePyr(NULL), ptTemplateSelect(NULL),
165  ptTemplateSelectPyr(NULL), ptTemplateSelectInit(false), templateSelectSize(0),
166  ptTemplateSupp(NULL), ptTemplateSuppPyr(NULL), ptTemplateCompo(NULL), ptTemplateCompoPyr(NULL),
167  zoneTracked(NULL), zoneTrackedPyr(NULL), pyr_IDes(NULL), H(), Hdesire(), HdesirePyr(NULL),
168  HLM(), HLMdesire(), HLMdesirePyr(NULL), HLMdesireInverse(), HLMdesireInversePyr(NULL),
169  G(), gain(0), thresholdGradient(0), costFunctionVerification(false),
170  blur(false), useBrent(false), nbIterBrent(0), taillef(0), fgG(NULL), fgdG(NULL),
171  ratioPixelIn(0), mod_i(0), mod_j(0), nbParam(), lambdaDep(0), iterationMax(0),
172  iterationGlobale(0), diverge(false), nbIteration(0), useCompositionnal(false),
173  useInverse(false), Warp(NULL), p(), dp(), X1(), X2(), dW(), BI(), dIx(), dIy(), zoneRef_()
174  {}
176  virtual ~vpTemplateTracker();
177 
178  void display(const vpImage<unsigned char> &I, const vpColor& col = vpColor::green, const unsigned int thickness=3);
179  void display(const vpImage<vpRGBa> &I, const vpColor& col = vpColor::green, const unsigned int thickness=3);
180 
181  bool getDiverge() const {return diverge;}
182  vpColVector getdp(){ return dp; }
183  vpColVector getG() const { return G; }
184  vpMatrix getH() const { return H; }
185  unsigned int getNbParam() const { return nbParam ; }
186  unsigned int getNbIteration() const { return nbIteration; }
187  vpColVector getp() const { return p;}
188  double getRatioPixelIn() const {return ratioPixelIn;}
189 
194  vpTemplateTrackerWarp *getWarp() const {return Warp;}
195 
199  vpTemplateTrackerZone getZoneRef() const { return zoneRef_; }
200 
201  void initClick(const vpImage<unsigned char> &I, bool delaunay=false);
202  void initFromPoints(const vpImage<unsigned char> &I, const std::vector< vpImagePoint > &v_ip, bool delaunay=false);
203  void initFromZone(const vpImage<unsigned char> &I, const vpTemplateTrackerZone& zone);
204 
205  void resetTracker();
206 
207  void setBlur(bool b){blur = b;}
208  void setCostFunctionVerification(bool b){costFunctionVerification = b;}
209  void setGain(double g){gain=g;}
210  void setGaussianFilterSize(unsigned int new_taill);
211  void setHDes(vpMatrix &tH){ Hdesire=tH; vpMatrix::computeHLM(Hdesire,lambdaDep,HLMdesire); HLMdesireInverse = HLMdesire.inverseByLU();}
216  void setIterationMax(const unsigned int &n) { iterationMax = n ; }
221  void setLambda(double l) { lambdaDep = l ; }
222  void setNbIterBrent(const unsigned int &b){nbIterBrent=b;}
223  void setp(const vpColVector &tp){ p=tp; diverge=false; iterationGlobale=0; }
231  void setPyramidal(unsigned int nlevels=2, unsigned int level_to_stop=1) {
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 << " (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){mod_i=sample_i; mod_j=sample_j;}
249  void setThresholdGradient(double threshold){thresholdGradient=threshold;}
251  void setUseBrent(bool b){useBrent = b;}
252 
253  void track(const vpImage<unsigned char> &I);
254  void trackRobust(const vpImage<unsigned char> &I);
255 
256  protected:
257 
258  void computeOptimalBrentGain(const vpImage<unsigned char> &I,vpColVector &tp,double tMI,vpColVector &direction,double &alpha);
259  virtual double getCost(const vpImage<unsigned char> &I, const vpColVector &tp) = 0;
261  void initCompInverse(const vpImage<unsigned char> &I);
262  virtual void initCompInversePyr(const vpImage<unsigned char> &I);
263  virtual void initHessienDesired(const vpImage<unsigned char> &I)=0;
264  virtual void initHessienDesiredPyr(const vpImage<unsigned char> &I);
265  virtual void initPyramidal(unsigned int nbLvl,unsigned int l0);
266  void initTracking(const vpImage<unsigned char>& I,vpTemplateTrackerZone &zone);
267  virtual void initTrackingPyr(const vpImage<unsigned char>& I,vpTemplateTrackerZone &zone);
268  virtual void trackNoPyr(const vpImage<unsigned char> &I) = 0;
269  virtual void trackPyr(const vpImage<unsigned char> &I);
270 };
271 #endif
272 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
vpColVector getG() const
void setSampling(int sample_i, int sample_j)
void setNbIterBrent(const unsigned int &b)
vpMatrix getH() const
unsigned int templateSelectSize
void getGaussianBluredImage(const vpImage< unsigned char > &I)
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerPoint ** ptTemplatePyr
void setHDes(vpMatrix &tH)
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void setGain(double g)
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:166
vpColVector getp() const
unsigned int templateSize
unsigned int iterationMax
vpTemplateTrackerZone * zoneTrackedPyr
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
vpTemplateTrackerZone zoneRef_
vpImage< double > dIx
unsigned int getNbParam() const
vpImage< double > dIy
unsigned int getNbIteration() const
unsigned int iterationGlobale
unsigned int nbIteration
double getRatioPixelIn() const
unsigned int * templateSizePyr
unsigned int nbLvlPyr
vpTemplateTrackerPointCompo * ptTemplateCompo
bool getDiverge() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setLambda(double l)
vpMatrix inverseByLU() const
vpTemplateTrackerZone * zoneTracked
vpTemplateTrackerZone getZoneRef() const
vpImage< unsigned char > * pyr_IDes
void setp(const vpColVector &tp)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:3470
unsigned int nbIterBrent
vpTemplateTrackerPointCompo ** ptTemplateCompoPyr
void setPyramidal(unsigned int nlevels=2, unsigned int level_to_stop=1)
vpTemplateTrackerWarp * getWarp() const
void setThresholdGradient(double threshold)
void setCostFunctionVerification(bool b)