Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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 
55  for (unsigned int point = 0; point < templateSize; point++) {
56  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
57  int i = ptTemplate[point].y;
58  int j = ptTemplate[point].x;
59  ptTemplate[point].dW = new double[nbParam];
60 
61  Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
62 
63  for (unsigned int it = 0; it < nbParam; it++)
64  for (unsigned int jt = 0; jt < nbParam; jt++)
65  H[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
66  }
67  }
68 
69  vpMatrix HLMtemp;
70  vpMatrix::computeHLM(H, lambdaDep, HLMtemp);
71 
72  HCompInverse = HLMtemp.inverseByLU();
73 
74  for (unsigned int point = 0; point < templateSize; point++) {
75  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
76  ptTemplate[point].HiG = new double[nbParam];
77 
78  for (unsigned int i = 0; i < HCompInverse.getRows(); i ++) {
79  ptTemplate[point].HiG[i] = 0;
80  for (unsigned int j = 0; j < HCompInverse.getCols(); j ++) {
81  ptTemplate[point].HiG[i] -= HCompInverse[i][j] * ptTemplate[point].dW[j];
82  }
83  }
84  }
85  }
86  compoInitialised = true;
87 }
88 
90 {
91  initCompInverse(I);
92 }
93 
95 {
96  if (blur)
98 
99  vpColVector dpinv(nbParam);
100  double IW;
101  double Tij;
102  unsigned int iteration = 0;
103  int i, j;
104  double i2, j2;
105  double alpha = 2.;
106  initPosEvalRMS(p);
107 
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  Warp->computeCoeff(p);
119  for (unsigned int point = 0; point < templateSize; point++) {
120  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
121  pt = &ptTemplate[point];
122  i = pt->y;
123  j = pt->x;
124  X1[0] = j;
125  X1[1] = i;
126  Warp->computeDenom(X1, p);
127  Warp->warpX(X1, X2, p);
128  j2 = X2[0];
129  i2 = X2[1];
130 
131  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
132  Tij = pt->val;
133  if (!blur)
134  IW = I.getValue(i2, j2);
135  else
136  IW = BI.getValue(i2, j2);
137  Nbpoint++;
138  double er = (Tij - IW);
139  for (unsigned int it = 0; it < nbParam; it++)
140  dp[it] += er * pt->HiG[it];
141 
142  erreur += er * er;
143  }
144  }
145  }
146  if (Nbpoint == 0) {
147  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
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->getParamInverse(dp, dpinv);
156  Warp->pRondp(p, dpinv, 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  nbIteration = iteration;
170 }
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
virtual void getParamInverse(const vpColVector &p, vpColVector &p_inv) const =0
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:304
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)
void initPosEvalRMS(const vpColVector &p)
unsigned int getRows() const
Definition: vpArray2D.h:289
void computeEvalRMS(const vpColVector &p)
vpImage< double > BI
unsigned int templateSize
unsigned int iterationMax
unsigned int getCols() const
Definition: vpArray2D.h:279
Error that can be emited by the vpTracker class and its derivates.
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &p12) const =0
virtual void getdW0(const int &v, const int &u, const double &dv, const double &du, 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:188
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:6683
unsigned int getWidth() const
Definition: vpImage.h:246