Visual Servoing Platform  version 3.0.0
vpTemplateTrackerSSDForwardCompositional.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 #include <visp3/tt/vpTemplateTrackerSSDForwardCompositional.h>
40 #include <visp3/core/vpImageFilter.h>
41 
43  : vpTemplateTrackerSSD(warp), compoInitialised(false)
44 {
45 }
46 
48 {
49  // std::cout<<"Initialise precomputed value of Compositionnal Direct"<<std::endl;
50  int i,j;
51  for(unsigned int point=0;point<templateSize;point++)
52  {
53  i=ptTemplate[point].y;
54  j=ptTemplate[point].x;
55  ptTemplate[point].dW=new double[2*nbParam];
56  X1[0]=j;X1[1]=i;
57  Warp->computeDenom(X1,p);
58  Warp->getdWdp0(i,j,ptTemplate[point].dW);
59 
60  }
61  compoInitialised=true;
62 }
63 
65 {
66  initCompo(I);
67 }
68 
70 {
71  if(!compoInitialised)
72  std::cout<<"Compositionnal tracking no initialised\nUse InitCompo(vpImage<unsigned char> &I) function"<<std::endl;
73  double erreur=0;
74  unsigned int Nbpoint=0;
75 
76  if(blur)
80 
81  dW=0;
82 
83  double lambda=lambdaDep;
84  double IW,dIWx,dIWy;
85  double Tij;
86  unsigned int iteration=0;
87  int i,j;
88  double i2,j2;
89  double alpha=2.;
90  do
91  {
92  Nbpoint=0;
93  erreur=0;
94  G=0;
95  H=0 ;
96  Warp->computeCoeff(p);
97  for(unsigned int point=0;point<templateSize;point++)
98  {
99  i=ptTemplate[point].y;
100  j=ptTemplate[point].x;
101  X1[0]=j;X1[1]=i;
102 
103  Warp->computeDenom(X1,p);
104  Warp->warpX(X1,X2,p);
105 
106  j2=X2[0];i2=X2[1];
107  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
108  {
109  Tij=ptTemplate[point].val;
110  if(!blur)
111  IW=I.getValue(i2,j2);
112  else
113  IW=BI.getValue(i2,j2);
114  dIWx=dIx.getValue(i2,j2);
115  dIWy=dIy.getValue(i2,j2);
116  Nbpoint++;
117  //Calcul du Hessien
118  /*Warp->dWarp(X1,X2,p,dW);
119  double *tempt=new double[nbParam];
120  for(int it=0;it<nbParam;it++)
121  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;*/
122 
123  Warp->dWarpCompo(X1,X2,p,ptTemplate[point].dW,dW);
124 
125  double *tempt=new double[nbParam];
126  for(unsigned int it=0;it<nbParam;it++)
127  tempt[it] =dW[0][it]*dIWx+dW[1][it]*dIWy;
128 
129  for(unsigned int it=0;it<nbParam;it++)
130  for(unsigned int jt=0;jt<nbParam;jt++)
131  H[it][jt]+=tempt[it]*tempt[jt];
132 
133  double er=(Tij-IW);
134  for(unsigned int it=0;it<nbParam;it++)
135  G[it]+=er*tempt[it];
136 
137  erreur+=(er*er);
138  delete[] tempt;
139  }
140 
141 
142  }
143  if(Nbpoint==0) {
144  //std::cout<<"plus de point dans template suivi"<<std::endl;
145  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
146  }
147 
148  vpMatrix::computeHLM(H,lambda,HLM);
149 
150  try
151  {
152  dp=1.*HLM.inverseByLU()*G;
153  }
154  catch(vpException &e)
155  {
156  //std::cout<<"probleme inversion"<<std::endl;
157  throw(e);
158  }
159 
160  dp=gain*dp;
161  if(useBrent)
162  {
163  alpha=2.;
164  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
165  dp=alpha*dp;
166  }
167  Warp->pRondp(p,dp,p);
168  //p+=Gain*dp;
169  iteration++;
170  }
171  while( /*( erreur_prec-erreur<50) &&*/ (iteration < iterationMax));
172 
173  nbIteration=iteration;
174 }
175 
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: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
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &pres) const =0
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
vpImage< double > dIy
unsigned int nbIteration
vpMatrix inverseByLU() const
unsigned int getHeight() const
Definition: vpImage.h:152
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:3470