Visual Servoing Platform  version 3.6.1 under development (2024-03-18)
vpTemplateTrackerSSDForwardAdditional.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  *
38 *****************************************************************************/
39 
40 #include <limits> // numeric_limits
41 
42 #include <visp3/core/vpImageTools.h>
43 #include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
44 
46  : vpTemplateTrackerSSD(warp), minimizationMethod(USE_NEWTON), p_prec(), G_prec(), KQuasiNewton()
47 {
48  useCompositionnal = false;
49 }
50 
52 {
53  if (blur)
57 
58  dW = 0;
59 
60  double lambda = lambdaDep;
61  double IW, dIWx, dIWy;
62  double Tij;
63  unsigned int iteration = 0;
64  int i, j;
65  double i2, j2;
66  double alpha = 2.;
67 
69 
70  double evolRMS_init = 0;
71  double evolRMS_prec = 0;
72  double evolRMS_delta;
73  double *tempt = new double[nbParam];
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  for (unsigned int it = 0; it < nbParam; it++)
106  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
107 
108  for (unsigned int it = 0; it < nbParam; it++)
109  for (unsigned int jt = 0; jt < nbParam; jt++)
110  H[it][jt] += tempt[it] * tempt[jt];
111 
112  double er = (Tij - IW);
113  for (unsigned int it = 0; it < nbParam; it++)
114  G[it] += er * tempt[it];
115 
116  erreur += (er * er);
117  }
118  }
119  if (Nbpoint == 0) {
120  delete[] tempt;
121  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
122  }
123 
124  vpMatrix::computeHLM(H, lambda, HLM);
125  try {
126  dp = HLM.inverseByLU() * G;
127  } catch (const vpException &e) {
128  delete[] tempt;
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  delete[] tempt;
199 
200  nbIteration = iteration;
201 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void getGradXGauss2D(const vpImage< ImageType > &I, vpImage< FilterType > &dIx, const FilterType *gaussianKernel, const FilterType *gaussianDerivativeKernel, unsigned int size, const vpImage< bool > *p_mask=nullptr)
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
static void getGradYGauss2D(const vpImage< ImageType > &I, vpImage< FilterType > &dIy, const FilterType *gaussianKernel, const FilterType *gaussianDerivativeKernel, unsigned int size, const vpImage< bool > *p_mask=nullptr)
unsigned int getWidth() const
Definition: vpImage.h:249
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1816
unsigned int getHeight() const
Definition: vpImage.h:184
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:6407
void trackNoPyr(const vpImage< unsigned char > &I)
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
virtual void dWarp(const vpColVector &X1, const vpColVector &X2, const vpColVector &p, vpMatrix &dM)=0
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
vpImage< double > dIx
vpImage< double > dIy
void computeEvalRMS(const vpColVector &p)
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
unsigned int iterationMax
void initPosEvalRMS(const vpColVector &p)
unsigned int nbIteration
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
unsigned int iterationGlobale
vpImage< double > BI
unsigned int templateSize
Error that can be emitted by the vpTracker class and its derivatives.
@ notEnoughPointError
Not enough point to track.