Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTemplateTrackerSSDForwardAdditional.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 
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  if(blur)
57 
58  dW=0;
59 
60  double lambda=lambdaDep;
61  double IW,dIWx,dIWy;
62  double Tij;
63  unsigned int iteration=0;
64  int i,j;
65  double i2,j2;
66  double alpha=2.;
67  do
68  {
69  unsigned int Nbpoint=0;
70  double erreur=0;
71  G=0;
72  H=0 ;
73  Warp->computeCoeff(p);
74  for(unsigned int point=0;point<templateSize;point++)
75  {
76  i=ptTemplate[point].y;
77  j=ptTemplate[point].x;
78  X1[0]=j;X1[1]=i;
79 
80  Warp->computeDenom(X1,p);
81  Warp->warpX(X1,X2,p);
82 
83  j2=X2[0];i2=X2[1];
84  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
85  {
86  Tij=ptTemplate[point].val;
87 
88  if(!blur)
89  IW=I.getValue(i2,j2);
90  else
91  IW=BI.getValue(i2,j2);
92 
93  dIWx=dIx.getValue(i2,j2);
94  dIWy=dIy.getValue(i2,j2);
95  Nbpoint++;
96  //Calcul du Hessien
97  Warp->dWarp(X1,X2,p,dW);
98  double *tempt=new double[nbParam];
99  for(unsigned int it=0;it<nbParam;it++)
100  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
101 
102  for(unsigned int it=0;it<nbParam;it++)
103  for(unsigned int jt=0;jt<nbParam;jt++)
104  H[it][jt]+=tempt[it]*tempt[jt];
105 
106  double er=(Tij-IW);
107  for(unsigned int it=0;it<nbParam;it++)
108  G[it]+=er*tempt[it];
109 
110  erreur+=(er*er);
111  delete[] tempt;
112  }
113 
114 
115  }
116  if(Nbpoint==0) {
117  //std::cout<<"plus de point dans template suivi"<<std::endl;
118  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
119  }
120 
121  vpMatrix::computeHLM(H,lambda,HLM);
122  try
123  {
124  dp=1.*HLM.inverseByLU()*G;
125  }
126  catch(vpException &e)
127  {
128  throw(e);
129  }
130 
131  switch(minimizationMethod)
132  {
134  {
135  vpColVector p_test_LMA(nbParam);
136  p_test_LMA=p+1.*dp;
137  erreur=-getCost(I,p);
138  double erreur_LMA=-getCost(I,p_test_LMA);
139  if(erreur_LMA<erreur)
140  {
141  p=p_test_LMA;
142  lambda=(lambda/10.<1e-6)?lambda/10.:1e-6;
143  }
144  else
145  {
146  lambda=(lambda*10.<1e6)?1e6:lambda*10.;
147  }
148  }
149  break;
151  {
152  dp=gain*0.000001*G;
153  if(useBrent)
154  {
155  alpha=2.;
156  computeOptimalBrentGain(I,p,erreur,dp,alpha);
157  dp=alpha*dp;
158  }
159  p += 1. * dp;
160  break;
161  }
162 
164  {
165  if(iterationGlobale!=0)
166  {
167  vpColVector s_quasi=p-p_prec;
168  vpColVector y_quasi=G-G_prec;
169  double s_scal_y=s_quasi.t()*y_quasi;
170  //if(s_scal_y!=0)//BFGS
171  // 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;
172  //if(s_scal_y!=0.0)//DFP
173  if(std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) //DFP
174  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));
175  }
176  dp=-KQuasiNewton*G;
177  p_prec=p;
178  G_prec=G;
179  p-=1.01*dp;
180  }
181  break;
182 
184  default:
185  {
186  if(useBrent)
187  {
188  alpha=2.;
189  computeOptimalBrentGain(I,p,erreur,dp,alpha);
190  dp=alpha*dp;
191  }
192 
193  p+=1.*dp;
194  break;
195  }
196  }
197 
198  iteration++;
200  }
201  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
202 
203  //std::cout<<"erreur "<<erreur<<std::endl;
204  nbIteration=iteration;
205 }
206 
void trackNoPyr(const vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:226
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: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
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
Error that can be emited by the vpTracker class and its derivates.
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:175
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:3525
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0