Visual Servoing Platform  version 3.6.1 under development (2024-07-18)
vpTemplateTrackerSSDForwardCompositional.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/vpImageFilter.h>
40 #include <visp3/tt/vpTemplateTrackerSSDForwardCompositional.h>
41 
44  : vpTemplateTrackerSSD(warp), compoInitialised(false)
45 { }
46 
48 {
49  for (unsigned int point = 0; point < templateSize; point++) {
50  int i = ptTemplate[point].y;
51  int j = ptTemplate[point].x;
52  ptTemplate[point].dW = new double[2 * nbParam];
53  X1[0] = j;
54  X1[1] = i;
55  Warp->computeDenom(X1, p);
56  Warp->getdWdp0(i, j, ptTemplate[point].dW);
57  }
58  compoInitialised = true;
59 }
60 
62 
64 {
65  if (!compoInitialised) {
66  std::cout << "Compositionnal tracking not initialised.\nUse initCompo() function." << std::endl;
67  }
68 
69  if (blur) {
71  }
74 
75  dW = 0;
76 
77  double lambda = lambdaDep;
78  double IW, dIWx, dIWy;
79  double Tij;
80  unsigned int iteration = 0;
81  int i, j;
82  double i2, j2;
83  double alpha = 2.;
84 
86 
87  double evolRMS_init = 0;
88  double evolRMS_prec = 0;
89  double evolRMS_delta;
90  double *tempt = new double[nbParam];
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  for (unsigned int it = 0; it < nbParam; it++)
122  tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
123 
124  for (unsigned int it = 0; it < nbParam; it++)
125  for (unsigned int jt = 0; jt < nbParam; jt++)
126  H[it][jt] += tempt[it] * tempt[jt];
127 
128  double er = (Tij - IW);
129  for (unsigned int it = 0; it < nbParam; it++)
130  G[it] += er * tempt[it];
131 
132  erreur += (er * er);
133  }
134  }
135  if (Nbpoint == 0) {
136  delete[] tempt;
137  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
138  }
139 
140  vpMatrix::computeHLM(H, lambda, HLM);
141 
142  try {
143  dp = HLM.inverseByLU() * G;
144  }
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 }
173 END_VISP_NAMESPACE
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void getGradXGauss2D(const vpImage< ImageType > &I, vpImage< FilterType > &dIx, const FilterType *gaussianKernel, const FilterType *gaussianDerivativeKernel, unsigned int size, const vpImage< bool > *p_mask=nullptr)
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
static void getGradYGauss2D(const vpImage< ImageType > &I, vpImage< FilterType > &dIy, const FilterType *gaussianKernel, const FilterType *gaussianDerivativeKernel, unsigned int size, const vpImage< bool > *p_mask=nullptr)
unsigned int getWidth() const
Definition: vpImage.h:242
Type getValue(unsigned int i, unsigned int j) const
unsigned int getHeight() const
Definition: vpImage.h:181
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition: vpMatrix.cpp:1873
VP_EXPLICIT vpTemplateTrackerSSDForwardCompositional(vpTemplateTrackerWarp *warp)
virtual void dWarpCompo(const vpColVector &X1, const vpColVector &X2, const vpColVector &p, const double *dwdp0, vpMatrix &dM)=0
virtual void getdWdp0(const int &v, const int &u, 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
vpImage< double > dIx
vpImage< double > dIy
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.