ViSP  2.10.0
vpTemplateTrackerWarp.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerWarp.cpp 4782 2014-07-15 13:04:19Z 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  if (p.size() < 2) {
47  vpCTRACE << "Bad template tracker warp parameters dimension. Should never occur. " << std::endl;
48  throw(vpException(vpException::dimensionError, "Bad template tracker warp parameters dimension"));
49  }
50  vpColVector S1(2),S2(2),S3(2);
51  vpColVector rS1(2),rS2(2),rS3(2);
52  in.getCorners(S1,S2,S3);
53  computeDenom(S1,p);
54  warpX(S1,rS1,p);
55  computeDenom(S2,p);
56  warpX(S2,rS2,p);
57  computeDenom(S3,p);
58  warpX(S3,rS3,p);
59  out.init(rS1,rS2,rS3);
60 }
62 {
64  out.clear();
65  for(unsigned int i=0;i<in.getNbTriangle();i++)
66  {
67  in.getTriangle(i,TR);
68  warpTriangle(TR,p,TT);
69  out.add(TT);
70  }
71 }
72 
74 {
75  unsigned int nb_corners = Z.getNbTriangle() * 3;
76  computeCoeff(p);
77  vpColVector X1(2),X2(2);
78 
79  double res=0;
81  for(unsigned int i=0;i<Z.getNbTriangle();i++)
82  {
83  Z.getTriangle(i, triangle);
84  for (unsigned int j=0; j<3; j++) {
85  triangle.getCorner(j, X1[0], X1[1]);
86 
87  computeDenom(X1,p);
88  warpX(X1,X2,p);
89  res+=sqrt((X2[0]-X1[0])*(X2[0]-X1[0])+(X2[1]-X1[1])*(X2[1]-X1[1]));
90  }
91  }
92 
93  return res/nb_corners;
94 }
95 
96 void vpTemplateTrackerWarp::warp(const double *ut0,const double *vt0,int nb_pt,const vpColVector& p,double *u,double *v)
97 {
98  computeCoeff(p);
99  vpColVector X1(2),X2(2);
100  for(int i=0;i<nb_pt;i++)
101  {
102  X1[0]=ut0[i];
103  X1[1]=vt0[i];
104  computeDenom(X1,p);
105  warpX(X1,X2,p);
106  u[i]=X2[0];
107  v[i]=X2[1];
108  //std::cout<<"warp "<<X2[0]<<","<<X2[1]<<std::endl;
109  }
110 }
111 
112 #ifndef DOXYGEN_SHOULD_SKIP_THIS
113 void vpTemplateTrackerWarp::findWarp(const double *ut0,const double *vt0,const double *u,const double *v,int nb_pt,vpColVector& p)
114 {
115  vpMatrix dW_(2,nbParam);
116  vpMatrix dX(2,1);
118  vpMatrix G(nbParam,1);
119 
120  int cpt=0;
121  vpColVector X1(2);
122  vpColVector fX1(2);
123  vpColVector X2(2);
124  double erreur=0;
125  double erreur_prec;
126  double lambda=0.01;
127  do
128  {
129  erreur_prec=erreur;
130  H=0;
131  G=0;
132  erreur=0;
133  computeCoeff(p);
134  for(int i=0;i<nb_pt;i++)
135  {
136  X1[0]=ut0[i];
137  X1[1]=vt0[i];
138  computeDenom(X1,p);
139  warpX(X1,fX1,p);
140  dWarp(X1,fX1,p,dW_);
141  H+=dW_.AtA();
142 
143  X2[0]=u[i];
144  X2[1]=v[i];
145 
146  dX=X2-fX1;
147  G+=dW_.t()*dX;
148 
149  erreur+=((u[i]-fX1[0])*(u[i]-fX1[0])+(v[i]-fX1[1])*(v[i]-fX1[1]));
150 
151  }
152 
153  vpMatrix::computeHLM(H, lambda, HLM);
154  try{
155  p+=HLM.inverseByLU()*G;
156  }
157  catch(vpException &e) {
158  //std::cout<<"Cannot inverse the matrix by LU " << std::endl;
159  throw(e);
160  }
161  cpt++;
162  }
163  while((cpt<150)&&(sqrt((erreur_prec-erreur)*(erreur_prec-erreur))>1e-20));
164  //std::cout<<"erreur apres transformation="<<erreur<<std::endl;
165 }
166 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
167 
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)
error that can be emited by ViSP classes.
Definition: vpException.h:76
vpColVector getCorner(unsigned int i) const
void getCorners(vpColVector &c1, vpColVector &c2, vpColVector &c3) const
unsigned int size() const
Definition: vpColVector.h:204
#define vpCTRACE
Definition: vpDebug.h:341
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:4320
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