Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerSSDESM.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Template tracker.
32 *
33 * Authors:
34 * Amaury Dame
35 * Aurelien Yol
36 */
37#include <visp3/core/vpImageFilter.h>
38#include <visp3/tt/vpTemplateTrackerSSDESM.h>
39
42 : vpTemplateTrackerSSD(warp), compoInitialised(false), HDir(), HInv(), HLMDir(), HLMInv(), GDir(), GInv()
43{
44 useCompositionnal = false;
45 useInverse = false;
46
47 if (!Warp->isESMcompatible()) {
48 throw(vpException(vpException::badValue, "The selected warp function is not appropriate for the ESM algorithm..."));
49 }
50
51 HInv.resize(nbParam, nbParam);
52 HDir.resize(nbParam, nbParam);
53 HLMInv.resize(nbParam, nbParam);
54 HLMDir.resize(nbParam, nbParam);
55 GInv.resize(nbParam);
56 GDir.resize(nbParam);
57}
58
60
62{
64 int i, j;
65 // direct
66 for (unsigned int point = 0; point < templateSize; point++) {
67 i = ptTemplate[point].y;
68 j = ptTemplate[point].x;
69 ptTemplateCompo[point].dW = new double[2 * nbParam];
70 Warp->getdWdp0(i, j, ptTemplateCompo[point].dW);
71 }
72
73 // inverse
74 HInv = 0;
75 for (unsigned int point = 0; point < templateSize; point++) {
76 i = ptTemplate[point].y;
77 j = ptTemplate[point].x;
78
79 ptTemplate[point].dW = new double[nbParam];
80 Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
81
82 for (unsigned int it = 0; it < nbParam; it++)
83 for (unsigned int jt = 0; jt < nbParam; jt++)
84 HInv[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
85 }
87
88 compoInitialised = true;
89}
90
92{
93 if (blur)
97
98 double IW, dIWx, dIWy;
99 double Tij;
100 unsigned int iteration = 0;
101 int i, j;
102 double i2, j2;
103 double alpha = 2.;
104
106
107 double evolRMS_init = 0;
108 double evolRMS_prec = 0;
109 double evolRMS_delta;
110 double *tempt = new double[nbParam];
111
112 do {
113 unsigned int Nbpoint = 0;
114 double erreur = 0;
115 dp = 0;
116 HDir = 0;
117 GDir = 0;
118 GInv = 0;
119 Warp->computeCoeff(p);
120 for (unsigned int point = 0; point < templateSize; point++) {
121 i = ptTemplate[point].y;
122 j = ptTemplate[point].x;
123 X1[0] = j;
124 X1[1] = i;
125
126 Warp->computeDenom(X1, p);
127 Warp->warpX(X1, X2, p);
128
129 j2 = X2[0];
130 i2 = X2[1];
131 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
132 // INVERSE
133 Tij = ptTemplate[point].val;
134 if (!blur)
135 IW = I.getValue(i2, j2);
136 else
137 IW = BI.getValue(i2, j2);
138 Nbpoint++;
139 double er = (Tij - IW);
140 for (unsigned int it = 0; it < nbParam; it++)
141 GInv[it] += er * ptTemplate[point].dW[it];
142
143 erreur += er * er;
144
145 dIWx = dIx.getValue(i2, j2) + ptTemplate[point].dx;
146 dIWy = dIy.getValue(i2, j2) + ptTemplate[point].dy;
147
148 // Calcul du Hessien
149 Warp->dWarpCompo(X1, X2, p, ptTemplateCompo[point].dW, dW);
150
151 for (unsigned int it = 0; it < nbParam; it++)
152 tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
153
154 for (unsigned int it = 0; it < nbParam; it++)
155 for (unsigned int jt = 0; jt < nbParam; jt++)
156 HDir[it][jt] += tempt[it] * tempt[jt];
157
158 for (unsigned int it = 0; it < nbParam; it++)
159 GDir[it] += er * tempt[it];
160 }
161 }
162 if (Nbpoint == 0) {
163 delete[] tempt;
164 throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
165 }
166
168
169 try {
170 dp = (HLMDir).inverseByLU() * (GDir);
171 }
172 catch (const vpException &e) {
173 delete[] tempt;
174 throw(e);
175 }
176
177 dp = gain * dp;
178 if (useBrent) {
179 alpha = 2.;
180 computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
181 dp = alpha * dp;
182 }
183
184 p += dp;
185
187
188 if (iteration == 0) {
189 evolRMS_init = evolRMS;
190 }
191
192 iteration++;
193
194 evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
195 evolRMS_prec = evolRMS;
196
197 } while ((iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init) * evolRMS_eps));
198 delete[] tempt;
199
200 nbIteration = iteration;
201}
202END_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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
void trackNoPyr(const vpImage< unsigned char > &I)
void initHessienDesired(const vpImage< unsigned char > &I)
VP_EXPLICIT vpTemplateTrackerSSDESM(vpTemplateTrackerWarp *warp)
void initCompInverse(const vpImage< unsigned char > &I)
VP_EXPLICIT vpTemplateTrackerSSD(vpTemplateTrackerWarp *warp)
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)
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
vpImage< double > BI
unsigned int templateSize
vpTemplateTrackerPointCompo * ptTemplateCompo
Error that can be emitted by the vpTracker class and its derivatives.
@ notEnoughPointError
Not enough point to track.