Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpTemplateTrackerSSDESM.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/vpTemplateTrackerSSDESM.h>
42 
44  : vpTemplateTrackerSSD(warp), compoInitialised(false), HDir(), HInv(), HLMDir(), HLMInv(), GDir(), GInv()
45 {
46  useCompositionnal = false;
47  useInverse = false;
48 
49  if (!Warp->isESMcompatible()) {
51  "The selected warp function is not appropriate for the ESM algorithm..."));
52  }
53 
60 }
61 
63 
65 {
67  int i, j;
68  // direct
69  for (unsigned int point = 0; point < templateSize; point++) {
70  i = ptTemplate[point].y;
71  j = ptTemplate[point].x;
72  ptTemplateCompo[point].dW = new double[2 * nbParam];
73  Warp->getdWdp0(i, j, ptTemplateCompo[point].dW);
74  }
75 
76  // inverse
77  HInv = 0;
78  for (unsigned int point = 0; point < templateSize; point++) {
79  i = ptTemplate[point].y;
80  j = ptTemplate[point].x;
81 
82  ptTemplate[point].dW = new double[nbParam];
83  Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
84 
85  for (unsigned int it = 0; it < nbParam; it++)
86  for (unsigned int jt = 0; jt < nbParam; jt++)
87  HInv[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
88  }
90 
91  compoInitialised = true;
92 }
93 
95 {
96  if (blur)
100 
101  double IW, dIWx, dIWy;
102  double Tij;
103  unsigned int iteration = 0;
104  int i, j;
105  double i2, j2;
106  double alpha = 2.;
107 
108  initPosEvalRMS(p);
109 
110  double evolRMS_init = 0;
111  double evolRMS_prec = 0;
112  double evolRMS_delta;
113 
114  do {
115  unsigned int Nbpoint = 0;
116  double erreur = 0;
117  dp = 0;
118  HDir = 0;
119  GDir = 0;
120  GInv = 0;
121  Warp->computeCoeff(p);
122  for (unsigned int point = 0; point < templateSize; point++) {
123  i = ptTemplate[point].y;
124  j = ptTemplate[point].x;
125  X1[0] = j;
126  X1[1] = i;
127 
128  Warp->computeDenom(X1, p);
129  Warp->warpX(X1, X2, p);
130 
131  j2 = X2[0];
132  i2 = X2[1];
133  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
134  // INVERSE
135  Tij = ptTemplate[point].val;
136  if (!blur)
137  IW = I.getValue(i2, j2);
138  else
139  IW = BI.getValue(i2, j2);
140  Nbpoint++;
141  double er = (Tij - IW);
142  for (unsigned int it = 0; it < nbParam; it++)
143  GInv[it] += er * ptTemplate[point].dW[it];
144 
145  erreur += er * er;
146 
147  dIWx = dIx.getValue(i2, j2) + ptTemplate[point].dx;
148  dIWy = dIy.getValue(i2, j2) + ptTemplate[point].dy;
149 
150  // Calcul du Hessien
151  Warp->dWarpCompo(X1, X2, p, ptTemplateCompo[point].dW, dW);
152 
153  double *tempt = new double[nbParam];
154  for (unsigned int it = 0; it < nbParam; it++)
155  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
156 
157  for (unsigned int it = 0; it < nbParam; it++)
158  for (unsigned int jt = 0; jt < nbParam; jt++)
159  HDir[it][jt] += tempt[it] * tempt[jt];
160 
161  for (unsigned int it = 0; it < nbParam; it++)
162  GDir[it] += er * tempt[it];
163  delete[] tempt;
164  }
165  }
166  if (Nbpoint == 0) {
167  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
168  }
169 
171 
172  try {
173  dp = (HLMDir).inverseByLU() * (GDir);
174  } catch (const vpException &e) {
175  throw(e);
176  }
177 
178  dp = gain * dp;
179  if (useBrent) {
180  alpha = 2.;
181  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
182  dp = alpha * dp;
183  }
184 
185  p += dp;
186 
187  computeEvalRMS(p);
188 
189  if (iteration == 0) {
190  evolRMS_init = evolRMS;
191  }
192 
193  iteration++;
194 
195  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
196  evolRMS_prec = evolRMS;
197 
198  } while ( (iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps) );
199 
200  nbIteration = iteration;
201 }
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
virtual void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &ParamM, const double *dwdp0, vpMatrix &dW)=0
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
vpTemplateTrackerSSDESM(vpTemplateTrackerWarp *warp)
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
void trackNoPyr(const vpImage< unsigned char > &I)
Error that can be emited by the vpTracker class and its derivates.
void initHessienDesired(const vpImage< unsigned char > &I)
vpImage< double > dIx
vpImage< double > dIy
virtual bool isESMcompatible() const =0
virtual void getdW0(const int &i, const int &j, const double &dy, const double &dx, double *dIdW)=0
unsigned int nbIteration
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, bool convolve=false)
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
unsigned int getHeight() const
Definition: vpImage.h:186
vpTemplateTrackerPointCompo * ptTemplateCompo
void initCompInverse(const vpImage< unsigned char > &I)
virtual void getdWdp0(const int &i, const int &j, double *dIdW)=0
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