Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerZNCC.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/tt/vpTemplateTrackerZNCC.h>
38
41{
42 dW.resize(2, nbParam);
43 G.resize(nbParam);
44 H.resize(nbParam, nbParam);
45 HLM.resize(nbParam, nbParam);
46
47 temp.resize(nbParam);
48
49 X1.resize(2);
50 X2.resize(2);
51 DI.resize(2);
52}
53
55{
56 double IW, Tij;
57 int i, j;
58 double i2, j2;
59 int Nbpoint = 0;
60
61 Warp->computeCoeff(tp);
62
63 double moyTij = 0;
64 double moyIW = 0;
65 for (unsigned int point = 0; point < templateSize; point++) {
66 i = ptTemplate[point].y;
67 j = ptTemplate[point].x;
68 X1[0] = j;
69 X1[1] = i;
70 Warp->computeDenom(X1, tp);
71 Warp->warpX(X1, X2, tp);
72
73 j2 = X2[0];
74 i2 = X2[1];
75 if ((j2 < I.getWidth() - 1) && (i2 < I.getHeight() - 1) && (i2 > 0) && (j2 > 0)) {
76 Tij = ptTemplate[point].val;
77 if (!blur)
78 IW = I.getValue(i2, j2);
79 else
80 IW = BI.getValue(i2, j2);
81 moyTij += Tij;
82 moyIW += IW;
83 Nbpoint++;
84 }
85 }
86 ratioPixelIn = static_cast<double>(Nbpoint) / static_cast<double>(templateSize);
87 if (!Nbpoint) {
88 throw(vpException(vpException::divideByZeroError, "Cannot get cost: size = 0"));
89 }
90
91 moyTij = moyTij / Nbpoint;
92 moyIW = moyIW / Nbpoint;
93
94 double nom = 0;
95 double var1 = 0, var2 = 0;
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 Warp->computeDenom(X1, tp);
102 Warp->warpX(X1, X2, tp);
103
104 j2 = X2[0];
105 i2 = X2[1];
106 if ((j2 < I.getWidth() - 1) && (i2 < I.getHeight() - 1) && (i2 > 0) && (j2 > 0)) {
107 Tij = ptTemplate[point].val;
108 if (!blur)
109 IW = I.getValue(i2, j2);
110 else
111 IW = BI.getValue(i2, j2);
112 nom += (Tij - moyTij) * (IW - moyIW);
113 var1 += (IW - moyIW) * (IW - moyIW);
114 var2 += (Tij - moyTij) * (Tij - moyTij);
115
116 Nbpoint++;
117 }
118 }
119 return -nom / sqrt(var1 * var2);
120}
121END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ divideByZeroError
Division by zero.
Definition vpException.h:70
Definition of the vpImage class member functions.
Definition vpImage.h:131
VP_EXPLICIT vpTemplateTrackerZNCC(vpTemplateTrackerWarp *warp)
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
vpTemplateTracker()
Default constructor.
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
vpImage< double > BI
unsigned int templateSize