Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerSSDForwardCompositional.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/vpTemplateTrackerSSDForwardCompositional.h>
39
44
46{
47 for (unsigned int point = 0; point < templateSize; point++) {
48 int i = ptTemplate[point].y;
49 int j = ptTemplate[point].x;
50 ptTemplate[point].dW = new double[2 * nbParam];
51 X1[0] = j;
52 X1[1] = i;
53 Warp->computeDenom(X1, p);
54 Warp->getdWdp0(i, j, ptTemplate[point].dW);
55 }
56 compoInitialised = true;
57}
58
60
62{
63 if (!compoInitialised) {
64 std::cout << "Compositionnal tracking not initialised.\nUse initCompo() function." << std::endl;
65 }
66
67 if (blur) {
69 }
72
73 dW = 0;
74
75 double lambda = lambdaDep;
76 double IW, dIWx, dIWy;
77 double Tij;
78 unsigned int iteration = 0;
79 int i, j;
80 double i2, j2;
81 double alpha = 2.;
82
84
85 double evolRMS_init = 0;
86 double evolRMS_prec = 0;
87 double evolRMS_delta;
88 double *tempt = new double[nbParam];
89
90 do {
91 unsigned int Nbpoint = 0;
92 double erreur = 0;
93 G = 0;
94 H = 0;
95 Warp->computeCoeff(p);
96 for (unsigned int point = 0; point < templateSize; point++) {
97 i = ptTemplate[point].y;
98 j = ptTemplate[point].x;
99 X1[0] = j;
100 X1[1] = i;
101
102 Warp->computeDenom(X1, p);
103 Warp->warpX(X1, X2, p);
104
105 j2 = X2[0];
106 i2 = X2[1];
107 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
108 Tij = ptTemplate[point].val;
109 if (!blur)
110 IW = I.getValue(i2, j2);
111 else
112 IW = BI.getValue(i2, j2);
113 dIWx = dIx.getValue(i2, j2);
114 dIWy = dIy.getValue(i2, j2);
115 Nbpoint++;
116
117 Warp->dWarpCompo(X1, X2, p, ptTemplate[point].dW, dW);
118
119 for (unsigned int it = 0; it < nbParam; it++)
120 tempt[it] = dW[0][it] * dIWx + dW[1][it] * dIWy;
121
122 for (unsigned int it = 0; it < nbParam; it++)
123 for (unsigned int jt = 0; jt < nbParam; jt++)
124 H[it][jt] += tempt[it] * tempt[jt];
125
126 double er = (Tij - IW);
127 for (unsigned int it = 0; it < nbParam; it++)
128 G[it] += er * tempt[it];
129
130 erreur += (er * er);
131 }
132 }
133 if (Nbpoint == 0) {
134 delete[] tempt;
135 throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
136 }
137
138 vpMatrix::computeHLM(H, lambda, HLM);
139
140 try {
141 dp = HLM.inverseByLU() * G;
142 }
143 catch (const vpException &e) {
144 delete[] tempt;
145 throw(e);
146 }
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->pRondp(p, dp, p);
155
157
158 if (iteration == 0) {
159 evolRMS_init = evolRMS;
160 }
161 iteration++;
162
163 evolRMS_delta = std::fabs(evolRMS - evolRMS_prec);
164 evolRMS_prec = evolRMS;
165
166 } while ((iteration < iterationMax) && (evolRMS_delta > std::fabs(evolRMS_init) * evolRMS_eps));
167 delete[] tempt;
168
169 nbIteration = iteration;
170}
171END_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)
VP_EXPLICIT vpTemplateTrackerSSDForwardCompositional(vpTemplateTrackerWarp *warp)
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
Error that can be emitted by the vpTracker class and its derivatives.
@ notEnoughPointError
Not enough point to track.