ViSP  2.9.0
vpTemplateTrackerSSDForwardAdditional.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerSSDForwardAdditional.cpp 4661 2014-02-10 19:34:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Template tracker.
35  *
36  * Authors:
37  * Amaury Dame
38  * Aurelien Yol
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 #include <limits> // numeric_limits
44 
45 #include <visp/vpTemplateTrackerSSDForwardAdditional.h>
46 #include <visp/vpImageTools.h>
47 
49  : vpTemplateTrackerSSD(warp), minimizationMethod(USE_NEWTON), p_prec(), G_prec(), KQuasiNewton()
50 {
51  useCompositionnal=false;
52 }
53 
55 {
56  double erreur=0;
57  int Nbpoint=0;
58 
59  if(blur)
63 
64  dW=0;
65 
66  double lambda=lambdaDep;
67  double IW,dIWx,dIWy;
68  double Tij;
69  unsigned int iteration=0;
70  int i,j;
71  double i2,j2;
72  double alpha=2.;
73  do
74  {
75  Nbpoint=0;
76  erreur=0;
77  G=0;
78  H=0 ;
79  Warp->computeCoeff(p);
80  for(unsigned int point=0;point<templateSize;point++)
81  {
82  i=ptTemplate[point].y;
83  j=ptTemplate[point].x;
84  X1[0]=j;X1[1]=i;
85 
86  Warp->computeDenom(X1,p);
87  Warp->warpX(X1,X2,p);
88 
89  j2=X2[0];i2=X2[1];
90  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
91  {
92  Tij=ptTemplate[point].val;
93 
94  if(!blur)
95  IW=I.getValue(i2,j2);
96  else
97  IW=BI.getValue(i2,j2);
98 
99  dIWx=dIx.getValue(i2,j2);
100  dIWy=dIy.getValue(i2,j2);
101  Nbpoint++;
102  //Calcul du Hessien
103  Warp->dWarp(X1,X2,p,dW);
104  double *tempt=new double[nbParam];
105  for(unsigned int it=0;it<nbParam;it++)
106  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
107 
108  for(unsigned int it=0;it<nbParam;it++)
109  for(unsigned int jt=0;jt<nbParam;jt++)
110  H[it][jt]+=tempt[it]*tempt[jt];
111 
112  double er=(Tij-IW);
113  for(unsigned int it=0;it<nbParam;it++)
114  G[it]+=er*tempt[it];
115 
116  erreur+=(er*er);
117  delete[] tempt;
118  }
119 
120 
121  }
122  if(Nbpoint==0)std::cout<<"plus de point dans template suivi"<<std::endl;
123 
124  vpMatrix::computeHLM(H,lambda,HLM);
125  try
126  {
127  dp=1.*HLM.inverseByLU()*G;
128  }
129  catch(...)
130  {
131  std::cout<<"probleme inversion"<<std::endl;
132  break;
133  }
134 
135  switch(minimizationMethod)
136  {
138  {
139  vpColVector p_test_LMA(nbParam);
140  p_test_LMA=p+1.*dp;
141  erreur=-getCost(I,p);
142  double erreur_LMA=-getCost(I,p_test_LMA);
143  if(erreur_LMA<erreur)
144  {
145  p=p_test_LMA;
146  lambda=(lambda/10.<1e-6)?lambda/10.:1e-6;
147  }
148  else
149  {
150  lambda=(lambda*10.<1e6)?1e6:lambda*10.;
151  }
152  }
153  break;
155  {
156  dp=gain*0.000001*G;
157  if(useBrent)
158  {
159  alpha=2.;
160  computeOptimalBrentGain(I,p,erreur,dp,alpha);
161  dp=alpha*dp;
162  }
163  p+=1.*dp;
164  break;
165  }
166 
168  {
169  double s_scal_y;
170  if(iterationGlobale!=0)
171  {
172  vpColVector s_quasi=p-p_prec;
173  vpColVector y_quasi=G-G_prec;
174  s_scal_y=s_quasi.t()*y_quasi;
175  //if(s_scal_y!=0)//BFGS
176  // KQuasiNewton=KQuasiNewton-(s_quasi*y_quasi.t()*KQuasiNewton+KQuasiNewton*y_quasi*s_quasi.t())/s_scal_y+(1.+y_quasi.t()*(KQuasiNewton*y_quasi)/s_scal_y)*s_quasi*s_quasi.t()/s_scal_y;
177  //if(s_scal_y!=0.0)//DFP
178  if(std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) //DFP
179  KQuasiNewton=KQuasiNewton+0.001*(s_quasi*s_quasi.t()/s_scal_y-KQuasiNewton*y_quasi*y_quasi.t()*KQuasiNewton/(y_quasi.t()*KQuasiNewton*y_quasi));
180  }
181  dp=-KQuasiNewton*G;
182  p_prec=p;
183  G_prec=G;
184  p-=1.01*dp;
185  }
186  break;
187 
189  default:
190  {
191  if(useBrent)
192  {
193  alpha=2.;
194  computeOptimalBrentGain(I,p,erreur,dp,alpha);
195  dp=alpha*dp;
196  }
197 
198  p+=1.*dp;
199  break;
200  }
201  }
202 
203  iteration++;
205  }
206  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
207 
208  //std::cout<<"erreur "<<erreur<<std::endl;
209  nbIteration=iteration;
210 }
211 
void trackNoPyr(const vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:159
vpTemplateTrackerPoint * ptTemplate
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
Type getValue(double i, double j) const
Definition: vpImage.h:1029
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
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
vpImage< double > dIx
vpRowVector t() const
transpose of Vector
vpImage< double > dIy
unsigned int iterationGlobale
double getCost(const vpImage< unsigned char > &I, vpColVector &tp)
unsigned int nbIteration
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
unsigned int getHeight() const
Definition: vpImage.h:150
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:4051
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0