Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpTemplateTrackerZNCCForwardAdditional.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/vpImageFilter.h>
41 #include <visp3/tt/vpTemplateTrackerZNCCForwardAdditional.h>
42 
44  : vpTemplateTrackerZNCC(warp)
45 {
46  useCompositionnal = false;
47 }
48 
50 {
51  if (blur)
55 
56  vpImage<double> dIxx, dIxy, dIyx, dIyy;
59 
62 
63  Warp->computeCoeff(p);
64  double IW, dIWx, dIWy;
65  double Tij;
66  int i, j;
67  double i2, j2;
68  int Nbpoint = 0;
69 
70  double moyTij = 0;
71  double moyIW = 0;
72  double denom = 0;
73  for (unsigned int point = 0; point < templateSize; point++) {
74  i = ptTemplate[point].y;
75  j = ptTemplate[point].x;
76  X1[0] = j;
77  X1[1] = i;
78  X2[0] = j;
79  X2[1] = i;
80 
81  Warp->computeDenom(X1, p);
82 
83  j2 = X2[0];
84  i2 = X2[1];
85 
86  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
87  Tij = ptTemplate[point].val;
88 
89  if (!blur)
90  IW = I.getValue(i2, j2);
91  else
92  IW = BI.getValue(i2, j2);
93 
94  Nbpoint++;
95  moyTij += Tij;
96  moyIW += IW;
97  }
98  }
99  moyTij = moyTij / Nbpoint;
100  moyIW = moyIW / Nbpoint;
101  Hdesire = 0;
102  for (unsigned int point = 0; point < templateSize; point++) {
103  i = ptTemplate[point].y;
104  j = ptTemplate[point].x;
105  X1[0] = j;
106  X1[1] = i;
107  X2[0] = j;
108  X2[1] = i;
109 
110  Warp->computeDenom(X1, p);
111 
112  j2 = X2[0];
113  i2 = X2[1];
114 
115  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
116  Tij = ptTemplate[point].val;
117 
118  if (!blur)
119  IW = I.getValue(i2, j2);
120  else
121  IW = BI.getValue(i2, j2);
122 
123  dIWx = dIx.getValue(i2, j2);
124  dIWy = dIy.getValue(i2, j2);
125  // Calcul du Hessien
126  Warp->dWarp(X1, X2, p, dW);
127  double *tempt = new double[nbParam];
128  for (unsigned int it = 0; it < nbParam; it++)
129  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
130 
131  double prod = (Tij - moyTij);
132 
133  double d_Ixx = dIxx.getValue(i2, j2);
134  double d_Iyy = dIyy.getValue(i2, j2);
135  double d_Ixy = dIxy.getValue(i2, j2);
136 
137  for (unsigned int it = 0; it < nbParam; it++)
138  for (unsigned int jt = 0; jt < nbParam; jt++)
139  Hdesire[it][jt] += prod * (dW[0][it] * (dW[0][jt] * d_Ixx + dW[1][jt] * d_Ixy) +
140  dW[1][it] * (dW[0][jt] * d_Ixy + dW[1][jt] * d_Iyy));
141  denom += (Tij - moyTij) * (Tij - moyTij) * (IW - moyIW) * (IW - moyIW);
142  delete[] tempt;
143  }
144  }
145 
146  Hdesire = Hdesire / sqrt(denom);
149  // std::cout<<"Hdesire = "<<Hdesire<<std::endl;
150 }
151 
153 {
154  if (blur)
158 
159  dW = 0;
160 
161  double IW, dIWx, dIWy;
162  double Tij;
163  unsigned int iteration = 0;
164  int i, j;
165  double i2, j2;
166  double alpha = 2.;
167 
168  initPosEvalRMS(p);
169 
170  double evolRMS_init = 0;
171  double evolRMS_prec = 0;
172  double evolRMS_delta;
173 
174  do {
175  int Nbpoint = 0;
176  double erreur = 0;
177  G = 0;
178  H = 0;
179  Warp->computeCoeff(p);
180  double moyTij = 0;
181  double moyIW = 0;
182  double denom = 0;
183  for (unsigned int point = 0; point < templateSize; point++) {
184  i = ptTemplate[point].y;
185  j = ptTemplate[point].x;
186  X1[0] = j;
187  X1[1] = i;
188 
189  Warp->computeDenom(X1, p);
190  Warp->warpX(X1, X2, p);
191 
192  j2 = X2[0];
193  i2 = X2[1];
194  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
195  Tij = ptTemplate[point].val;
196 
197  if (!blur)
198  IW = I.getValue(i2, j2);
199  else
200  IW = BI.getValue(i2, j2);
201 
202  Nbpoint++;
203  moyTij += Tij;
204  moyIW += IW;
205  }
206  }
207 
208  if (!Nbpoint) {
209  throw(vpException(vpException::divideByZeroError, "Cannot track the template: no point"));
210  }
211 
212  moyTij = moyTij / Nbpoint;
213  moyIW = moyIW / Nbpoint;
214  for (unsigned int point = 0; point < templateSize; point++) {
215  i = ptTemplate[point].y;
216  j = ptTemplate[point].x;
217  X1[0] = j;
218  X1[1] = i;
219 
220  Warp->computeDenom(X1, p);
221  Warp->warpX(X1, X2, p);
222 
223  j2 = X2[0];
224  i2 = X2[1];
225  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
226  Tij = ptTemplate[point].val;
227 
228  if (!blur)
229  IW = I.getValue(i2, j2);
230  else
231  IW = BI.getValue(i2, j2);
232 
233  dIWx = dIx.getValue(i2, j2);
234  dIWy = dIy.getValue(i2, j2);
235  // Calcul du Hessien
236  Warp->dWarp(X1, X2, p, dW);
237  double *tempt = new double[nbParam];
238  for (unsigned int it = 0; it < nbParam; it++)
239  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
240 
241  double prod = (Tij - moyTij);
242  for (unsigned int it = 0; it < nbParam; it++)
243  G[it] += prod * tempt[it];
244 
245  double er = (Tij - IW);
246  erreur += (er * er);
247  denom += (Tij - moyTij) * (Tij - moyTij) * (IW - moyIW) * (IW - moyIW);
248  delete[] tempt;
249  }
250  }
251  G = G / sqrt(denom);
252  H = H / sqrt(denom);
253 
254  try {
255  dp = HLMdesireInverse * G;
256  } catch (const vpException &e) {
257  throw(e);
258  }
259 
260  dp = gain * dp;
261  if (useBrent) {
262  alpha = 2.;
263  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
264  dp = alpha * dp;
265  }
266  p -= dp;
267 
268  computeEvalRMS(p);
269 
270  if (iteration == 0) {
271  evolRMS_init = evolRMS;
272  }
273  iteration++;
274 
275  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
276  evolRMS_prec = evolRMS;
277 
278  } while ((iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps));
279 
280  // std::cout<<"erreur "<<erreur<<std::endl;
281  nbIteration = iteration;
282 }
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1422
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
vpTemplateTrackerPoint * ptTemplate
static void getGradY(const vpImage< unsigned char > &I, vpImage< double > &dIy)
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
vpMatrix inverseByLU() const
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
error that can be emited by ViSP classes.
Definition: vpException.h:71
void initPosEvalRMS(const vpColVector &p)
static void getGradYGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIy, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
void computeEvalRMS(const vpColVector &p)
static void getGradXGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIx, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
vpImage< double > BI
unsigned int templateSize
unsigned int iterationMax
vpImage< double > dIx
vpImage< double > dIy
unsigned int nbIteration
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, bool convolve=false)
unsigned int getHeight() const
Definition: vpImage.h:186
void initHessienDesired(const vpImage< unsigned char > &I)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:5453
unsigned int getWidth() const
Definition: vpImage.h:244
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0