Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
40
BEGIN_VISP_NAMESPACE
41
vpTemplateTrackerZNCCForwardAdditional::vpTemplateTrackerZNCCForwardAdditional
(
vpTemplateTrackerWarp
*warp)
42
:
vpTemplateTrackerZNCC
(warp)
43
{
44
useCompositionnal
=
false
;
45
}
46
47
void
vpTemplateTrackerZNCCForwardAdditional::initHessienDesired
(
const
vpImage<unsigned char>
&I)
48
{
49
if
(
blur
)
50
vpImageFilter::filter
(I,
BI
,
fgG
,
taillef
);
51
vpImageFilter::getGradXGauss2D
(I,
dIx
,
fgG
,
fgdG
,
taillef
);
52
vpImageFilter::getGradYGauss2D
(I,
dIy
,
fgG
,
fgdG
,
taillef
);
53
54
vpImage<double>
dIxx, dIxy, dIyx, dIyy;
55
vpImageFilter::getGradX
(
dIx
, dIxx,
fgdG
,
taillef
);
56
vpImageFilter::getGradY
(
dIx
, dIxy,
fgdG
,
taillef
);
57
58
vpImageFilter::getGradX
(
dIy
, dIyx,
fgdG
,
taillef
);
59
vpImageFilter::getGradY
(
dIy
, dIyy,
fgdG
,
taillef
);
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);
146
vpMatrix::computeHLM
(
Hdesire
,
lambdaDep
,
HLMdesire
);
147
HLMdesireInverse
=
HLMdesire
.inverseByLU();
148
}
149
150
void
vpTemplateTrackerZNCCForwardAdditional::trackNoPyr
(
const
vpImage<unsigned char>
&I)
151
{
152
if
(
blur
)
153
vpImageFilter::filter
(I,
BI
,
fgG
,
taillef
);
154
vpImageFilter::getGradXGauss2D
(I,
dIx
,
fgG
,
fgdG
,
taillef
);
155
vpImageFilter::getGradYGauss2D
(I,
dIy
,
fgG
,
fgdG
,
taillef
);
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
166
initPosEvalRMS
(
p
);
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
{
253
dp
=
HLMdesireInverse
*
G
;
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
268
computeEvalRMS
(
p
);
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
}
283
END_VISP_NAMESPACE
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::divideByZeroError
@ divideByZeroError
Division by zero.
Definition
vpException.h:70
vpImageFilter::getGradX
static void getGradX(const vpImage< unsigned char > &I, vpImage< FilterType > &dIx, const vpImage< bool > *p_mask=nullptr)
Definition
vpImageFilter.h:1959
vpImageFilter::getGradXGauss2D
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)
Definition
vpImageFilter.h:2038
vpImageFilter::filter
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
Definition
vpImageFilter.h:715
vpImageFilter::getGradYGauss2D
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
vpImageFilter.h:2189
vpImageFilter::getGradY
static void getGradY(const vpImage< unsigned char > &I, vpImage< FilterType > &dIy, const vpImage< bool > *p_mask=nullptr)
Definition
vpImageFilter.h:2099
vpImage
Definition of the vpImage class member functions.
Definition
vpImage.h:131
vpImage::getValue
Type getValue(unsigned int i, unsigned int j) const
Definition
vpImage_getters.h:51
vpMatrix::computeHLM
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Definition
vpMatrix.cpp:1882
vpTemplateTrackerWarp
Definition
vpTemplateTrackerWarp.h:65
vpTemplateTrackerZNCCForwardAdditional::trackNoPyr
void trackNoPyr(const vpImage< unsigned char > &I)
Definition
vpTemplateTrackerZNCCForwardAdditional.cpp:150
vpTemplateTrackerZNCCForwardAdditional::initHessienDesired
void initHessienDesired(const vpImage< unsigned char > &I)
Definition
vpTemplateTrackerZNCCForwardAdditional.cpp:47
vpTemplateTrackerZNCCForwardAdditional::vpTemplateTrackerZNCCForwardAdditional
VP_EXPLICIT vpTemplateTrackerZNCCForwardAdditional(vpTemplateTrackerWarp *warp)
Definition
vpTemplateTrackerZNCCForwardAdditional.cpp:41
vpTemplateTrackerZNCC::vpTemplateTrackerZNCC
VP_EXPLICIT vpTemplateTrackerZNCC(vpTemplateTrackerWarp *warp)
Definition
vpTemplateTrackerZNCC.cpp:40
vpTemplateTracker::nbParam
unsigned int nbParam
Definition
vpTemplateTracker.h:125
vpTemplateTracker::dIx
vpImage< double > dIx
Definition
vpTemplateTracker.h:148
vpTemplateTracker::useBrent
bool useBrent
Definition
vpTemplateTracker.h:117
vpTemplateTracker::dIy
vpImage< double > dIy
Definition
vpTemplateTracker.h:149
vpTemplateTracker::HLMdesireInverse
vpMatrix HLMdesireInverse
Definition
vpTemplateTracker.h:109
vpTemplateTracker::lambdaDep
double lambdaDep
Definition
vpTemplateTracker.h:126
vpTemplateTracker::computeEvalRMS
void computeEvalRMS(const vpColVector &p)
Definition
vpTemplateTracker.cpp:790
vpTemplateTracker::Hdesire
vpMatrix Hdesire
Definition
vpTemplateTracker.h:104
vpTemplateTracker::G
vpColVector G
Definition
vpTemplateTracker.h:111
vpTemplateTracker::evolRMS
double evolRMS
Definition
vpTemplateTracker.h:76
vpTemplateTracker::p
vpColVector p
Definition
vpTemplateTracker.h:138
vpTemplateTracker::computeOptimalBrentGain
void computeOptimalBrentGain(const vpImage< unsigned char > &I, vpColVector &tp, double tMI, vpColVector &direction, double &alpha)
Definition
vpTemplateTracker.cpp:371
vpTemplateTracker::H
vpMatrix H
Definition
vpTemplateTracker.h:103
vpTemplateTracker::evolRMS_eps
double evolRMS_eps
Definition
vpTemplateTracker.h:79
vpTemplateTracker::gain
double gain
Definition
vpTemplateTracker.h:113
vpTemplateTracker::X2
vpColVector X2
Definition
vpTemplateTracker.h:143
vpTemplateTracker::taillef
unsigned int taillef
Definition
vpTemplateTracker.h:119
vpTemplateTracker::fgdG
double * fgdG
Definition
vpTemplateTracker.h:121
vpTemplateTracker::iterationMax
unsigned int iterationMax
Definition
vpTemplateTracker.h:127
vpTemplateTracker::initPosEvalRMS
void initPosEvalRMS(const vpColVector &p)
Definition
vpTemplateTracker.cpp:823
vpTemplateTracker::nbIteration
unsigned int nbIteration
Definition
vpTemplateTracker.h:132
vpTemplateTracker::useCompositionnal
bool useCompositionnal
Definition
vpTemplateTracker.h:133
vpTemplateTracker::ptTemplate
vpTemplateTrackerPoint * ptTemplate
Definition
vpTemplateTracker.h:81
vpTemplateTracker::HLMdesire
vpMatrix HLMdesire
Definition
vpTemplateTracker.h:107
vpTemplateTracker::dW
vpMatrix dW
Definition
vpTemplateTracker.h:145
vpTemplateTracker::dp
vpColVector dp
Definition
vpTemplateTracker.h:139
vpTemplateTracker::fgG
double * fgG
Definition
vpTemplateTracker.h:120
vpTemplateTracker::Warp
vpTemplateTrackerWarp * Warp
Definition
vpTemplateTracker.h:136
vpTemplateTracker::blur
bool blur
Definition
vpTemplateTracker.h:116
vpTemplateTracker::BI
vpImage< double > BI
Definition
vpTemplateTracker.h:147
vpTemplateTracker::X1
vpColVector X1
Definition
vpTemplateTracker.h:142
vpTemplateTracker::templateSize
unsigned int templateSize
Definition
vpTemplateTracker.h:84
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
tracker
tt
src
zncc
vpTemplateTrackerZNCCForwardAdditional.cpp
Generated by
1.17.0