Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTemplateTrackerSSDInverseCompositional.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/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  if(blur)
113 
114  vpColVector dpinv(nbParam);
115  double IW;
116  double Tij;
117  unsigned int iteration=0;
118  int i,j;
119  double i2,j2;
120  double alpha=2.;
121  //vpTemplateTrackerPointtest *pt;
122  initPosEvalRMS(p);
123 
125  do
126  {
127  unsigned int Nbpoint=0;
128  double erreur=0;
129  dp=0;
130  Warp->computeCoeff(p);
131  for(unsigned int point=0;point<templateSize;point++)
132  {
133  if((!useTemplateSelect)||(ptTemplateSelect[point]))
134  {
135  //pt=&ptTemplatetest[point];
136  pt=&ptTemplate[point];
137  i=pt->y;
138  j=pt->x;
139  X1[0]=j;X1[1]=i;
140  Warp->computeDenom(X1,p);
141  Warp->warpX(X1,X2,p);
142  j2=X2[0];i2=X2[1];
143 
144  if((i2>=0)&&(j2>=0)&&(i2<I.getHeight()-1)&&(j2<I.getWidth()-1))
145  {
146  Tij=pt->val;
147  if(!blur)
148  IW=I.getValue(i2,j2);
149  else
150  IW=BI.getValue(i2,j2);
151  Nbpoint++;
152  double er=(Tij-IW);
153  for(unsigned int it=0;it<nbParam;it++)
154  dp[it]+=er*pt->HiG[it];
155 
156  erreur+=er*er;
157  }
158  }
159  }
160  //std::cout << "npoint: " << Nbpoint << std::endl;
161  if(Nbpoint==0) {
162  //std::cout<<"plus de point dans template suivi"<<std::endl;
164  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
165  }
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  //std::cout << "iteration: " << iteration << " max: " << iterationMax << std::endl;
180  //std::cout << "evolRMS: " << evolRMS << " threshold: " << threshold_RMS << std::endl;
181  }
182  while(/*( erreur_prec-erreur<50) &&*/ (iteration < iterationMax)&&(evolRMS>threshold_RMS));
183 
184  nbIteration=iteration;
186 }
187 
189 {
190  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
191  x_pos.resize(nb_corners);
192  y_pos.resize(nb_corners);
193 
194  Warp->computeCoeff(p_);
195  vpTemplateTrackerTriangle triangle;
196 
197  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
198  {
199  zoneTracked->getTriangle(i, triangle);
200  for (unsigned int j=0; j<3; j++) {
201  triangle.getCorner(j, X1[0], X1[1]);
202 
203  Warp->computeDenom(X1,p_);
204  Warp->warpX(X1,X2,p_);
205  x_pos[i*3+j]=X2[0];
206  y_pos[i*3+j]=X2[1];
207  }
208  }
209 }
210 
212 {
213  unsigned int nb_corners = zoneTracked->getNbTriangle() * 3;
214 
215  Warp->computeCoeff(p_);
216  evolRMS=0;
217  vpTemplateTrackerTriangle triangle;
218 
219  for(unsigned int i=0;i<zoneTracked->getNbTriangle();i++)
220  {
221  zoneTracked->getTriangle(i, triangle);
222  for (unsigned int j=0; j<3; j++) {
223  triangle.getCorner(j, X1[0], X1[1]);
224 
225  Warp->computeDenom(X1,p_);
226  Warp->warpX(X1,X2,p_);
227  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]);
228  x_pos[i*3+j]=X2[0];
229  y_pos[i*3+j]=X2[1];
230  }
231  }
232  evolRMS=evolRMS/nb_corners;
233 }
234 
236 {
237 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void getTriangle(unsigned int i, vpTemplateTrackerTriangle &T) const
unsigned int getWidth() const
Definition: vpImage.h:226
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:1477
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
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: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