Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpTemplateTrackerSSDInverseCompositional.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/vpImageTools.h>
41 #include <visp3/tt/vpTemplateTrackerSSDInverseCompositional.h>
42 
44  : vpTemplateTrackerSSD(warp), compoInitialised(false), HInv(), HCompInverse(), useTemplateSelect(false)
45 {
46  useInverse = true;
49 }
50 
52 {
53  H = 0;
54  int i, j;
55 
56  for (unsigned int point = 0; point < templateSize; point++) {
57  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
58  i = ptTemplate[point].y;
59  j = ptTemplate[point].x;
60  ptTemplate[point].dW = new double[nbParam];
61 
62  Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
63 
64  for (unsigned int it = 0; it < nbParam; it++)
65  for (unsigned int jt = 0; jt < nbParam; jt++)
66  H[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
67  }
68  }
69  HInv = H;
70  vpMatrix HLMtemp(nbParam, nbParam);
71  vpMatrix::computeHLM(H, lambdaDep, HLMtemp);
72 
74  HCompInverse = HLMtemp.inverseByLU();
75  vpColVector dWtemp(nbParam);
76  vpColVector HiGtemp(nbParam);
77 
78  for (unsigned int point = 0; point < templateSize; point++) {
79  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
80  for (unsigned int it = 0; it < nbParam; it++)
81  dWtemp[it] = ptTemplate[point].dW[it];
82 
83  HiGtemp = - HCompInverse * dWtemp;
84  ptTemplate[point].HiG = new double[nbParam];
85 
86  for (unsigned int it = 0; it < nbParam; it++)
87  ptTemplate[point].HiG[it] = HiGtemp[it];
88  }
89  }
90  compoInitialised = true;
91 }
92 
94 {
95  initCompInverse(I);
96 }
97 
99 {
100  if (blur)
102 
103  vpColVector dpinv(nbParam);
104  double IW;
105  double Tij;
106  unsigned int iteration = 0;
107  int i, j;
108  double i2, j2;
109  double alpha = 2.;
110  initPosEvalRMS(p);
111 
113 
114  double evolRMS_init = 0;
115  double evolRMS_prec = 0;
116  double evolRMS_delta;
117 
118  do {
119  unsigned int Nbpoint = 0;
120  double erreur = 0;
121  dp = 0;
122  Warp->computeCoeff(p);
123  for (unsigned int point = 0; point < templateSize; point++) {
124  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
125  pt = &ptTemplate[point];
126  i = pt->y;
127  j = pt->x;
128  X1[0] = j;
129  X1[1] = i;
130  Warp->computeDenom(X1, p);
131  Warp->warpX(X1, X2, p);
132  j2 = X2[0];
133  i2 = X2[1];
134 
135  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
136  Tij = pt->val;
137  if (!blur)
138  IW = I.getValue(i2, j2);
139  else
140  IW = BI.getValue(i2, j2);
141  Nbpoint++;
142  double er = (Tij - IW);
143  for (unsigned int it = 0; it < nbParam; it++)
144  dp[it] += er * pt->HiG[it];
145 
146  erreur += er * er;
147  }
148  }
149  }
150  if (Nbpoint == 0) {
151  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
152  }
153  dp = gain * dp;
154  if (useBrent) {
155  alpha = 2.;
156  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
157  dp = alpha * dp;
158  }
159  Warp->getParamInverse(dp, dpinv);
160  Warp->pRondp(p, dpinv, p);
161 
162  computeEvalRMS(p);
163 
164  if (iteration == 0) {
165  evolRMS_init = evolRMS;
166  }
167  iteration++;
168 
169  evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
170  evolRMS_prec = evolRMS;
171 
172  } while ( (iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init)*evolRMS_eps) );
173  nbIteration = iteration;
174 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
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)
void initPosEvalRMS(const vpColVector &p)
void computeEvalRMS(const vpColVector &p)
vpImage< double > BI
virtual void getParamInverse(const vpColVector &ParamM, vpColVector &ParamMinv) const =0
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.
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)
unsigned int getHeight() const
Definition: vpImage.h:186
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
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