Visual Servoing Platform  version 3.2.1 under development (2019-10-21) under development (2019-10-21)
vpTemplateTrackerSSDForwardAdditional.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 
41 #include <limits> // numeric_limits
42 
43 #include <visp3/core/vpImageTools.h>
44 #include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
45 
47  : vpTemplateTrackerSSD(warp), minimizationMethod(USE_NEWTON), p_prec(), G_prec(), KQuasiNewton()
48 {
49  useCompositionnal = false;
50 }
51 
53 {
54  if (blur)
58 
59  dW = 0;
60 
61  double lambda = lambdaDep;
62  double IW, dIWx, dIWy;
63  double Tij;
64  unsigned int iteration = 0;
65  int i, j;
66  double i2, j2;
67  double alpha = 2.;
68 
70 
71  double evolRMS_init = 0;
72  double evolRMS_prec = 0;
73  double evolRMS_delta;
74 
75  do {
76  unsigned int Nbpoint = 0;
77  double erreur = 0;
78  G = 0;
79  H = 0;
80  Warp->computeCoeff(p);
81  for (unsigned int point = 0; point < templateSize; point++) {
82  i = ptTemplate[point].y;
83  j = ptTemplate[point].x;
84  X1[0] = j;
85  X1[1] = i;
86 
87  Warp->computeDenom(X1, p);
88  Warp->warpX(X1, X2, p);
89 
90  j2 = X2[0];
91  i2 = X2[1];
92  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
93  Tij = ptTemplate[point].val;
94 
95  if (!blur)
96  IW = I.getValue(i2, j2);
97  else
98  IW = BI.getValue(i2, j2);
99 
100  dIWx = dIx.getValue(i2, j2);
101  dIWy = dIy.getValue(i2, j2);
102  Nbpoint++;
103  // Calcul du Hessien
104  Warp->dWarp(X1, X2, p, dW);
105  double *tempt = new double[nbParam];
106  for (unsigned int it = 0; it < nbParam; it++)
107  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
108 
109  for (unsigned int it = 0; it < nbParam; it++)
110  for (unsigned int jt = 0; jt < nbParam; jt++)
111  H[it][jt] += tempt[it] * tempt[jt];
112 
113  double er = (Tij - IW);
114  for (unsigned int it = 0; it < nbParam; it++)
115  G[it] += er * tempt[it];
116 
117  erreur += (er * er);
118  delete[] tempt;
119  }
120  }
121  if (Nbpoint == 0) {
122  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
123  }
124 
125  vpMatrix::computeHLM(H, lambda, HLM);
126  try {
127  dp = HLM.inverseByLU() * G;
128  } catch (const vpException &e) {
129  throw(e);
130  }
131 
132  switch (minimizationMethod) {
134  vpColVector p_test_LMA(nbParam);
135  p_test_LMA = p + dp;
136  erreur = -getCost(I, p);
137  double erreur_LMA = -getCost(I, p_test_LMA);
138  if (erreur_LMA < erreur) {
139  p = p_test_LMA;
140  lambda = (lambda / 10. < 1e-6) ? lambda / 10. : 1e-6;
141  } else {
142  lambda = (lambda * 10. < 1e6) ? 1e6 : lambda * 10.;
143  }
144  } break;
146  dp = gain * 0.000001 * G;
147  if (useBrent) {
148  alpha = 2.;
149  computeOptimalBrentGain(I, p, erreur, dp, alpha);
150  dp = alpha * dp;
151  }
152  p += dp;
153  break;
154  }
155 
157  if (iterationGlobale != 0) {
158  vpColVector s_quasi = p - p_prec;
159  vpColVector y_quasi = G - G_prec;
160  double s_scal_y = s_quasi.t() * y_quasi;
161  if (std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) // DFP
162  KQuasiNewton = KQuasiNewton + 0.001 * (s_quasi * s_quasi.t() / s_scal_y -
163  KQuasiNewton * y_quasi * y_quasi.t() * KQuasiNewton /
164  (y_quasi.t() * KQuasiNewton * y_quasi));
165  }
166  dp = -KQuasiNewton * G;
167  p_prec = p;
168  G_prec = G;
169  p -= 1.01 * dp;
170  } break;
171 
173  default: {
174  if (useBrent) {
175  alpha = 2.;
176  computeOptimalBrentGain(I, p, erreur, dp, alpha);
177  dp = alpha * dp;
178  }
179 
180  p += dp;
181  break;
182  }
183  }
184 
185  computeEvalRMS(p);
186 
187  if (iteration == 0) {
188  evolRMS_init = evolRMS;
189  }
190 
191  iteration++;
193 
194  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
195  evolRMS_prec = evolRMS;
196 
197  } while ( (iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps) );
198 
199  nbIteration = iteration;
200 }
void trackNoPyr(const vpImage< unsigned char > &I)
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1422
vpTemplateTrackerPoint * ptTemplate
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)
vpRowVector t() const
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
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
Error that can be emited by the vpTracker class and its derivates.
vpImage< double > dIx
vpImage< double > dIy
unsigned int iterationGlobale
unsigned int nbIteration
unsigned int getHeight() const
Definition: vpImage.h:186
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:5300
unsigned int getWidth() const
Definition: vpImage.h:244
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, vpMatrix &dW)=0