Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerSSDInverseCompositional.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/vpImageTools.h>
38#include <visp3/tt/vpTemplateTrackerSSDInverseCompositional.h>
39
48
50{
51 H = 0;
52
53 for (unsigned int point = 0; point < templateSize; point++) {
54 if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
55 int i = ptTemplate[point].y;
56 int j = ptTemplate[point].x;
57 ptTemplate[point].dW = new double[nbParam];
58
59 Warp->getdW0(i, j, ptTemplate[point].dy, ptTemplate[point].dx, ptTemplate[point].dW);
60
61 for (unsigned int it = 0; it < nbParam; it++)
62 for (unsigned int jt = 0; jt < nbParam; jt++)
63 H[it][jt] += ptTemplate[point].dW[it] * ptTemplate[point].dW[jt];
64 }
65 }
66
67 vpMatrix HLMtemp;
69
70 HCompInverse = HLMtemp.inverseByLU();
71
72 for (unsigned int point = 0; point < templateSize; point++) {
73 if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
74 ptTemplate[point].HiG = new double[nbParam];
75
76 for (unsigned int i = 0; i < HCompInverse.getRows(); i++) {
77 ptTemplate[point].HiG[i] = 0;
78 for (unsigned int j = 0; j < HCompInverse.getCols(); j++) {
79 ptTemplate[point].HiG[i] -= HCompInverse[i][j] * ptTemplate[point].dW[j];
80 }
81 }
82 }
83 }
84 compoInitialised = true;
85}
86
91
93{
94 if (blur)
96
97 vpColVector dpinv(nbParam);
98 double IW;
99 double Tij;
100 unsigned int iteration = 0;
101 int i, j;
102 double i2, j2;
103 double alpha = 2.;
105
107
108 double evolRMS_init = 0;
109 double evolRMS_prec = 0;
110 double evolRMS_delta;
111
112 do {
113 unsigned int Nbpoint = 0;
114 double erreur = 0;
115 dp = 0;
116 Warp->computeCoeff(p);
117 for (unsigned int point = 0; point < templateSize; point++) {
118 if ((!useTemplateSelect) || (ptTemplateSelect[point])) {
119 pt = &ptTemplate[point];
120 i = pt->y;
121 j = pt->x;
122 X1[0] = j;
123 X1[1] = i;
124 Warp->computeDenom(X1, p);
125 Warp->warpX(X1, X2, p);
126 j2 = X2[0];
127 i2 = X2[1];
128
129 if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
130 Tij = pt->val;
131 if (!blur)
132 IW = I.getValue(i2, j2);
133 else
134 IW = BI.getValue(i2, j2);
135 Nbpoint++;
136 double er = (Tij - IW);
137 for (unsigned int it = 0; it < nbParam; it++)
138 dp[it] += er * pt->HiG[it];
139
140 erreur += er * er;
141 }
142 }
143 }
144 if (Nbpoint == 0) {
145 throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No points in the template"));
146 }
147 dp = gain * dp;
148 if (useBrent) {
149 alpha = 2.;
150 computeOptimalBrentGain(I, p, erreur / Nbpoint, dp, alpha);
151 dp = alpha * dp;
152 }
153 Warp->getParamInverse(dp, dpinv);
154 Warp->pRondp(p, dpinv, 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 nbIteration = iteration;
168}
169END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
vpMatrix inverseByLU() const
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
VP_EXPLICIT vpTemplateTrackerSSDInverseCompositional(vpTemplateTrackerWarp *warp)
VP_EXPLICIT vpTemplateTrackerSSD(vpTemplateTrackerWarp *warp)
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.