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