Visual Servoing Platform  version 3.0.0
vpTemplateTrackerZNCCForwardAdditional.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/vpTemplateTrackerZNCCForwardAdditional.h>
40 #include <visp3/core/vpImageFilter.h>
41 
43 {
44  useCompositionnal=false;
45 }
46 
48 {
49  if(blur)
53 
54  vpImage<double> dIxx,dIxy,dIyx,dIyy;
57 
60 
61  Warp->computeCoeff(p);
62  double IW,dIWx,dIWy;
63  double Tij;
64  int i,j;
65  double i2,j2;
66  int Nbpoint=0;
67 
68  double moyTij=0;
69  double moyIW=0;
70  double denom=0;
71  for(unsigned int point=0;point<templateSize;point++)
72  {
73  i=ptTemplate[point].y;
74  j=ptTemplate[point].x;
75  X1[0]=j;X1[1]=i;
76  X2[0]=j;X2[1]=i;
77 
78  Warp->computeDenom(X1,p);
79 
80  j2=X2[0];i2=X2[1];
81 
82  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
83  {
84  Tij=ptTemplate[point].val;
85 
86  if(!blur)
87  IW=I.getValue(i2,j2);
88  else
89  IW=BI.getValue(i2,j2);
90 
91  Nbpoint++;
92  moyTij+=Tij;
93  moyIW+=IW;
94  }
95  }
96  moyTij=moyTij/Nbpoint;
97  moyIW=moyIW/Nbpoint;
98  Hdesire=0;
99  for(unsigned int point=0;point<templateSize;point++)
100  {
101  i=ptTemplate[point].y;
102  j=ptTemplate[point].x;
103  X1[0]=j;X1[1]=i;
104  X2[0]=j;X2[1]=i;
105 
106  Warp->computeDenom(X1,p);
107 
108  j2=X2[0];i2=X2[1];
109 
110  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
111  {
112  Tij=ptTemplate[point].val;
113 
114  if(!blur)
115  IW=I.getValue(i2,j2);
116  else
117  IW=BI.getValue(i2,j2);
118 
119  dIWx=dIx.getValue(i2,j2);
120  dIWy=dIy.getValue(i2,j2);
121  //Calcul du Hessien
122  Warp->dWarp(X1,X2,p,dW);
123  double *tempt=new double[nbParam];
124  for(unsigned int it=0;it<nbParam;it++)
125  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
126 
127 
128  double prod=(Tij-moyTij);
129 
130  double d_Ixx=dIxx.getValue(i2,j2);
131  double d_Iyy=dIyy.getValue(i2,j2);
132  double d_Ixy=dIxy.getValue(i2,j2);
133 
134  for(unsigned int it=0;it<nbParam;it++)
135  for(unsigned int jt=0;jt<nbParam;jt++)
136  Hdesire[it][jt] +=prod*(dW[0][it]*(dW[0][jt]*d_Ixx+dW[1][jt]*d_Ixy)
137  +dW[1][it]*(dW[0][jt]*d_Ixy+dW[1][jt]*d_Iyy));
138  /*Hdesire[0][0]+=prod*d_Ixx;
139  Hdesire[1][0]+=prod*d_Ixy;
140  Hdesire[0][1]+=prod*d_Ixy;
141  Hdesire[1][1]+=prod*d_Iyy;*/
142 
143  denom+=(Tij-moyTij)*(Tij-moyTij)*(IW-moyIW)*(IW-moyIW);
144  delete[] tempt;
145  }
146 
147 
148  }
149 
150  Hdesire=Hdesire/sqrt(denom);
153  //std::cout<<"Hdesire = "<<Hdesire<<std::endl;
154 
155 }
156 
158 {
159  double erreur=0;
160  int Nbpoint=0;
161 
162  if(blur)
166 
167  /*vpImage<double> dIxx,dIxy,dIyx,dIyy;
168  getGradX(dIx, dIxx, fgdG,taillef);
169  getGradY(dIx, dIxy, fgdG,taillef);
170 
171  getGradX(dIy, dIyx, fgdG,taillef);
172  getGradY(dIy, dIyy, fgdG,taillef);*/
173 
174  dW=0;
175 
176  //double lambda=lambdaDep;
177  double IW,dIWx,dIWy;
178  double Tij;
179  unsigned int iteration=0;
180  int i,j;
181  double i2,j2;
182  double alpha=2.;
183  do
184  {
185  Nbpoint=0;
186  erreur=0;
187  G=0;
188  H=0 ;
189  Warp->computeCoeff(p);
190  double moyTij=0;
191  double moyIW=0;
192  double denom=0;
193  for(unsigned int point=0;point<templateSize;point++)
194  {
195  i=ptTemplate[point].y;
196  j=ptTemplate[point].x;
197  X1[0]=j;X1[1]=i;
198 
199  Warp->computeDenom(X1,p);
200  Warp->warpX(X1,X2,p);
201 
202  j2=X2[0];i2=X2[1];
203  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
204  {
205  Tij=ptTemplate[point].val;
206 
207  if(!blur)
208  IW=I.getValue(i2,j2);
209  else
210  IW=BI.getValue(i2,j2);
211 
212  Nbpoint++;
213  moyTij+=Tij;
214  moyIW+=IW;
215  }
216  }
217 
218  if(! Nbpoint) {
220  "Cannot track the template: no point")) ;
221  }
222 
223  moyTij=moyTij/Nbpoint;
224  moyIW=moyIW/Nbpoint;
225  //vpMatrix d2Wx(nbParam,nbParam);
226  //vpMatrix d2Wy(nbParam,nbParam);
227  for(unsigned int point=0;point<templateSize;point++)
228  {
229  i=ptTemplate[point].y;
230  j=ptTemplate[point].x;
231  X1[0]=j;X1[1]=i;
232 
233  Warp->computeDenom(X1,p);
234  Warp->warpX(X1,X2,p);
235 
236  j2=X2[0];i2=X2[1];
237  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
238  {
239  Tij=ptTemplate[point].val;
240 
241  if(!blur)
242  IW=I.getValue(i2,j2);
243  else
244  IW=BI.getValue(i2,j2);
245 
246  dIWx=dIx.getValue(i2,j2);
247  dIWy=dIy.getValue(i2,j2);
248  //Calcul du Hessien
249  Warp->dWarp(X1,X2,p,dW);
250  double *tempt=new double[nbParam];
251  for(unsigned int it=0;it<nbParam;it++)
252  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
253 
254 
255  double prod=(Tij-moyTij);
256  for(unsigned int it=0;it<nbParam;it++)
257  G[it]+=prod*tempt[it];
258 
259  /* Warp->d2Warp(X1,X2,p,d2Wx,d2Wy);
260  for(int it=0;it<nbParam;it++)
261  for(int jt=0;jt<nbParam;jt++)
262  H[it][jt]+=prod*(d2Wx[it][jt]*dIWx+d2Wx[it][jt]*dIWy);*/
263  /*double d_Ixx=dIxx.getValue(i2,j2);
264  double d_Iyy=dIyy.getValue(i2,j2);
265  double d_Ixy=dIxy.getValue(i2,j2);
266 
267  for(int it=0;it<nbParam;it++)
268  for(int jt=0;jt<nbParam;jt++)
269  H[it][jt] +=prod*(dW[0][it]*(dW[0][jt]*d_Ixx+dW[1][jt]*d_Ixy)
270  +dW[1][it]*(dW[0][jt]*d_Ixy+dW[1][jt]*d_Iyy));*/
271  /*H[0][0]+=prod*d_Ixx;
272  H[1][0]+=prod*d_Ixy;
273  H[0][1]+=prod*d_Ixy;
274  H[1][1]+=prod*d_Iyy;*/
275 
276  double er=(Tij-IW);
277  erreur+=(er*er);
278  denom+=(Tij-moyTij)*(Tij-moyTij)*(IW-moyIW)*(IW-moyIW);
279  delete[] tempt;
280  }
281 
282 
283  }
284  /*std::cout<<"G="<<G<<std::endl;
285  std::cout<<"H="<<H<<std::endl;
286  std::cout<<" denom="<<denom<<std::endl;*/
287  G=G/sqrt(denom);
288  //std::cout<<G<<std::endl;
289  H=H/sqrt(denom);
290 
291  //if(Nbpoint==0)std::cout<<"plus de point dans template suivi"<<std::endl; // cannot occur
292 
293  try
294  {
295  //vpMatrix::computeHLM(H,lambda,HLM);
296  //dp=1.*HLM.inverseByLU()*G;
297  dp=1.*HLMdesireInverse*G;
298  }
299  catch(vpException &e)
300  {
301  //std::cout<<"probleme inversion"<<std::endl;
302  throw(e);
303  }
304 
305  dp=gain*dp;
306  if(useBrent)
307  {
308  alpha=2.;
309  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
310  dp=alpha*dp;
311  }
312  p-=dp;
313  iteration++;
314  }
315  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
316 
317  //std::cout<<"erreur "<<erreur<<std::endl;
318  nbIteration=iteration;
319 }
320 
unsigned int getWidth() const
Definition: vpImage.h:161
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
vpTemplateTrackerPoint * ptTemplate
static void getGradY(const vpImage< unsigned char > &I, vpImage< double > &dIy)
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
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
void initHessienDesired(const vpImage< unsigned char > &I)
vpMatrix inverseByLU() const
unsigned int getHeight() const
Definition: vpImage.h:152
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:3470
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0