Visual Servoing Platform  version 3.0.0
vpTemplateTrackerSSDInverseCompositional.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/vpTemplateTrackerSSDInverseCompositional.h>
40 #include <visp3/core/vpImageTools.h>
41 
43  : vpTemplateTrackerSSD(warp), compoInitialised(false), HInv(), HCompInverse(), useTemplateSelect(false),
44  evolRMS(0), x_pos(), y_pos(), threshold_RMS(1e-8)
45 {
46  useInverse=true;
49 }
50 
52 {
53 
54  H=0;
55  int i,j;
56 
57  for(unsigned int point=0;point<templateSize;point++)
58  {
59  if((!useTemplateSelect)||(ptTemplateSelect[point]))
60  {
61  i=ptTemplate[point].y;
62  j=ptTemplate[point].x;
63  X1[0]=j;X1[1]=i;
64  Warp->computeDenom(X1,p);
65  ptTemplate[point].dW=new double[nbParam];
66 
67  Warp->getdW0(i,j,ptTemplate[point].dy,ptTemplate[point].dx,ptTemplate[point].dW);
68 
69  for(unsigned int it=0;it<nbParam;it++)
70  for(unsigned int jt=0;jt<nbParam;jt++)
71  H[it][jt]+=ptTemplate[point].dW[it]*ptTemplate[point].dW[jt];
72  }
73 
74  }
75  HInv=H;
76  vpMatrix HLMtemp(nbParam,nbParam);
78 
80  HCompInverse=HLMtemp.inverseByLU();
81  //std::cout<<Hinverse<<std::endl;
82  vpColVector dWtemp(nbParam);
83  vpColVector HiGtemp(nbParam);
84 
85  for(unsigned int point=0;point<templateSize;point++)
86  {
87  if((!useTemplateSelect)||(ptTemplateSelect[point]))
88  {
89  i=ptTemplate[point].y;
90  j=ptTemplate[point].x;
91  for(unsigned int it=0;it<nbParam;it++)
92  dWtemp[it]=ptTemplate[point].dW[it];
93 
94  HiGtemp = -1.*HCompInverse*dWtemp;
95  ptTemplate[point].HiG=new double[nbParam];
96 
97  for(unsigned int it=0;it<nbParam;it++)
98  ptTemplate[point].HiG[it]=HiGtemp[it];
99  }
100  }
101  compoInitialised=true;
102 }
103 
105 {
106  initCompInverse(I);
107 }
108 
110 {
111  double erreur=0;
112  unsigned int Nbpoint=0;
113  if(blur)
115 
116  vpColVector dpinv(nbParam);
117  double IW;
118  double Tij;
119  unsigned int iteration=0;
120  int i,j;
121  double i2,j2;
122  double alpha=2.;
123  //vpTemplateTrackerPointtest *pt;
124  initPosEvalRMS(p);
125 
127  do
128  {
129  Nbpoint=0;
130  erreur=0;
131  dp=0;
132  Warp->computeCoeff(p);
133  for(unsigned int point=0;point<templateSize;point++)
134  {
135  if((!useTemplateSelect)||(ptTemplateSelect[point]))
136  {
137  //pt=&ptTemplatetest[point];
138  pt=&ptTemplate[point];
139  i=pt->y;
140  j=pt->x;
141  X1[0]=j;X1[1]=i;
142  Warp->computeDenom(X1,p);
143  Warp->warpX(X1,X2,p);
144  j2=X2[0];i2=X2[1];
145 
146  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
147  {
148  Tij=pt->val;
149  if(!blur)
150  IW=I.getValue(i2,j2);
151  else
152  IW=BI.getValue(i2,j2);
153  Nbpoint++;
154  double er=(Tij-IW);
155  for(unsigned int it=0;it<nbParam;it++)
156  dp[it]+=er*pt->HiG[it];
157 
158  erreur+=er*er;
159  }
160  }
161  }
162  //std::cout << "npoint: " << Nbpoint << std::endl;
163  if(Nbpoint==0) {
164  //std::cout<<"plus de point dans template suivi"<<std::endl;
166  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
167  }
168  dp=gain*dp;
169  //std::cout<<erreur/Nbpoint<<","<<GetCost(I,p)<<std::endl;
170  if(useBrent)
171  {
172  alpha=2.;
173  computeOptimalBrentGain(I,p,erreur/Nbpoint,dp,alpha);
174  dp=alpha*dp;
175  }
176  Warp->getParamInverse(dp,dpinv);
177  Warp->pRondp(p,dpinv,p);
178  iteration++;
179 
180  computeEvalRMS(p);
181  //std::cout << "iteration: " << iteration << " max: " << iterationMax << std::endl;
182  //std::cout << "evolRMS: " << evolRMS << " threshold: " << threshold_RMS << std::endl;
183  }
184  while(/*( erreur_prec-erreur<50) &&*/ (iteration < iterationMax)&&(evolRMS>threshold_RMS));
185 
186  nbIteration=iteration;
188 }
189 
191 {
192  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
193  x_pos.resize(nb_corners);
194  y_pos.resize(nb_corners);
195 
196  Warp->computeCoeff(p_);
197  vpTemplateTrackerTriangle triangle;
198 
199  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
200  {
201  zoneTracked->getTriangle(i, triangle);
202  for (unsigned int j=0; j<3; j++) {
203  triangle.getCorner(j, X1[0], X1[1]);
204 
205  Warp->computeDenom(X1,p_);
206  Warp->warpX(X1,X2,p_);
207  x_pos[i*3+j]=X2[0];
208  y_pos[i*3+j]=X2[1];
209  }
210  }
211 }
212 
214 {
215  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
216 
217  Warp->computeCoeff(p_);
218  evolRMS=0;
219  vpTemplateTrackerTriangle triangle;
220 
221  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
222  {
223  zoneTracked->getTriangle(i, triangle);
224  for (unsigned int j=0; j<3; j++) {
225  triangle.getCorner(j, X1[0], X1[1]);
226 
227  Warp->computeDenom(X1,p_);
228  Warp->warpX(X1,X2,p_);
229  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]);
230  x_pos[i*3+j]=X2[0];
231  y_pos[i*3+j]=X2[1];
232  }
233  }
234  evolRMS=evolRMS/nb_corners;
235 }
236 
238 {
239 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
void getTriangle(unsigned int i, vpTemplateTrackerTriangle &T) const
unsigned int getWidth() const
Definition: vpImage.h:161
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
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:1148
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
Error that can be emited by the vpTracker class and its derivates.
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
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
vpTemplateTrackerZone * zoneTracked
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