Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTemplateTrackerZNCCForwardAdditional.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/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  if(blur)
163 
164  /*vpImage<double> dIxx,dIxy,dIyx,dIyy;
165  getGradX(dIx, dIxx, fgdG,taillef);
166  getGradY(dIx, dIxy, fgdG,taillef);
167 
168  getGradX(dIy, dIyx, fgdG,taillef);
169  getGradY(dIy, dIyy, fgdG,taillef);*/
170 
171  dW=0;
172 
173  //double lambda=lambdaDep;
174  double IW,dIWx,dIWy;
175  double Tij;
176  unsigned int iteration=0;
177  int i,j;
178  double i2,j2;
179  double alpha=2.;
180  do
181  {
182  int Nbpoint=0;
183  double erreur=0;
184  G=0;
185  H=0 ;
186  Warp->computeCoeff(p);
187  double moyTij=0;
188  double moyIW=0;
189  double denom=0;
190  for(unsigned int point=0;point<templateSize;point++)
191  {
192  i=ptTemplate[point].y;
193  j=ptTemplate[point].x;
194  X1[0]=j;X1[1]=i;
195 
196  Warp->computeDenom(X1,p);
197  Warp->warpX(X1,X2,p);
198 
199  j2=X2[0];i2=X2[1];
200  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
201  {
202  Tij=ptTemplate[point].val;
203 
204  if(!blur)
205  IW=I.getValue(i2,j2);
206  else
207  IW=BI.getValue(i2,j2);
208 
209  Nbpoint++;
210  moyTij+=Tij;
211  moyIW+=IW;
212  }
213  }
214 
215  if(! Nbpoint) {
217  "Cannot track the template: no point")) ;
218  }
219 
220  moyTij=moyTij/Nbpoint;
221  moyIW=moyIW/Nbpoint;
222  //vpMatrix d2Wx(nbParam,nbParam);
223  //vpMatrix d2Wy(nbParam,nbParam);
224  for(unsigned int point=0;point<templateSize;point++)
225  {
226  i=ptTemplate[point].y;
227  j=ptTemplate[point].x;
228  X1[0]=j;X1[1]=i;
229 
230  Warp->computeDenom(X1,p);
231  Warp->warpX(X1,X2,p);
232 
233  j2=X2[0];i2=X2[1];
234  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
235  {
236  Tij=ptTemplate[point].val;
237 
238  if(!blur)
239  IW=I.getValue(i2,j2);
240  else
241  IW=BI.getValue(i2,j2);
242 
243  dIWx=dIx.getValue(i2,j2);
244  dIWy=dIy.getValue(i2,j2);
245  //Calcul du Hessien
246  Warp->dWarp(X1,X2,p,dW);
247  double *tempt=new double[nbParam];
248  for(unsigned int it=0;it<nbParam;it++)
249  tempt[it]=dW[0][it]*dIWx+dW[1][it]*dIWy;
250 
251 
252  double prod=(Tij-moyTij);
253  for(unsigned int it=0;it<nbParam;it++)
254  G[it]+=prod*tempt[it];
255 
256  /* Warp->d2Warp(X1,X2,p,d2Wx,d2Wy);
257  for(int it=0;it<nbParam;it++)
258  for(int jt=0;jt<nbParam;jt++)
259  H[it][jt]+=prod*(d2Wx[it][jt]*dIWx+d2Wx[it][jt]*dIWy);*/
260  /*double d_Ixx=dIxx.getValue(i2,j2);
261  double d_Iyy=dIyy.getValue(i2,j2);
262  double d_Ixy=dIxy.getValue(i2,j2);
263 
264  for(int it=0;it<nbParam;it++)
265  for(int jt=0;jt<nbParam;jt++)
266  H[it][jt] +=prod*(dW[0][it]*(dW[0][jt]*d_Ixx+dW[1][jt]*d_Ixy)
267  +dW[1][it]*(dW[0][jt]*d_Ixy+dW[1][jt]*d_Iyy));*/
268  /*H[0][0]+=prod*d_Ixx;
269  H[1][0]+=prod*d_Ixy;
270  H[0][1]+=prod*d_Ixy;
271  H[1][1]+=prod*d_Iyy;*/
272 
273  double er=(Tij-IW);
274  erreur+=(er*er);
275  denom+=(Tij-moyTij)*(Tij-moyTij)*(IW-moyIW)*(IW-moyIW);
276  delete[] tempt;
277  }
278 
279 
280  }
281  /*std::cout<<"G="<<G<<std::endl;
282  std::cout<<"H="<<H<<std::endl;
283  std::cout<<" denom="<<denom<<std::endl;*/
284  G=G/sqrt(denom);
285  //std::cout<<G<<std::endl;
286  H=H/sqrt(denom);
287 
288  //if(Nbpoint==0)std::cout<<"plus de point dans template suivi"<<std::endl; // cannot occur
289 
290  try
291  {
292  //vpMatrix::computeHLM(H,lambda,HLM);
293  //dp=1.*HLM.inverseByLU()*G;
294  dp=1.*HLMdesireInverse*G;
295  }
296  catch(vpException &e)
297  {
298  //std::cout<<"probleme inversion"<<std::endl;
299  throw(e);
300  }
301 
302  dp=gain*dp;
303  if(useBrent)
304  {
305  alpha=2.;
306  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
307  dp=alpha*dp;
308  }
309  p-=dp;
310  iteration++;
311  }
312  while( /*( erreur_prec-erreur<50) && */(iteration < iterationMax));
313 
314  //std::cout<<"erreur "<<erreur<<std::endl;
315  nbIteration=iteration;
316 }
317 
unsigned int getWidth() const
Definition: vpImage.h:226
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: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
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:175
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:3525
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0