Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpTemplateTrackerWarpHomography.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Template tracker.
33  *
34  * Authors:
35  * Amaury Dame
36  * Aurelien Yol
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 #include <visp3/core/vpTrackingException.h>
41 #include <visp3/tt/vpTemplateTrackerWarpHomography.h>
42 
44 {
45  nbParam = 8;
46  dW.resize(2, nbParam);
47 }
48 
49 // get the parameter corresponding to the lower level of a gaussian pyramid
51 {
52  pdown = p;
53  pdown[2] = p[2] * 2.;
54  pdown[5] = p[5] * 2.;
55  pdown[6] = p[6] / 2.;
56  pdown[7] = p[7] / 2.;
57 }
58 
60 {
61  pup = p;
62  pup[2] = p[2] / 2.;
63  pup[5] = p[5] / 2.;
64  pup[6] = p[6] * 2.;
65  pup[7] = p[7] * 2.;
66 }
67 
68 /*calcul de di*dw(x,p0)/dp */
69 void vpTemplateTrackerWarpHomography::getdW0(const int &i, const int &j, const double &dy, const double &dx,
70  double *dIdW)
71 {
72  double j_dx_ = j * dx;
73  double i_dy_ = i * dy;
74  dIdW[0] = j_dx_;
75  dIdW[1] = j * dy;
76  dIdW[2] = -j * j_dx_ - j * i_dy_;
77  dIdW[3] = i * dx;
78  dIdW[4] = i_dy_;
79  dIdW[5] = -i * j_dx_ - i * i_dy_;
80  dIdW[6] = dx;
81  dIdW[7] = dy;
82 }
83 /*calcul de dw(x,p0)/dp */
84 void vpTemplateTrackerWarpHomography::getdWdp0(const int &i, const int &j, double *dIdW)
85 {
86  double ij_ = i * j;
87  dIdW[0] = j;
88  dIdW[1] = 0;
89  dIdW[2] = -j * j;
90  dIdW[3] = i;
91  dIdW[4] = 0;
92  dIdW[5] = - ij_;
93  dIdW[6] = 1.;
94  dIdW[7] = 0;
95 
96  dIdW[8] = 0;
97  dIdW[9] = j;
98  dIdW[10] = - ij_;
99  dIdW[11] = 0;
100  dIdW[12] = i;
101  dIdW[13] = -i * i;
102  dIdW[14] = 0;
103  dIdW[15] = 1.;
104 }
106 {
107  double value = (ParamM[2] * vX[0] + ParamM[5] * vX[1] + 1.);
108 
109  if (std::fabs(value) > std::numeric_limits<double>::epsilon()) {
110  denom = (1. / value);
111  } else {
113  "Division by zero in vpTemplateTrackerWarpHomography::computeDenom()"));
114  }
115 }
116 
117 void vpTemplateTrackerWarpHomography::warpX(const int &i, const int &j, double &i2, double &j2,
118  const vpColVector &ParamM)
119 {
120  j2 = ((1. + ParamM[0]) * j + ParamM[3] * i + ParamM[6]) * denom;
121  i2 = (ParamM[1] * j + (1. + ParamM[4]) * i + ParamM[7]) * denom;
122 }
123 
125 {
126  vXres[0] = ((1 + ParamM[0]) * vX[0] + ParamM[3] * vX[1] + ParamM[6]) * denom;
127  vXres[1] = (ParamM[1] * vX[0] + (1 + ParamM[4]) * vX[1] + ParamM[7]) * denom;
128 }
129 
131  const vpColVector & /*ParamM*/, vpMatrix &dW_)
132 {
133  double j = X1[0];
134  double i = X1[1];
135  dW_ = 0;
136  dW_[0][0] = j * denom;
137  dW_[0][2] = -j * X2[0] * denom;
138  dW_[0][3] = i * denom;
139  dW_[0][5] = -i * X2[0] * denom;
140  dW_[0][6] = denom;
141 
142  dW_[1][1] = j * denom;
143  dW_[1][2] = -j * X2[1] * denom;
144  dW_[1][4] = i * denom;
145  dW_[1][5] = -i * X2[1] * denom;
146  dW_[1][7] = denom;
147 }
148 
149 /*compute dw=dw/dx*dw/dp */
151  const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW_)
152 {
153  double dwdx0, dwdx1;
154  double dwdy0, dwdy1;
155 
156  dwdx0 = ((1. + ParamM[0]) - X2[0] * ParamM[2]) * denom;
157  dwdx1 = (ParamM[1] - X2[1] * ParamM[2]) * denom;
158  dwdy0 = (ParamM[3] - X2[0] * ParamM[5]) * denom;
159  dwdy1 = ((1. + ParamM[4]) - X2[1] * ParamM[5]) * denom;
160  for (unsigned int i = 0; i < nbParam; i++) {
161  dW_[0][i] = dwdx0 * dwdp0[i] + dwdy0 * dwdp0[i + nbParam];
162  dW_[1][i] = dwdx1 * dwdp0[i] + dwdy1 * dwdp0[i + nbParam];
163  }
164 }
165 
167 {
168  double value = (ParamM[2] * vX[0] + ParamM[5] * vX[1] + 1.);
169 
170  if (std::fabs(value) > std::numeric_limits<double>::epsilon()) {
171  vXres[0] = ((1 + ParamM[0]) * vX[0] + ParamM[3] * vX[1] + ParamM[6]) / value;
172  vXres[1] = (ParamM[1] * vX[0] + (1 + ParamM[4]) * vX[1] + ParamM[7]) / value;
173  } else {
174  throw(vpTrackingException(vpTrackingException::fatalError, "Division by zero in "
175  "vpTemplateTrackerWarpHomography::"
176  "warpXInv()"));
177  }
178 }
180 {
181  vpHomography H = getHomography(ParamM);
182  vpHomography Hinv = H.inverse();
183  getParam(Hinv, ParamMinv);
184 }
185 
187 {
188  vpHomography H;
189  for (unsigned int i = 0; i < 3; i++)
190  for (unsigned int j = 0; j < 3; j++) {
191  if (i + 3 * j != 8) {
192  H[i][j] = ParamM[i + 3 * j];
193  if (i == j)
194  H[i][j]++;
195  } else
196  H[i][j] = 1.;
197  }
198 
199  return H;
200 }
202 {
203  par = 0;
204  for (unsigned int i = 0; i < 3; i++)
205  for (unsigned int j = 0; j < 3; j++) {
206  if (i + 3 * j != 8) {
207  par[i + 3 * j] = H[i][j] / H[2][2];
208  if (i == j)
209  par[i + 3 * j]--;
210  }
211  }
212 }
213 
215 {
216  vpHomography H1 = getHomography(p1);
217  vpHomography H2 = getHomography(p2);
218  vpHomography H = H1 * H2;
219  getParam(H, pres);
220 }
void getdW0(const int &i, const int &j, const double &dy, const double &dx, double *dIdW)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
void getParamPyramidDown(const vpColVector &p, vpColVector &pdown)
void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
Error that can be emited by the vpTracker class and its derivates.
void computeDenom(vpColVector &vX, const vpColVector &ParamM)
vpHomography inverse() const
invert the homography
void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)
void getdWdp0(const int &i, const int &j, double *dIdW)
void getParamInverse(const vpColVector &ParamM, vpColVector &ParamMinv) const
void getParam(const vpHomography &H, vpColVector &par) const
void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &pres) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void warpX(const vpColVector &vX, vpColVector &vXres, const vpColVector &ParamM)
void getParamPyramidUp(const vpColVector &p, vpColVector &pup)
void warpXInv(const vpColVector &vX, vpColVector &vXres, const vpColVector &ParamM)
vpHomography getHomography(const vpColVector &ParamM) const