ViSP  2.9.0
vpTemplateTrackerSSDInverseCompositional.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerSSDInverseCompositional.cpp 4672 2014-02-17 09:01:17Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Template tracker.
35  *
36  * Authors:
37  * Amaury Dame
38  * Aurelien Yol
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 #include <visp/vpTemplateTrackerSSDInverseCompositional.h>
43 #include <visp/vpImageTools.h>
44 
46  : vpTemplateTrackerSSD(warp), compoInitialised(false), HInv(), HCompInverse(), useTemplateSelect(false),
47  evolRMS(0), x_pos(), y_pos(), threshold_RMS(1e-8)
48 {
49  useInverse=true;
52 }
53 
55 {
56 
57  H=0;
58  int i,j;
59 
60  for(unsigned int point=0;point<templateSize;point++)
61  {
62  if((!useTemplateSelect)||(ptTemplateSelect[point]))
63  {
64  i=ptTemplate[point].y;
65  j=ptTemplate[point].x;
66  X1[0]=j;X1[1]=i;
67  Warp->computeDenom(X1,p);
68  ptTemplate[point].dW=new double[nbParam];
69 
70  Warp->getdW0(i,j,ptTemplate[point].dy,ptTemplate[point].dx,ptTemplate[point].dW);
71 
72  for(unsigned int it=0;it<nbParam;it++)
73  for(unsigned int jt=0;jt<nbParam;jt++)
74  H[it][jt]+=ptTemplate[point].dW[it]*ptTemplate[point].dW[jt];
75  }
76 
77  }
78  HInv=H;
79  vpMatrix HLMtemp(nbParam,nbParam);
81 
83  HCompInverse=HLMtemp.inverseByLU();
84  //std::cout<<Hinverse<<std::endl;
85  vpColVector dWtemp(nbParam);
86  vpColVector HiGtemp(nbParam);
87 
88  for(unsigned int point=0;point<templateSize;point++)
89  {
90  if((!useTemplateSelect)||(ptTemplateSelect[point]))
91  {
92  i=ptTemplate[point].y;
93  j=ptTemplate[point].x;
94  for(unsigned int it=0;it<nbParam;it++)
95  dWtemp[it]=ptTemplate[point].dW[it];
96 
97  HiGtemp = -1.*HCompInverse*dWtemp;
98  ptTemplate[point].HiG=new double[nbParam];
99 
100  for(unsigned int it=0;it<nbParam;it++)
101  ptTemplate[point].HiG[it]=HiGtemp[it];
102  }
103  }
104  compoInitialised=true;
105 }
106 
108 {
109  initCompInverse(I);
110 }
111 
113 {
114  double erreur=0;
115  int Nbpoint=0;
116  if(blur)
118 
119  vpColVector dpinv(nbParam);
120  double IW;
121  double Tij;
122  unsigned int iteration=0;
123  int i,j;
124  double i2,j2;
125  double alpha=2.;
126  //vpTemplateTrackerPointtest *pt;
127  initPosEvalRMS(p);
128 
130  do
131  {
132  Nbpoint=0;
133  erreur=0;
134  dp=0;
135  Warp->computeCoeff(p);
136  for(unsigned int point=0;point<templateSize;point++)
137  {
138  if((!useTemplateSelect)||(ptTemplateSelect[point]))
139  {
140  //pt=&ptTemplatetest[point];
141  pt=&ptTemplate[point];
142  i=pt->y;
143  j=pt->x;
144  X1[0]=j;X1[1]=i;
145  Warp->computeDenom(X1,p);
146  Warp->warpX(X1,X2,p);
147  j2=X2[0];i2=X2[1];
148 
149  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
150  {
151  Tij=pt->val;
152  if(!blur)
153  IW=I.getValue(i2,j2);
154  else
155  IW=BI.getValue(i2,j2);
156  Nbpoint++;
157  double er=(Tij-IW);
158  for(unsigned int it=0;it<nbParam;it++)
159  dp[it]+=er*pt->HiG[it];
160 
161  erreur+=er*er;
162  }
163  }
164  }
165  if(Nbpoint==0)std::cout<<"plus de point dans template suivi"<<std::endl;
166  dp=gain*dp;
167  //std::cout<<erreur/Nbpoint<<","<<GetCost(I,p)<<std::endl;
168  if(useBrent)
169  {
170  alpha=2.;
171  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
172  dp=alpha*dp;
173  }
174  Warp->getParamInverse(dp,dpinv);
175  Warp->pRondp(p,dpinv,p);
176  iteration++;
177 
178  computeEvalRMS(p);
179  }
180  while(/*( erreur_prec-erreur<50) &&*/ (iteration < iterationMax)&&(evolRMS>threshold_RMS));
181 
182  nbIteration=iteration;
184 }
185 
187 {
188  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
189  x_pos.resize(nb_corners);
190  y_pos.resize(nb_corners);
191 
192  Warp->computeCoeff(p_);
193  vpTemplateTrackerTriangle triangle;
194 
195  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
196  {
197  zoneTracked->getTriangle(i, triangle);
198  for (unsigned int j=0; j<3; j++) {
199  triangle.getCorner(j, X1[0], X1[1]);
200 
201  Warp->computeDenom(X1,p_);
202  Warp->warpX(X1,X2,p_);
203  x_pos[i*3+j]=X2[0];
204  y_pos[i*3+j]=X2[1];
205  }
206  }
207 }
208 
210 {
211  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
212 
213  Warp->computeCoeff(p_);
214  evolRMS=0;
215  vpTemplateTrackerTriangle triangle;
216 
217  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
218  {
219  zoneTracked->getTriangle(i, triangle);
220  for (unsigned int j=0; j<3; j++) {
221  triangle.getCorner(j, X1[0], X1[1]);
222 
223  Warp->computeDenom(X1,p_);
224  Warp->warpX(X1,X2,p_);
225  evolRMS+=(x_pos[i*3+j]-X2[0])*(x_pos[i*3+j]-X2[0])+(y_pos[i*3+j]-X2[1])*(y_pos[i*3+j]-X2[1]);
226  x_pos[i*3+j]=X2[0];
227  y_pos[i*3+j]=X2[1];
228  }
229  }
230  evolRMS=evolRMS/nb_corners;
231 }
232 
234 {
235 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:183
void getTriangle(unsigned int i, vpTemplateTrackerTriangle &T) const
unsigned int getWidth() const
Definition: vpImage.h:159
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)
Type getValue(double i, double j) const
Definition: vpImage.h:1029
vpColVector getCorner(unsigned int i) const
vpImage< double > BI
virtual void getParamInverse(const vpColVector &ParamM, vpColVector &ParamMinv) const =0
unsigned int templateSize
unsigned int iterationMax
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &pres) const =0
unsigned int getNbTriangle() const
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
virtual void getdW0(const int &i, const int &j, const double &dy, const double &dx, double *dIdW)=0
unsigned int nbIteration
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
vpTemplateTrackerZone * zoneTracked
unsigned int getHeight() const
Definition: vpImage.h:150
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:4051