Visual Servoing Platform  version 3.0.0
vpTemplateTrackerSSDForwardAdditional.cpp
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  *****************************************************************************/
39 
40 #include <limits> // numeric_limits
41 
42 #include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
43 #include <visp3/core/vpImageTools.h>
44 
46  : vpTemplateTrackerSSD(warp), minimizationMethod(USE_NEWTON), p_prec(), G_prec(), KQuasiNewton()
47 {
48  useCompositionnal=false;
49 }
50 
52 {
53  double erreur=0;
54  unsigned int Nbpoint=0;
55 
56  if(blur)
60 
61  dW=0;
62 
63  double lambda=lambdaDep;
64  double IW,dIWx,dIWy;
65  double Tij;
66  unsigned int iteration=0;
67  int i,j;
68  double i2,j2;
69  double alpha=2.;
70  do
71  {
72  Nbpoint=0;
73  erreur=0;
74  G=0;
75  H=0 ;
76  Warp->computeCoeff(p);
77  for(unsigned int point=0;point<templateSize;point++)
78  {
79  i=ptTemplate[point].y;
80  j=ptTemplate[point].x;
81  X1[0]=j;X1[1]=i;
82 
83  Warp->computeDenom(X1,p);
84  Warp->warpX(X1,X2,p);
85 
86  j2=X2[0];i2=X2[1];
87  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
88  {
89  Tij=ptTemplate[point].val;
90 
91  if(!blur)
92  IW=I.getValue(i2,j2);
93  else
94  IW=BI.getValue(i2,j2);
95 
96  dIWx=dIx.getValue(i2,j2);
97  dIWy=dIy.getValue(i2,j2);
98  Nbpoint++;
99  //Calcul du Hessien
100  Warp->dWarp(X1,X2,p,dW);
101  double *tempt=new double[nbParam];
102  for(unsigned int it=0;it<nbParam;it++)
103  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
104 
105  for(unsigned int it=0;it<nbParam;it++)
106  for(unsigned int jt=0;jt<nbParam;jt++)
107  H[it][jt]+=tempt[it]*tempt[jt];
108 
109  double er=(Tij-IW);
110  for(unsigned int it=0;it<nbParam;it++)
111  G[it]+=er*tempt[it];
112 
113  erreur+=(er*er);
114  delete[] tempt;
115  }
116 
117 
118  }
119  if(Nbpoint==0) {
120  //std::cout<<"plus de point dans template suivi"<<std::endl;
121  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
122  }
123 
124  vpMatrix::computeHLM(H,lambda,HLM);
125  try
126  {
127  dp=1.*HLM.inverseByLU()*G;
128  }
129  catch(vpException &e)
130  {
131  throw(e);
132  }
133 
134  switch(minimizationMethod)
135  {
137  {
138  vpColVector p_test_LMA(nbParam);
139  p_test_LMA=p+1.*dp;
140  erreur=-getCost(I,p);
141  double erreur_LMA=-getCost(I,p_test_LMA);
142  if(erreur_LMA<erreur)
143  {
144  p=p_test_LMA;
145  lambda=(lambda/10.<1e-6)?lambda/10.:1e-6;
146  }
147  else
148  {
149  lambda=(lambda*10.<1e6)?1e6:lambda*10.;
150  }
151  }
152  break;
154  {
155  dp=gain*0.000001*G;
156  if(useBrent)
157  {
158  alpha=2.;
159  computeOptimalBrentGain(I,p,erreur,dp,alpha);
160  dp=alpha*dp;
161  }
162  p += 1. * dp;
163  break;
164  }
165 
167  {
168  double s_scal_y;
169  if(iterationGlobale!=0)
170  {
171  vpColVector s_quasi=p-p_prec;
172  vpColVector y_quasi=G-G_prec;
173  s_scal_y=s_quasi.t()*y_quasi;
174  //if(s_scal_y!=0)//BFGS
175  // 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;
176  //if(s_scal_y!=0.0)//DFP
177  if(std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) //DFP
178  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));
179  }
180  dp=-KQuasiNewton*G;
181  p_prec=p;
182  G_prec=G;
183  p-=1.01*dp;
184  }
185  break;
186 
188  default:
189  {
190  if(useBrent)
191  {
192  alpha=2.;
193  computeOptimalBrentGain(I,p,erreur,dp,alpha);
194  dp=alpha*dp;
195  }
196 
197  p+=1.*dp;
198  break;
199  }
200  }
201 
202  iteration++;
204  }
205  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
206 
207  //std::cout<<"erreur "<<erreur<<std::endl;
208  nbIteration=iteration;
209 }
210 
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:73
Type getValue(double i, double j) const
Definition: vpImage.h:1148
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
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
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
vpImage< double > dIy
unsigned int iterationGlobale
unsigned int nbIteration
Implementation of column vector and the associated operations.
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:3470
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0