Visual Servoing Platform  version 3.0.0
vpTemplateTrackerMIForwardCompositional.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  * Example of template tracking.
32  *
33  * Authors:
34  * Amaury Dame
35  * Aurelien Yol
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 #include <visp3/tt_mi/vpTemplateTrackerMIForwardCompositional.h>
40 
42  : vpTemplateTrackerMI(_warp), CompoInitialised(false)
43 {
44 }
45 
47 {
48  std::cout<<"Initialise precomputed value of Compositionnal Direct"<<std::endl;
49  ptTemplateSupp=new vpTemplateTrackerPointSuppMIInv[templateSize];
50  int i,j;
51  double et;
52  int ct;
53  double Tij;
54  for(unsigned int point=0;point<templateSize;point++)
55  {
56  i=ptTemplate[point].y;
57  j=ptTemplate[point].x;
58  X1[0]=j;X1[1]=i;
59  Warp->computeDenom(X1,p);
60  ptTemplate[point].dW=new double[2*nbParam];
61  Warp->getdWdp0(i,j,ptTemplate[point].dW);
62 
63  Tij=ptTemplate[point].val;
64  ct=(int)((Tij*(Nc-1))/255.);
65  et=(Tij*(Nc-1))/255.-ct;
66  ptTemplateSupp[point].et=et;
67  ptTemplateSupp[point].ct=ct;
68  ptTemplateSupp[point].Bt=new double[4];
69  ptTemplateSupp[point].dBt=new double[4];
70  for(char it=-1;it<=2;it++)
71  {
72  ptTemplateSupp[point].Bt[it+1] =vpTemplateTrackerBSpline::Bspline4(-it+et);
73  ptTemplateSupp[point].dBt[it+1] =vpTemplateTrackerMIBSpline::dBspline4(-it+et);
74  }
75 
76  }
77  CompoInitialised=true;
78 }
80 {
81  initCompo();
82 
83  //std::cout<<"Initialise Hessian at Desired position..."<<std::endl;
84 
85  dW=0;
86 
87  if(blur)
91 
92  double erreur=0;
93  int Nbpoint=0;
94 
95  double i2,j2;
96  double Tij;
97  double IW,dx,dy;
98  int cr,ct;
99  double er,et;
100 
101  int i,j;
102 
103  Nbpoint=0;
104  erreur=0;
105 
107 
108  Warp->computeCoeff(p);
109  for(unsigned int point=0;point<templateSize;point++)
110  {
111  i=ptTemplate[point].y;
112  j=ptTemplate[point].x;
113  X1[0]=j;X1[1]=i;
114 
115  Warp->computeDenom(X1,p);
116  Warp->warpX(X1,X2,p);
117 
118  j2=X2[0];i2=X2[1];
119 
120  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
121  {
122  Nbpoint++;
123  Tij=ptTemplate[point].val;
124  if(!blur)
125  IW=I.getValue(i2,j2);
126  else
127  IW=BI.getValue(i2,j2);
128 
129  dx=1.*dIx.getValue(i2,j2)*(Nc-1)/255.;
130  dy=1.*dIy.getValue(i2,j2)*(Nc-1)/255.;
131 
132  cr=ptTemplateSupp[point].ct;
133  er=ptTemplateSupp[point].et;
134  ct=(int)((IW*(Nc-1))/255.);
135  et=((double)IW*(Nc-1))/255.-ct;
136 
137  Warp->dWarpCompo(X1,X2,p,ptTemplate[point].dW,dW);
138 
139  double *tptemp=new double[nbParam];
140  for(unsigned int it=0;it<nbParam;it++)
141  tptemp[it] =dW[0][it]*dx+dW[1][it]*dy;
142 
143  //calcul de l'erreur
144  erreur+=(Tij-IW)*(Tij-IW);
145 
146  vpTemplateTrackerMIBSpline::PutTotPVBspline(PrtTout, cr, er, ct, et, Nc,tptemp, nbParam, bspline);
147 
148  delete[] tptemp;
149  }
150  }
151  double MI;
152  computeProba(Nbpoint);
153  computeMI(MI);
155 
157 
160  //std::cout<<"Hdesire = "<<Hdesire<<std::endl;
161  //std::cout<<"\tEnd initialisation..."<<std::endl;
162 }
163 
164 
166 {
167  if(!CompoInitialised)
168  std::cout<<"Compositionnal tracking no initialised\nUse initCompo(vpImage<unsigned char> &I) function"<<std::endl;
169  dW=0;
170 
171  if(blur)
175 
176  double erreur=0;
177  int Nbpoint=0;
178 
179 
181  double MI=0,MIprec=-1000;
182 
184 
185  double i2,j2;
186  double Tij;
187  double IW;
188  //unsigned
189  int cr,ct;
190  double er,et;
191  double dx,dy;
192 
193  vpColVector dpinv(nbParam);
194  double alpha=2.;
195 
196  int i,j;
197  unsigned int iteration=0;
198  do
199  {
200  Nbpoint=0;
201  MIprec=MI;
202  MI=0;
203  erreur=0;
204 
206 
207  Warp->computeCoeff(p);
208 
209  for(unsigned int point=0;point<templateSize;point++)
210  {
211  i=ptTemplate[point].y;
212  j=ptTemplate[point].x;
213  X1[0]=j;X1[1]=i;
214  Warp->warpX(i,j,i2,j2,p);
215  X2[0]=j2;X2[1]=i2;
216 
217  Warp->computeDenom(X1,p);
218  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
219  {
220  Nbpoint++;
221  Tij=ptTemplate[point].val;
222  if(!blur)
223  IW=I.getValue(i2,j2);
224  else
225  IW=BI.getValue(i2,j2);
226 
227  dx=1.*dIx.getValue(i2,j2)*(Nc-1)/255.;
228  dy=1.*dIy.getValue(i2,j2)*(Nc-1)/255.;
229 
230  ct=(int)((IW*(Nc-1))/255.);
231  et=((double)IW*(Nc-1))/255.-ct;
232  cr=ptTemplateSupp[point].ct;
233  er=ptTemplateSupp[point].et;
234 
235  Warp->dWarpCompo(X1,X2,p,ptTemplate[point].dW,dW);
236 
237  double *tptemp=new double[nbParam];
238  for(unsigned int it=0;it<nbParam;it++)
239  tptemp[it] =dW[0][it]*dx+dW[1][it]*dy;
240 
241 
242  //calcul de l'erreur
243  erreur+=(Tij-IW)*(Tij-IW);
244 
246  vpTemplateTrackerMIBSpline::PutTotPVBsplineNoSecond(PrtTout, cr, er, ct, et, Nc, tptemp, nbParam, bspline);
248  vpTemplateTrackerMIBSpline::PutTotPVBspline(PrtTout, cr, er, ct, et, Nc, tptemp, nbParam, bspline);
249 
250  delete[] tptemp;
251 
252  }
253  }
254  if(Nbpoint==0)
255  {
256  //std::cout<<"plus de point dans template suivi"<<std::endl;
257  diverge=true;
258  MI=0;
259  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
260  }
261  else
262  {
263  computeProba(Nbpoint);
264  computeMI(MI);
266  computeHessien(H);
267  computeGradient();
268 
270 
271  try
272  {
273  switch(hessianComputation)
274  {
277  break;
279  if(HLM.cond()>HLMdesire.cond())
281  else
282  dp=gain*0.2*HLM.inverseByLU()*G;
283  break;
284  default:
285  dp=gain*0.2*HLM.inverseByLU()*G;
286  break;
287  }
288  }
289  catch(vpException &e)
290  {
291  //std::cerr<<"probleme inversion"<<std::endl;
292  throw(e);
293  }
294  }
295 
297  dp=-0.04*dp;
298  else
299  dp=1.*dp;
300 
301  if(useBrent)
302  {
303  alpha=2.;
304  computeOptimalBrentGain(I,p,-MI,dp,alpha);
305  dp=alpha*dp;
306  }
307  Warp->pRondp(p,dp,p);
308 
309  iteration++;
310 
311 
312  }
313  while( (std::fabs(MI-MIprec) > std::fabs(MI)*std::numeric_limits<double>::epsilon()) && (iteration< iterationMax) );
314  //while( (MI!=MIprec) && (iteration< iterationMax) );
315  nbIteration=iteration;
316 
319  {
320  MI_postEstimation = -1;
321  }
322 }
323 
virtual void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW)=0
void computeHessien(vpMatrix &H)
void initHessienDesired(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
vpHessienApproximationType ApproxHessian
double cond() const
Definition: vpMatrix.cpp:3445
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.
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
vpImage< double > dIx
void computeMI(double &MI)
vpImage< double > dIy
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
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
void computeProba(int &nbpoint)
vpHessienType hessianComputation