Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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 
92  do {
93  unsigned int Nbpoint = 0;
94  double erreur = 0;
95  G = 0;
96  H = 0;
97  Warp->computeCoeff(p);
98  for (unsigned int point = 0; point < templateSize; point++) {
99  i = ptTemplate[point].y;
100  j = ptTemplate[point].x;
101  X1[0] = j;
102  X1[1] = i;
103 
104  Warp->computeDenom(X1, p);
105  Warp->warpX(X1, X2, p);
106 
107  j2 = X2[0];
108  i2 = X2[1];
109  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
110  Tij = ptTemplate[point].val;
111  if (!blur)
112  IW = I.getValue(i2, j2);
113  else
114  IW = BI.getValue(i2, j2);
115  dIWx = dIx.getValue(i2, j2);
116  dIWy = dIy.getValue(i2, j2);
117  Nbpoint++;
118 
119  Warp->dWarpCompo(X1, X2, p, ptTemplate[point].dW, dW);
120 
121  double *tempt = new double[nbParam];
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  delete[] tempt;
135  }
136  }
137  if (Nbpoint == 0) {
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  throw(e);
147  }
148 
149  dp = gain * dp;
150  if (useBrent) {
151  alpha = 2.;
152  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
153  dp = alpha * dp;
154  }
155  Warp->pRondp(p, dp, p);
156 
157  computeEvalRMS(p);
158 
159  if (iteration == 0) {
160  evolRMS_init = evolRMS;
161  }
162  iteration++;
163 
164  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
165  evolRMS_prec = evolRMS;
166 
167  } while ( (iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps) );
168 
169  nbIteration = iteration;
170 }
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
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
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &pres) const =0
Error that can be emited by the vpTracker class and its derivates.
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
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