ViSP  2.9.0
vpTemplateTrackerWarpHomography.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerWarpHomography.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/vpTemplateTrackerWarpHomography.h>
43 #include <visp/vpTrackingException.h>
44 
46 {
47  nbParam = 8 ;
48  dW.resize(2,nbParam);
49 }
50 
51 //get the parameter corresponding to the lower level of a gaussian pyramid
53 {
54  pdown=p;
55  pdown[2]=p[2]*2.;
56  pdown[5]=p[5]*2.;
57  pdown[6]=p[6]/2.;
58  pdown[7]=p[7]/2.;
59 }
60 
62 {
63  pup=p;
64  pup[2]=p[2]/2.;
65  pup[5]=p[5]/2.;
66  pup[6]=p[6]*2.;
67  pup[7]=p[7]*2.;
68 }
69 
70 /*calcul de di*dw(x,p0)/dp */
71 void vpTemplateTrackerWarpHomography::getdW0(const int &i,const int &j,const double &dy,const double &dx,double *dIdW)
72 {
73  dIdW[0]=j*dx;
74  dIdW[1]=j*dy;
75  dIdW[2]=-j*j*dx-i*j*dy;
76  dIdW[3]=i*dx;
77  dIdW[4]=i*dy;
78  dIdW[5]=-i*j*dx-i*i*dy;
79  dIdW[6]=dx;
80  dIdW[7]=dy;
81 }
82 /*calcul de dw(x,p0)/dp */
83 void vpTemplateTrackerWarpHomography::getdWdp0(const int &i,const int &j,double *dIdW)
84 {
85  dIdW[0]=j;
86  dIdW[1]=0;
87  dIdW[2]=-j*j;
88  dIdW[3]=i;
89  dIdW[4]=0;
90  dIdW[5]=-i*j;
91  dIdW[6]=1.;
92  dIdW[7]=0;
93 
94  dIdW[8]=0;
95  dIdW[9]=j;
96  dIdW[10]=-i*j;
97  dIdW[11]=0;
98  dIdW[12]=i;
99  dIdW[13]=-i*i;
100  dIdW[14]=0;
101  dIdW[15]=1.;
102 }
104 {
105  denom=(1./(ParamM[2]*vX[0]+ParamM[5]*vX[1]+1.));
106 }
107 
108 void vpTemplateTrackerWarpHomography::warpX(const int &i,const int &j,double &i2,double &j2,const vpColVector &ParamM)
109 {
110  j2=((1.+ParamM[0])*j+ParamM[3]*i+ParamM[6])*denom;
111  i2=(ParamM[1]*j+(1.+ParamM[4])*i+ParamM[7])*denom;
112 }
113 
114 
116 {
117  //if((ParamM[2]*vX[0]+ParamM[5]*vX[1]+1)>0)//si dans le plan image reel
118  if((denom)>0)// FS optimisation
119  {
120  vXres[0]=((1+ParamM[0])*vX[0]+ParamM[3]*vX[1]+ParamM[6])*denom;
121  vXres[1]=(ParamM[1]*vX[0]+(1+ParamM[4])*vX[1]+ParamM[7])*denom;
122  }
123  else
124  throw(vpTrackingException(vpTrackingException::fatalError,"Division by zero in vpTemplateTrackerWarpHomography::warpX()"));
125 }
126 
128 {
129  double j=X1[0];
130  double i=X1[1];
131  dW_=0;
132  dW_[0][0]=j*denom;
133  dW_[0][2]=-j*X2[0]*denom;
134  dW_[0][3]=i*denom;
135  dW_[0][5]=-i*X2[0]*denom;
136  dW_[0][6]=denom;
137 
138  dW_[1][1]=j*denom;
139  dW_[1][2]=-j*X2[1]*denom;
140  dW_[1][4]=i*denom;
141  dW_[1][5]=-i*X2[1]*denom;
142  dW_[1][7]=denom;
143 }
144 
145 /*compute dw=dw/dx*dw/dp */
147  const double *dwdp0,vpMatrix &dW_)
148 {
149  double dwdx0,dwdx1;
150  double dwdy0,dwdy1;
151 
152  dwdx0=((1.+ParamM[0])-X2[0]*ParamM[2])*denom;
153  dwdx1=(ParamM[1]-X2[1]*ParamM[2])*denom;
154  dwdy0=(ParamM[3]-X2[0]*ParamM[5])*denom;
155  dwdy1=((1.+ParamM[4])-X2[1]*ParamM[5])*denom;
156  for(unsigned int i=0;i<nbParam;i++)
157  {
158  dW_[0][i]=dwdx0*dwdp0[i]+dwdy0*dwdp0[i+nbParam];
159  dW_[1][i]=dwdx1*dwdp0[i]+dwdy1*dwdp0[i+nbParam];
160  }
161 }
162 
164 {
165 
166  if((ParamM[2]*vX[0]+ParamM[5]*vX[1]+1)<0)//si dans le plan image reel
167  {
168  vXres[0]=((1+ParamM[0])*vX[0]+ParamM[3]*vX[1]+ParamM[6])/(ParamM[2]*vX[0]+ParamM[5]*vX[1]+1);
169  vXres[1]=(ParamM[1]*vX[0]+(1+ParamM[4])*vX[1]+ParamM[7])/(ParamM[2]*vX[0]+ParamM[5]*vX[1]+1);
170  }
171  else
172  throw(vpTrackingException(vpTrackingException::fatalError,"Division by zero in vpTemplateTrackerWarpHomography::warpXSpecialInv()")) ;
173 }
175 {
176  vpHomography H = getHomography(ParamM);
177  vpHomography Hinv = H.inverse();
178  getParam(Hinv, ParamMinv);
179 }
180 
181 
183 {
184  vpHomography H;
185  for(unsigned int i=0;i<3;i++)
186  for(unsigned int j=0;j<3;j++)
187  {
188  if(i+3*j!=8)
189  {
190  H[i][j]=ParamM[i+3*j];
191  if(i==j)H[i][j]++;
192  }
193  else
194  H[i][j]=1.;
195  }
196 
197  return H;
198 }
200 {
201  par=0;
202  for(unsigned int i=0;i<3;i++)
203  for(unsigned int j=0;j<3;j++)
204  {
205  if(i+3*j!=8)
206  {
207  par[i+3*j]=H[i][j]/H[2][2];
208  if(i==j)par[i+3*j]--;
209  }
210  }
211 }
212 
213 
214 
216 {
217  vpHomography H1 = getHomography(p1);
218  vpHomography H2 = getHomography(p2);
219  vpHomography H = H1*H2;
220  getParam(H,pres);
221 }
void getdW0(const int &i, const int &j, const double &dy, const double &dx, double *dIdW)
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 getParamPyramidDown(const vpColVector &p, vpColVector &pdown)
void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW)
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
Error that can be emited by the vpTracker class and its derivates.
void getParamInverse(const vpColVector &ParamM, vpColVector &ParamMinv) const
void computeDenom(vpColVector &vX, const vpColVector &ParamM)
void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)
void getdWdp0(const int &i, const int &j, double *dIdW)
void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &pres) const
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomography inverse() const
invert the homography
void warpX(const vpColVector &vX, vpColVector &vXres, const vpColVector &ParamM)
void getParam(const vpHomography &H, vpColVector &par) const
void getParamPyramidUp(const vpColVector &p, vpColVector &pup)
void warpXInv(const vpColVector &vX, vpColVector &vXres, const vpColVector &ParamM)
vpHomography getHomography(const vpColVector &ParamM) const