ViSP  2.9.0
vpTemplateTrackerWarp.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerWarp.cpp 4649 2014-02-07 14:57:11Z 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/vpTemplateTrackerWarp.h>
43 
45 {
46  vpColVector S1(2),S2(2),S3(2);
47  vpColVector rS1(2),rS2(2),rS3(2);
48  in.getCorners(S1,S2,S3);
49  computeDenom(S1,p);
50  warpX(S1,rS1,p);
51  computeDenom(S2,p);
52  warpX(S2,rS2,p);
53  computeDenom(S3,p);
54  warpX(S3,rS3,p);
55  out.init(rS1,rS2,rS3);
56 }
58 {
60  out.clear();
61  for(unsigned int i=0;i<in.getNbTriangle();i++)
62  {
63  in.getTriangle(i,TR);
64  warpTriangle(TR,p,TT);
65  out.add(TT);
66  }
67 }
68 
70 {
71  unsigned int nb_corners = Z.getNbTriangle() * 3;
72  computeCoeff(p);
73  vpColVector X1(2),X2(2);
74 
75  double res=0;
77  for(unsigned int i=0;i<Z.getNbTriangle();i++)
78  {
79  Z.getTriangle(i, triangle);
80  for (unsigned int j=0; j<3; j++) {
81  triangle.getCorner(j, X1[0], X1[1]);
82 
83  computeDenom(X1,p);
84  warpX(X1,X2,p);
85  res+=sqrt((X2[0]-X1[0])*(X2[0]-X1[0])+(X2[1]-X1[1])*(X2[1]-X1[1]));
86  }
87  }
88 
89  return res/nb_corners;
90 }
91 
92 void vpTemplateTrackerWarp::warp(const double *ut0,const double *vt0,int nb_pt,const vpColVector& p,double *u,double *v)
93 {
94  computeCoeff(p);
95  vpColVector X1(2),X2(2);
96  for(int i=0;i<nb_pt;i++)
97  {
98  X1[0]=ut0[i];
99  X1[1]=vt0[i];
100  computeDenom(X1,p);
101  warpX(X1,X2,p);
102  u[i]=X2[0];
103  v[i]=X2[1];
104  //std::cout<<"warp "<<X2[0]<<","<<X2[1]<<std::endl;
105  }
106 }
107 
108 #ifndef DOXYGEN_SHOULD_SKIP_THIS
109 void vpTemplateTrackerWarp::findWarp(const double *ut0,const double *vt0,const double *u,const double *v,int nb_pt,vpColVector& p)
110 {
111  vpMatrix dW_(2,nbParam);
112  vpMatrix dX(2,1);
114  vpMatrix G(nbParam,1);
115 
116  int cpt=0;
117  vpColVector X1(2);
118  vpColVector fX1(2);
119  vpColVector X2(2);
120  double erreur=0;
121  double erreur_prec;
122  double lambda=0.01;
123  do
124  {
125  erreur_prec=erreur;
126  H=0;
127  G=0;
128  erreur=0;
129  computeCoeff(p);
130  for(int i=0;i<nb_pt;i++)
131  {
132  X1[0]=ut0[i];
133  X1[1]=vt0[i];
134  computeDenom(X1,p);
135  warpX(X1,fX1,p);
136  dWarp(X1,fX1,p,dW_);
137  H+=dW_.AtA();
138 
139  X2[0]=u[i];
140  X2[1]=v[i];
141 
142  dX=X2-fX1;
143  G+=dW_.t()*dX;
144 
145  erreur+=((u[i]-fX1[0])*(u[i]-fX1[0])+(v[i]-fX1[1])*(v[i]-fX1[1]));
146 
147  }
148 
149  vpMatrix::computeHLM(H, lambda, HLM);
150  try{
151  p+=HLM.inverseByLU()*G;
152  }
153  catch(...) {
154  std::cout<<"Cannot inverse the matrix by LU " << std::endl;
155  break;
156  }
157  cpt++;
158  }
159  while((cpt<150)&&(sqrt((erreur_prec-erreur)*(erreur_prec-erreur))>1e-20));
160  //std::cout<<"erreur apres transformation="<<erreur<<std::endl;
161 }
162 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
163 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void getTriangle(unsigned int i, vpTemplateTrackerTriangle &T) const
void warpTriangle(const vpTemplateTrackerTriangle &in, const vpColVector &p, vpTemplateTrackerTriangle &out)
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
double getDistanceBetweenZoneAndWarpedZone(const vpTemplateTrackerZone &Z, const vpColVector &p)
vpColVector getCorner(unsigned int i) const
void getCorners(vpColVector &c1, vpColVector &c2, vpColVector &c3) const
void add(const vpTemplateTrackerTriangle &t)
unsigned int getNbTriangle() const
void warpZone(const vpTemplateTrackerZone &in, const vpColVector &p, vpTemplateTrackerZone &out)
void init(const vpColVector &c1, const vpColVector &c2, const vpColVector &c3)
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
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:4051
void warp(const double *ut0, const double *vt0, int nb_pt, const vpColVector &p, double *u, double *v)
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0