Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpTemplateTrackerSSDForwardCompositional.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/vpTemplateTrackerSSDForwardCompositional.h>
42 
44  : vpTemplateTrackerSSD(warp), compoInitialised(false)
45 {
46 }
47 
49 {
50  for (unsigned int point = 0; point < templateSize; point++) {
51  int i = ptTemplate[point].y;
52  int j = ptTemplate[point].x;
53  ptTemplate[point].dW = new double[2 * nbParam];
54  X1[0] = j;
55  X1[1] = i;
56  Warp->computeDenom(X1, p);
57  Warp->getdWdp0(i, j, ptTemplate[point].dW);
58  }
59  compoInitialised = true;
60 }
61 
63 
65 {
66  if (!compoInitialised) {
67  std::cout << "Compositionnal tracking not initialised.\nUse initCompo() function." << std::endl;
68  }
69 
70  if (blur) {
72  }
75 
76  dW = 0;
77 
78  double lambda = lambdaDep;
79  double IW, dIWx, dIWy;
80  double Tij;
81  unsigned int iteration = 0;
82  int i, j;
83  double i2, j2;
84  double alpha = 2.;
85 
87 
88  double evolRMS_init = 0;
89  double evolRMS_prec = 0;
90  double evolRMS_delta;
91  double *tempt = new double[nbParam];
92 
93  do {
94  unsigned int Nbpoint = 0;
95  double erreur = 0;
96  G = 0;
97  H = 0;
98  Warp->computeCoeff(p);
99  for (unsigned int point = 0; point < templateSize; point++) {
100  i = ptTemplate[point].y;
101  j = ptTemplate[point].x;
102  X1[0] = j;
103  X1[1] = i;
104 
105  Warp->computeDenom(X1, p);
106  Warp->warpX(X1, X2, p);
107 
108  j2 = X2[0];
109  i2 = X2[1];
110  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
111  Tij = ptTemplate[point].val;
112  if (!blur)
113  IW = I.getValue(i2, j2);
114  else
115  IW = BI.getValue(i2, j2);
116  dIWx = dIx.getValue(i2, j2);
117  dIWy = dIy.getValue(i2, j2);
118  Nbpoint++;
119 
120  Warp->dWarpCompo(X1, X2, p, ptTemplate[point].dW, dW);
121 
122  for (unsigned int it = 0; it < nbParam; it++)
123  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
124 
125  for (unsigned int it = 0; it < nbParam; it++)
126  for (unsigned int jt = 0; jt < nbParam; jt++)
127  H[it][jt] += tempt[it] * tempt[jt];
128 
129  double er = (Tij - IW);
130  for (unsigned int it = 0; it < nbParam; it++)
131  G[it] += er * tempt[it];
132 
133  erreur += (er * er);
134  }
135  }
136  if (Nbpoint == 0) {
137  delete[] tempt;
138  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
139  }
140 
141  vpMatrix::computeHLM(H, lambda, HLM);
142 
143  try {
144  dp = HLM.inverseByLU() * G;
145  } catch (const vpException &e) {
146  delete[] tempt;
147  throw(e);
148  }
149 
150  dp = gain * dp;
151  if (useBrent) {
152  alpha = 2.;
153  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
154  dp = alpha * dp;
155  }
156  Warp->pRondp(p, dp, p);
157 
158  computeEvalRMS(p);
159 
160  if (iteration == 0) {
161  evolRMS_init = evolRMS;
162  }
163  iteration++;
164 
165  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
166  evolRMS_prec = evolRMS;
167 
168  } while ( (iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps) );
169  delete[] tempt;
170 
171  nbIteration = iteration;
172 }
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1346
vpTemplateTrackerPoint * ptTemplate
vpMatrix inverseByLU() const
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
virtual void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &p, const double *dwdp0, vpMatrix &dM)=0
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
virtual void getdWdp0(const int &v, const int &u, double *dIdW)=0
unsigned int iterationMax
Error that can be emited by the vpTracker class and its derivates.
vpImage< double > dIx
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &p12) const =0
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:188
vpTemplateTrackerWarp * Warp
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:6683
unsigned int getWidth() const
Definition: vpImage.h:246