Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTemplateTrackerSSDESM.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  *****************************************************************************/
39 #include <visp3/tt/vpTemplateTrackerSSDESM.h>
40 #include <visp3/core/vpImageFilter.h>
41 
43  : vpTemplateTrackerSSD(warp), compoInitialised(false), HDir(), HInv(),
44  HLMDir(), HLMInv(), GDir(), GInv()
45 {
46  useCompositionnal=false;
47  useInverse=false;
48 
49  if(!Warp->isESMcompatible())
50  std::cerr<<"The selected warp function is not appropriate for the ESM algorithm..."<<std::endl;
51 
58 }
59 
61 {
62  initCompInverse(I);
63 }
64 
66 {
67  //std::cout<<"Initialise precomputed value of ESM with templateSize: "<< templateSize<<std::endl;
69  int i,j;
70  //direct
71  for(unsigned int point=0;point<templateSize;point++)
72  {
73  i=ptTemplate[point].y;
74  j=ptTemplate[point].x;
75  ptTemplateCompo[point].dW=new double[2*nbParam];
76  X1[0]=j;X1[1]=i;
77  Warp->computeDenom(X1,p);
78  Warp->getdWdp0(i,j,ptTemplateCompo[point].dW);
79 
80  }
81 
82  //inverse
83  HInv=0;
84  for(unsigned int point=0;point<templateSize;point++)
85  {
86  i=ptTemplate[point].y;
87  j=ptTemplate[point].x;
88 
89  X1[0]=j;X1[1]=i;
90  Warp->computeDenom(X1,p);
91  ptTemplate[point].dW=new double[nbParam];
92  Warp->getdW0(i,j,ptTemplate[point].dy,ptTemplate[point].dx,ptTemplate[point].dW);
93 
94  for(unsigned int it=0;it<nbParam;it++)
95  for(unsigned int jt=0;jt<nbParam;jt++)
96  HInv[it][jt]+=ptTemplate[point].dW[it]*ptTemplate[point].dW[jt];
97  }
99 
100 compoInitialised=true;
101 }
102 
104 {
105  if(blur)
109 
110  double IW,dIWx,dIWy;
111  double Tij;
112  unsigned int iteration=0;
113  int i,j;
114  double i2,j2;
115  double alpha=2.;
116  do
117  {
118  unsigned int Nbpoint=0;
119  double erreur=0;
120  dp=0;
121  HDir=0;
122  GDir=0;
123  GInv=0;
124  Warp->computeCoeff(p);
125  for(unsigned int point=0;point<templateSize;point++)
126  {
127  i=ptTemplate[point].y;
128  j=ptTemplate[point].x;
129  X1[0]=j;X1[1]=i;
130 
131  Warp->computeDenom(X1,p);
132  Warp->warpX(X1,X2,p);
133 
134  j2=X2[0];i2=X2[1];
135  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
136  {
137  //INVERSE
138  Tij=ptTemplate[point].val;
139  if(!blur)
140  IW=I.getValue(i2,j2);
141  else
142  IW=BI.getValue(i2,j2);
143  Nbpoint++;
144  double er=(Tij-IW);
145  for(unsigned int it=0;it<nbParam;it++)
146  GInv[it]+=er*ptTemplate[point].dW[it];
147 
148  erreur+=er*er;
149 
150  //DIRECT
151  //dIWx=dIx.getValue(i2,j2);
152  //dIWy=dIy.getValue(i2,j2);
153 
154  dIWx=dIx.getValue(i2,j2)+ptTemplate[point].dx;
155  dIWy=dIy.getValue(i2,j2)+ptTemplate[point].dy;
156 
157  //Calcul du Hessien
158  //Warp->dWarp(X1,X2,p,dW);
159  Warp->dWarpCompo(X1,X2,p,ptTemplateCompo[point].dW,dW);
160 
161  double *tempt=new double[nbParam];
162  for(unsigned int it=0;it<nbParam;it++)
163  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
164 
165  for(unsigned int it=0;it<nbParam;it++)
166  for(unsigned int jt=0;jt<nbParam;jt++)
167  HDir[it][jt]+=tempt[it]*tempt[jt];
168 
169  for(unsigned int it=0;it<nbParam;it++)
170  GDir[it]+=er*tempt[it];
171  delete[] tempt;
172  }
173 
174 
175  }
176  if(Nbpoint==0) {
177  //std::cout<<"plus de point dans template suivi"<<std::endl;
178  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
179  }
180 
182 
183  try
184  {
185  //dp=(HLMInv+HLMDir).inverseByLU()*(GInv+GDir);
186  //dp=HLMInv.inverseByLU()*GInv+HLMDir.inverseByLU()*GDir;
187  //dp=HLMInv.inverseByLU()*GInv;
188  dp=(HLMDir).inverseByLU()*(GDir);
189  }
190  catch(vpException &e)
191  {
192  //std::cout<<"probleme inversion"<<std::endl;
193  throw(e);
194  }
195 
196  dp=gain*dp;
197  if(useBrent)
198  {
199  alpha=2.;
200  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
201  dp=alpha*dp;
202  }
203 
204  //Warp->pRondp(p,dp,p);
205  p+=dp;
206  iteration++;
207 
208  }
209  while( (iteration < iterationMax));
210 
211  nbIteration=iteration;
212 }
213 
214 
215 /*void vpTemplateTrackerSSDESM::InitCompInverse(vpImage<unsigned char> &I)
216 {
217  ptTempateCompo=new vpTemplateTrackerPointCompo[taille_template];
218  int i,j;
219  for(int point=0;point<taille_template;point++)
220  {
221  i=ptTemplate[point].y;
222  j=ptTemplate[point].x;
223  ptTempateCompo[point].dW=new double[2*nbParam];
224  Warp->getdWdp0(i,j,ptTempateCompo[point].dW);
225  }
226 
227 }
228 
229 void vpTemplateTrackerSSDESM::track(vpImage<unsigned char> &I)
230 {
231  double erreur=0,erreur_prec=1e38;
232  int Nbpoint=0;
233 
234  int taillefiltre=taille_filtre_dgaussien;
235  double *fg=new double[taillefiltre+1] ;
236  getGaussianDerivativeKernel(fg, taillefiltre) ;
237  getGradX(I, dIx,fg,taillefiltre);
238  getGradY(I, dIy,fg,taillefiltre);
239  delete[] fg;
240 
241  vpColVector dpinv(nbParam);
242  double lambda=lambdaDep;
243  double IW,dIWx,dIWy;
244  double Tij;
245  int iteration=0;
246  int i,j;
247  double i2,j2;
248  vpTemplateTrackerPoint *pt;
249  do
250  {
251  Nbpoint=0;
252  erreur_prec=erreur;
253  erreur=0;
254  dp=0;
255  HDir=0;
256  GDir=0;
257  GInv=0;
258  Warp->ComputeCoeff(p);
259  for(int point=0;point<taille_template;point++)
260  {
261  pt=&ptTemplate[point];
262  i=pt->y;
263  j=pt->x;
264  X1[0]=j;X1[1]=i;
265 
266  Warp->ComputeDenom(X1,p);
267  Warp->warpX(X1,X2,p);
268 
269  j2=X2[0];i2=X2[1];
270  if((j2<I.getWidth())&&(i2<I.getHeight())&&(i2>0)&&(j2>0))
271  {
272  //INVERSE
273  Tij=pt->val;
274  IW=I.getPixelBI(j2,i2);
275  Nbpoint++;
276  double er=(Tij-IW);
277  for(int it=0;it<nbParam;it++)
278  GInv[it]+=er*ptTemplate[point].dW[it];
279 
280  erreur+=er*er;
281 
282  //DIRECT COMPO
283  Tij=ptTemplate[point].val;
284  IW=I.getPixelBI(j2,i2);
285  dIWx=dIx.getPixelBI(j2,i2);
286  dIWy=dIy.getPixelBI(j2,i2);
287  Nbpoint++;
288  Warp->dWarpCompo(X1,X2,p,ptTempateCompo[point].dW,dW);
289  double *tempt=new double[nbParam];
290  for(int it=0;it<nbParam;it++)
291  tempt[it] =dW[0][it]*dIWx+dW[1][it]*dIWy;
292 
293  for(int it=0;it<nbParam;it++)
294  for(int jt=0;jt<nbParam;jt++)
295  HDir[it][jt]+=tempt[it]*tempt[jt];
296 
297  for(int it=0;it<nbParam;it++)
298  GDir[it]+=er*tempt[it];
299 
300  delete[] tempt;
301  }
302 
303 
304  }
305  if(Nbpoint==0)std::cout<<"plus de point dans template suivi"<<std::endl;
306  try
307  {
308  dp=(HInv+HDir).inverseByLU()*(GInv+GDir);
309  }
310  catch(...)
311  {
312  std::cout<<"probleme inversion"<<std::endl;
313  break;
314  }
315 
316  if(Compare)write_infos(p,erreur/Nbpoint);
317 
318  p+=Gain*dp;
319  iteration++;
320 
321  }
322  while( (iteration < IterationMax));
323 
324  NbIteration=iteration;
325 }*/
326 
virtual void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW)=0
unsigned int getWidth() const
Definition: vpImage.h:226
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
vpTemplateTrackerPoint * ptTemplate
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
vpTemplateTrackerSSDESM(vpTemplateTrackerWarp *warp)
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
error that can be emited by ViSP classes.
Definition: vpException.h:73
Type getValue(double i, double j) const
Definition: vpImage.h:1477
static void getGradYGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIy, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
static void getGradXGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIx, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
vpImage< double > BI
unsigned int templateSize
unsigned int iterationMax
void trackNoPyr(const vpImage< unsigned char > &I)
Error that can be emited by the vpTracker class and its derivates.
void initHessienDesired(const vpImage< unsigned char > &I)
vpImage< double > dIx
vpImage< double > dIy
virtual bool isESMcompatible() const =0
virtual void getdW0(const int &i, const int &j, const double &dy, const double &dx, double *dIdW)=0
unsigned int nbIteration
vpTemplateTrackerPointCompo * ptTemplateCompo
void initCompInverse(const vpImage< unsigned char > &I)
unsigned int getHeight() const
Definition: vpImage.h:175
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
virtual void getdWdp0(const int &i, const int &j, double *dIdW)=0
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:3525
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225