Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTemplateTrackerMIForwardCompositional.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  * 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  for(unsigned int point=0;point<templateSize;point++)
51  {
52  int i=ptTemplate[point].y;
53  int j=ptTemplate[point].x;
54  X1[0]=j;X1[1]=i;
55  Warp->computeDenom(X1,p);
56  ptTemplate[point].dW=new double[2*nbParam];
57  Warp->getdWdp0(i,j,ptTemplate[point].dW);
58 
59  double Tij=ptTemplate[point].val;
60  int ct=(int)((Tij*(Nc-1))/255.);
61  double et=(Tij*(Nc-1))/255.-ct;
62  ptTemplateSupp[point].et=et;
63  ptTemplateSupp[point].ct=ct;
64  ptTemplateSupp[point].Bt=new double[4];
65  ptTemplateSupp[point].dBt=new double[4];
66  for(char it=-1;it<=2;it++)
67  {
68  ptTemplateSupp[point].Bt[it+1] =vpTemplateTrackerBSpline::Bspline4(-it+et);
69  ptTemplateSupp[point].dBt[it+1] =vpTemplateTrackerMIBSpline::dBspline4(-it+et);
70  }
71 
72  }
73  CompoInitialised=true;
74 }
76 {
77  initCompo();
78 
79  //std::cout<<"Initialise Hessian at Desired position..."<<std::endl;
80 
81  dW=0;
82 
83  if(blur)
87 
88  //double erreur=0;
89  int Nbpoint=0;
90 
91  //double Tij;
92  double IW,dx,dy;
93  int cr,ct;
94  double er,et;
95 
96  Nbpoint=0;
97  //erreur=0;
98 
100 
101  Warp->computeCoeff(p);
102  for(unsigned int point=0;point<templateSize;point++)
103  {
104  int i=ptTemplate[point].y;
105  int j=ptTemplate[point].x;
106  X1[0]=j;
107  X1[1]=i;
108 
109  Warp->computeDenom(X1,p);
110  Warp->warpX(X1,X2,p);
111 
112  double j2=X2[0];
113  double i2=X2[1];
114 
115  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
116  {
117  Nbpoint++;
118  //Tij=ptTemplate[point].val;
119  if(!blur)
120  IW=I.getValue(i2,j2);
121  else
122  IW=BI.getValue(i2,j2);
123 
124  dx=1.*dIx.getValue(i2,j2)*(Nc-1)/255.;
125  dy=1.*dIy.getValue(i2,j2)*(Nc-1)/255.;
126 
127  cr=ptTemplateSupp[point].ct;
128  er=ptTemplateSupp[point].et;
129  ct=(int)((IW*(Nc-1))/255.);
130  et=((double)IW*(Nc-1))/255.-ct;
131 
132  Warp->dWarpCompo(X1,X2,p,ptTemplate[point].dW,dW);
133 
134  double *tptemp=new double[nbParam];
135  for(unsigned int it=0;it<nbParam;it++)
136  tptemp[it] =dW[0][it]*dx+dW[1][it]*dy;
137 
138  //calcul de l'erreur
139  //erreur+=(Tij-IW)*(Tij-IW);
140 
141  vpTemplateTrackerMIBSpline::PutTotPVBspline(PrtTout, cr, er, ct, et, Nc,tptemp, nbParam, bspline);
142 
143  delete[] tptemp;
144  }
145  }
146  double MI;
147  computeProba(Nbpoint);
148  computeMI(MI);
150 
152 
155  //std::cout<<"Hdesire = "<<Hdesire<<std::endl;
156  //std::cout<<"\tEnd initialisation..."<<std::endl;
157 }
158 
159 
161 {
162  if(!CompoInitialised)
163  std::cout<<"Compositionnal tracking no initialised\nUse initCompo(vpImage<unsigned char> &I) function"<<std::endl;
164  dW=0;
165 
166  if(blur)
170 
171  //double erreur=0;
172 
174  double MI=0,MIprec=-1000;
175 
177 
178  double i2,j2;
179  //double Tij;
180  double IW;
181  //unsigned
182  int cr,ct;
183  double er,et;
184  double dx,dy;
185 
186  vpColVector dpinv(nbParam);
187  double alpha=2.;
188 
189  int i,j;
190  unsigned int iteration=0;
191  do
192  {
193  int Nbpoint=0;
194  MIprec=MI;
195  MI=0;
196  //erreur=0;
197 
199 
200  Warp->computeCoeff(p);
201 
202  for(unsigned int point=0;point<templateSize;point++)
203  {
204  i=ptTemplate[point].y;
205  j=ptTemplate[point].x;
206  X1[0]=j;X1[1]=i;
207  Warp->warpX(i,j,i2,j2,p);
208  X2[0]=j2;X2[1]=i2;
209 
210  Warp->computeDenom(X1,p);
211  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
212  {
213  Nbpoint++;
214  //Tij=ptTemplate[point].val;
215  if(!blur)
216  IW=I.getValue(i2,j2);
217  else
218  IW=BI.getValue(i2,j2);
219 
220  dx=1.*dIx.getValue(i2,j2)*(Nc-1)/255.;
221  dy=1.*dIy.getValue(i2,j2)*(Nc-1)/255.;
222 
223  ct=(int)((IW*(Nc-1))/255.);
224  et=((double)IW*(Nc-1))/255.-ct;
225  cr=ptTemplateSupp[point].ct;
226  er=ptTemplateSupp[point].et;
227 
228  Warp->dWarpCompo(X1,X2,p,ptTemplate[point].dW,dW);
229 
230  double *tptemp=new double[nbParam];
231  for(unsigned int it=0;it<nbParam;it++)
232  tptemp[it] =dW[0][it]*dx+dW[1][it]*dy;
233 
234 
235  //calcul de l'erreur
236  //erreur+=(Tij-IW)*(Tij-IW);
237 
239  vpTemplateTrackerMIBSpline::PutTotPVBsplineNoSecond(PrtTout, cr, er, ct, et, Nc, tptemp, nbParam, bspline);
241  vpTemplateTrackerMIBSpline::PutTotPVBspline(PrtTout, cr, er, ct, et, Nc, tptemp, nbParam, bspline);
242 
243  delete[] tptemp;
244 
245  }
246  }
247  if(Nbpoint==0)
248  {
249  //std::cout<<"plus de point dans template suivi"<<std::endl;
250  diverge=true;
251  MI=0;
252  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
253  }
254  else
255  {
256  computeProba(Nbpoint);
257  computeMI(MI);
259  computeHessien(H);
260  computeGradient();
261 
263 
264  try
265  {
266  switch(hessianComputation)
267  {
270  break;
272  if(HLM.cond()>HLMdesire.cond())
274  else
275  dp=gain*0.2*HLM.inverseByLU()*G;
276  break;
277  default:
278  dp=gain*0.2*HLM.inverseByLU()*G;
279  break;
280  }
281  }
282  catch(vpException &e)
283  {
284  //std::cerr<<"probleme inversion"<<std::endl;
285  throw(e);
286  }
287  }
288 
290  dp=-0.04*dp;
291  else
292  dp=1.*dp;
293 
294  if(useBrent)
295  {
296  alpha=2.;
297  computeOptimalBrentGain(I,p,-MI,dp,alpha);
298  dp=alpha*dp;
299  }
300  Warp->pRondp(p,dp,p);
301 
302  iteration++;
303 
304 
305  }
306  while( (std::fabs(MI-MIprec) > std::fabs(MI)*std::numeric_limits<double>::epsilon()) && (iteration< iterationMax) );
307  //while( (MI!=MIprec) && (iteration< iterationMax) );
308  nbIteration=iteration;
309 
312  {
313  MI_postEstimation = -1;
314  }
315 }
316 
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: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
vpHessienApproximationType ApproxHessian
double cond() const
Definition: vpMatrix.cpp:3500
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)
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: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
void computeProba(int &nbpoint)
vpHessienType hessianComputation