Visual Servoing Platform  version 3.6.1 under development (2024-03-18)
vpTemplateTrackerSSDInverseCompositional.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  *
38 *****************************************************************************/
39 #include <visp3/core/vpImageTools.h>
40 #include <visp3/tt/vpTemplateTrackerSSDInverseCompositional.h>
41 
43  : vpTemplateTrackerSSD(warp), compoInitialised(false), HInv(), HCompInverse(), useTemplateSelect(false)
44 {
45  useInverse = true;
48 }
49 
51 {
52  H = 0;
53 
54  for (unsigned int point = 0; point < templateSize; point++) {
55  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
56  int i = ptTemplate[point].y;
57  int j = ptTemplate[point].x;
58  ptTemplate[point].dW = new double[nbParam];
59 
60  Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
61 
62  for (unsigned int it = 0; it < nbParam; it++)
63  for (unsigned int jt = 0; jt < nbParam; jt++)
64  H[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
65  }
66  }
67 
68  vpMatrix HLMtemp;
69  vpMatrix::computeHLM(H, lambdaDep, HLMtemp);
70 
71  HCompInverse = HLMtemp.inverseByLU();
72 
73  for (unsigned int point = 0; point < templateSize; point++) {
74  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
75  ptTemplate[point].HiG = new double[nbParam];
76 
77  for (unsigned int i = 0; i < HCompInverse.getRows(); i++) {
78  ptTemplate[point].HiG[i] = 0;
79  for (unsigned int j = 0; j < HCompInverse.getCols(); j++) {
80  ptTemplate[point].HiG[i] -= HCompInverse[i][j] * ptTemplate[point].dW[j];
81  }
82  }
83  }
84  }
85  compoInitialised = true;
86 }
87 
89 {
90  initCompInverse(I);
91 }
92 
94 {
95  if (blur)
97 
98  vpColVector dpinv(nbParam);
99  double IW;
100  double Tij;
101  unsigned int iteration = 0;
102  int i, j;
103  double i2, j2;
104  double alpha = 2.;
105  initPosEvalRMS(p);
106 
108 
109  double evolRMS_init = 0;
110  double evolRMS_prec = 0;
111  double evolRMS_delta;
112 
113  do {
114  unsigned int Nbpoint = 0;
115  double erreur = 0;
116  dp = 0;
117  Warp->computeCoeff(p);
118  for (unsigned int point = 0; point < templateSize; point++) {
119  if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
120  pt = &ptTemplate[point];
121  i = pt->y;
122  j = pt->x;
123  X1[0] = j;
124  X1[1] = i;
125  Warp->computeDenom(X1, p);
126  Warp->warpX(X1, X2, p);
127  j2 = X2[0];
128  i2 = X2[1];
129 
130  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
131  Tij = pt->val;
132  if (!blur)
133  IW = I.getValue(i2, j2);
134  else
135  IW = BI.getValue(i2, j2);
136  Nbpoint++;
137  double er = (Tij - IW);
138  for (unsigned int it = 0; it < nbParam; it++)
139  dp[it] += er * pt->HiG[it];
140 
141  erreur += er * er;
142  }
143  }
144  }
145  if (Nbpoint == 0) {
146  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
147  }
148  dp = gain * dp;
149  if (useBrent) {
150  alpha = 2.;
151  computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
152  dp = alpha * dp;
153  }
154  Warp->getParamInverse(dp, dpinv);
155  Warp->pRondp(p, dpinv, 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  nbIteration = iteration;
169 }
unsigned int getCols() const
Definition: vpArray2D.h:274
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:299
unsigned int getRows() const
Definition: vpArray2D.h:284
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
unsigned int getWidth() const
Definition: vpImage.h:249
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1816
unsigned int getHeight() const
Definition: vpImage.h:184
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:6407
virtual void getParamInverse(const vpColVector &p, vpColVector &p_inv) const =0
virtual void getdW0(const int &v, const int &u, const double &dv, const double &du, double *dIdW)=0
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
virtual void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &p12) const =0
void computeEvalRMS(const vpColVector &p)
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
unsigned int iterationMax
void initPosEvalRMS(const vpColVector &p)
unsigned int nbIteration
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
vpImage< double > BI
unsigned int templateSize
Error that can be emitted by the vpTracker class and its derivatives.
@ notEnoughPointError
Not enough point to track.