Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerZNCCForwardAdditional.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/vpTemplateTrackerZNCCForwardAdditional.h>
39
46
48{
49 if (blur)
53
54 vpImage<double> dIxx, dIxy, dIyx, dIyy;
57
60
61 Warp->computeCoeff(p);
62 double IW, dIWx, dIWy;
63 double Tij;
64 int i, j;
65 double i2, j2;
66 int Nbpoint = 0;
67
68 double moyTij = 0;
69 double moyIW = 0;
70 double denom = 0;
71 double *tempt = new double[nbParam];
72
73 for (unsigned int point = 0; point < templateSize; point++) {
74 i = ptTemplate[point].y;
75 j = ptTemplate[point].x;
76 X1[0] = j;
77 X1[1] = i;
78 X2[0] = j;
79 X2[1] = i;
80
81 Warp->computeDenom(X1, p);
82
83 j2 = X2[0];
84 i2 = X2[1];
85
86 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
87 Tij = ptTemplate[point].val;
88
89 if (!blur)
90 IW = I.getValue(i2, j2);
91 else
92 IW = BI.getValue(i2, j2);
93
94 Nbpoint++;
95 moyTij += Tij;
96 moyIW += IW;
97 }
98 }
99 moyTij = moyTij / Nbpoint;
100 moyIW = moyIW / Nbpoint;
101 Hdesire = 0;
102 for (unsigned int point = 0; point < templateSize; point++) {
103 i = ptTemplate[point].y;
104 j = ptTemplate[point].x;
105 X1[0] = j;
106 X1[1] = i;
107 X2[0] = j;
108 X2[1] = i;
109
110 Warp->computeDenom(X1, p);
111
112 j2 = X2[0];
113 i2 = X2[1];
114
115 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
116 Tij = ptTemplate[point].val;
117
118 if (!blur)
119 IW = I.getValue(i2, j2);
120 else
121 IW = BI.getValue(i2, j2);
122
123 dIWx = dIx.getValue(i2, j2);
124 dIWy = dIy.getValue(i2, j2);
125 // Calcul du Hessien
126 Warp->dWarp(X1, X2, p, dW);
127 for (unsigned int it = 0; it < nbParam; it++)
128 tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
129
130 double prod = (Tij - moyTij);
131
132 double d_Ixx = dIxx.getValue(i2, j2);
133 double d_Iyy = dIyy.getValue(i2, j2);
134 double d_Ixy = dIxy.getValue(i2, j2);
135
136 for (unsigned int it = 0; it < nbParam; it++)
137 for (unsigned int jt = 0; jt < nbParam; jt++)
138 Hdesire[it][jt] += prod * (dW[0][it] * (dW[0][jt] * d_Ixx + dW[1][jt] * d_Ixy) +
139 dW[1][it] * (dW[0][jt] * d_Ixy + dW[1][jt] * d_Iyy));
140 denom += (Tij - moyTij) * (Tij - moyTij) * (IW - moyIW) * (IW - moyIW);
141 }
142 }
143 delete[] tempt;
144
145 Hdesire = Hdesire / sqrt(denom);
147 HLMdesireInverse = HLMdesire.inverseByLU();
148}
149
151{
152 if (blur)
156
157 dW = 0;
158
159 double IW, dIWx, dIWy;
160 double Tij;
161 unsigned int iteration = 0;
162 int i, j;
163 double i2, j2;
164 double alpha = 2.;
165
167
168 double evolRMS_init = 0;
169 double evolRMS_prec = 0;
170 double evolRMS_delta;
171 double *tempt = new double[nbParam];
172
173 do {
174 int Nbpoint = 0;
175 double erreur = 0;
176 G = 0;
177 H = 0;
178 Warp->computeCoeff(p);
179 double moyTij = 0;
180 double moyIW = 0;
181 double denom = 0;
182 for (unsigned int point = 0; point < templateSize; point++) {
183 i = ptTemplate[point].y;
184 j = ptTemplate[point].x;
185 X1[0] = j;
186 X1[1] = i;
187
188 Warp->computeDenom(X1, p);
189 Warp->warpX(X1, X2, p);
190
191 j2 = X2[0];
192 i2 = X2[1];
193 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
194 Tij = ptTemplate[point].val;
195
196 if (!blur)
197 IW = I.getValue(i2, j2);
198 else
199 IW = BI.getValue(i2, j2);
200
201 Nbpoint++;
202 moyTij += Tij;
203 moyIW += IW;
204 }
205 }
206
207 if (!Nbpoint) {
208 delete[] tempt;
209 throw(vpException(vpException::divideByZeroError, "Cannot track the template: no point"));
210 }
211
212 moyTij = moyTij / Nbpoint;
213 moyIW = moyIW / Nbpoint;
214 for (unsigned int point = 0; point < templateSize; point++) {
215 i = ptTemplate[point].y;
216 j = ptTemplate[point].x;
217 X1[0] = j;
218 X1[1] = i;
219
220 Warp->computeDenom(X1, p);
221 Warp->warpX(X1, X2, p);
222
223 j2 = X2[0];
224 i2 = X2[1];
225 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
226 Tij = ptTemplate[point].val;
227
228 if (!blur)
229 IW = I.getValue(i2, j2);
230 else
231 IW = BI.getValue(i2, j2);
232
233 dIWx = dIx.getValue(i2, j2);
234 dIWy = dIy.getValue(i2, j2);
235 // Calcul du Hessien
236 Warp->dWarp(X1, X2, p, dW);
237 for (unsigned int it = 0; it < nbParam; it++)
238 tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
239
240 double prod = (Tij - moyTij);
241 for (unsigned int it = 0; it < nbParam; it++)
242 G[it] += prod * tempt[it];
243
244 double er = (Tij - IW);
245 erreur += (er * er);
246 denom += (Tij - moyTij) * (Tij - moyTij) * (IW - moyIW) * (IW - moyIW);
247 }
248 }
249 G = G / sqrt(denom);
250 H = H / sqrt(denom);
251
252 try {
254 }
255 catch (const vpException &e) {
256 delete[] tempt;
257 throw(e);
258 }
259
260 dp = gain * dp;
261 if (useBrent) {
262 alpha = 2.;
263 computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
264 dp = alpha * dp;
265 }
266 p -= dp;
267
269
270 if (iteration == 0) {
271 evolRMS_init = evolRMS;
272 }
273 iteration++;
274
275 evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
276 evolRMS_prec = evolRMS;
277
278 } while ((iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init) * evolRMS_eps));
279 delete[] tempt;
280
281 nbIteration = iteration;
282}
283END_VISP_NAMESPACE
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ divideByZeroError
Division by zero.
Definition vpException.h:70
static void getGradX(const vpImage< unsigned char > &I, vpImage< FilterType > &dIx, const vpImage< bool > *p_mask=nullptr)
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)
static void getGradY(const vpImage< unsigned char > &I, vpImage< FilterType > &dIy, const vpImage< bool > *p_mask=nullptr)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Type getValue(unsigned int i, unsigned int j) const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
void initHessienDesired(const vpImage< unsigned char > &I)
VP_EXPLICIT vpTemplateTrackerZNCCForwardAdditional(vpTemplateTrackerWarp *warp)
VP_EXPLICIT vpTemplateTrackerZNCC(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