Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerSSDForwardAdditional.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
38#include <limits> // numeric_limits
39
40#include <visp3/core/vpImageTools.h>
41#include <visp3/tt/vpTemplateTrackerSSDForwardAdditional.h>
42
45 : vpTemplateTrackerSSD(warp), minimizationMethod(USE_NEWTON), p_prec(), G_prec(), KQuasiNewton()
46{
47 useCompositionnal = false;
48}
49
51{
52 if (blur)
56
57 dW = 0;
58
59 double lambda = lambdaDep;
60 double IW, dIWx, dIWy;
61 double Tij;
62 unsigned int iteration = 0;
63 int i, j;
64 double i2, j2;
65 double alpha = 2.;
66
68
69 double evolRMS_init = 0;
70 double evolRMS_prec = 0;
71 double evolRMS_delta;
72 double *tempt = new double[nbParam];
73
74 do {
75 unsigned int Nbpoint = 0;
76 double erreur = 0;
77 G = 0;
78 H = 0;
79 Warp->computeCoeff(p);
80 for (unsigned int point = 0; point < templateSize; point++) {
81 i = ptTemplate[point].y;
82 j = ptTemplate[point].x;
83 X1[0] = j;
84 X1[1] = i;
85
86 Warp->computeDenom(X1, p);
87 Warp->warpX(X1, X2, p);
88
89 j2 = X2[0];
90 i2 = X2[1];
91 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
92 Tij = ptTemplate[point].val;
93
94 if (!blur)
95 IW = I.getValue(i2, j2);
96 else
97 IW = BI.getValue(i2, j2);
98
99 dIWx = dIx.getValue(i2, j2);
100 dIWy = dIy.getValue(i2, j2);
101 Nbpoint++;
102 // Calcul du Hessien
103 Warp->dWarp(X1, X2, p, dW);
104 for (unsigned int it = 0; it < nbParam; it++)
105 tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
106
107 for (unsigned int it = 0; it < nbParam; it++)
108 for (unsigned int jt = 0; jt < nbParam; jt++)
109 H[it][jt] += tempt[it] * tempt[jt];
110
111 double er = (Tij - IW);
112 for (unsigned int it = 0; it < nbParam; it++)
113 G[it] += er * tempt[it];
114
115 erreur += (er * er);
116 }
117 }
118 if (Nbpoint == 0) {
119 delete[] tempt;
120 throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
121 }
122
123 vpMatrix::computeHLM(H, lambda, HLM);
124 try {
125 dp = HLM.inverseByLU() * G;
126 }
127 catch (const vpException &e) {
128 delete[] tempt;
129 throw(e);
130 }
131
132 switch (minimizationMethod) {
134 vpColVector p_test_LMA(nbParam);
135 p_test_LMA = p + dp;
136 erreur = -getCost(I, p);
137 double erreur_LMA = -getCost(I, p_test_LMA);
138 if (erreur_LMA < erreur) {
139 p = p_test_LMA;
140 lambda = (lambda / 10. < 1e-6) ? lambda / 10. : 1e-6;
141 }
142 else {
143 lambda = (lambda * 10. < 1e6) ? 1e6 : lambda * 10.;
144 }
145 } break;
147 dp = gain * 0.000001 * G;
148 if (useBrent) {
149 alpha = 2.;
150 computeOptimalBrentGain(I, p, erreur, dp, alpha);
151 dp = alpha * dp;
152 }
153 p += dp;
154 break;
155 }
156
158 if (iterationGlobale != 0) {
159 vpColVector s_quasi = p - p_prec;
160 vpColVector y_quasi = G - G_prec;
161 double s_scal_y = s_quasi.t() * y_quasi;
162 if (std::fabs(s_scal_y) > std::numeric_limits<double>::epsilon()) // DFP
163 KQuasiNewton = KQuasiNewton + 0.001 * (s_quasi * s_quasi.t() / s_scal_y -
164 KQuasiNewton * y_quasi * y_quasi.t() * KQuasiNewton /
165 (y_quasi.t() * KQuasiNewton * y_quasi));
166 }
167 dp = -KQuasiNewton * G;
168 p_prec = p;
169 G_prec = G;
170 p -= 1.01 * dp;
171 } break;
172
174 default: {
175 if (useBrent) {
176 alpha = 2.;
177 computeOptimalBrentGain(I, p, erreur, dp, alpha);
178 dp = alpha * dp;
179 }
180
181 p += dp;
182 break;
183 }
184 }
185
187
188 if (iteration == 0) {
189 evolRMS_init = evolRMS;
190 }
191
192 iteration++;
194
195 evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
196 evolRMS_prec = evolRMS;
197
198 } while ((iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init) * evolRMS_eps));
199 delete[] tempt;
200
201 nbIteration = iteration;
202}
203END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
vpRowVector t() const
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)
VP_EXPLICIT vpTemplateTrackerSSDForwardAdditional(vpTemplateTrackerWarp *warp)
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
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
unsigned int iterationGlobale
vpImage< double > BI
unsigned int templateSize
Error that can be emitted by the vpTracker class and its derivatives.
@ notEnoughPointError
Not enough point to track.