ViSP  2.10.0
vpTemplateTrackerSSDForwardAdditional.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerSSDForwardAdditional.cpp 5264 2015-02-04 13:49:55Z 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  unsigned 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) {
123  //std::cout<<"plus de point dans template suivi"<<std::endl;
124  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
125  }
126 
127  vpMatrix::computeHLM(H,lambda,HLM);
128  try
129  {
130  dp=1.*HLM.inverseByLU()*G;
131  }
132  catch(vpException &e)
133  {
134  throw(e);
135  }
136 
137  switch(minimizationMethod)
138  {
140  {
141  vpColVector p_test_LMA(nbParam);
142  p_test_LMA=p+1.*dp;
143  erreur=-getCost(I,p);
144  double erreur_LMA=-getCost(I,p_test_LMA);
145  if(erreur_LMA<erreur)
146  {
147  p=p_test_LMA;
148  lambda=(lambda/10.<1e-6)?lambda/10.:1e-6;
149  }
150  else
151  {
152  lambda=(lambda*10.<1e6)?1e6:lambda*10.;
153  }
154  }
155  break;
157  {
158  dp=gain*0.000001*G;
159  if(useBrent)
160  {
161  alpha=2.;
162  computeOptimalBrentGain(I,p,erreur,dp,alpha);
163  dp=alpha*dp;
164  }
165  p+=1.*dp;
166  break;
167  }
168 
170  {
171  double s_scal_y;
172  if(iterationGlobale!=0)
173  {
174  vpColVector s_quasi=p-p_prec;
175  vpColVector y_quasi=G-G_prec;
176  s_scal_y=s_quasi.t()*y_quasi;
177  //if(s_scal_y!=0)//BFGS
178  // 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;
179  //if(s_scal_y!=0.0)//DFP
180  if(std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) //DFP
181  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));
182  }
183  dp=-KQuasiNewton*G;
184  p_prec=p;
185  G_prec=G;
186  p-=1.01*dp;
187  }
188  break;
189 
191  default:
192  {
193  if(useBrent)
194  {
195  alpha=2.;
196  computeOptimalBrentGain(I,p,erreur,dp,alpha);
197  dp=alpha*dp;
198  }
199 
200  p+=1.*dp;
201  break;
202  }
203  }
204 
205  iteration++;
207  }
208  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
209 
210  //std::cout<<"erreur "<<erreur<<std::endl;
211  nbIteration=iteration;
212 }
213 
void trackNoPyr(const vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:161
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)
error that can be emited by ViSP classes.
Definition: vpException.h:76
Type getValue(double i, double j) const
Definition: vpImage.h:1068
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
Error that can be emited by the vpTracker class and its derivates.
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
vpImage< double > dIx
vpRowVector t() const
Transpose of a 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:152
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:4320
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0