Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpTemplateTrackerZNCC.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Template tracker.
33  *
34  * Authors:
35  * Amaury Dame
36  * Aurelien Yol
37  *
38 *****************************************************************************/
39 #include <visp3/tt/vpTemplateTrackerZNCC.h>
40 
42 {
43  dW.resize(2, nbParam);
44  G.resize(nbParam);
47 
49 
50  X1.resize(2);
51  X2.resize(2);
52  DI.resize(2);
53 }
54 
56 {
57  double IW, Tij;
58  int i, j;
59  double i2, j2;
60  int Nbpoint = 0;
61 
62  Warp->computeCoeff(tp);
63 
64  double moyTij = 0;
65  double moyIW = 0;
66  for (unsigned int point = 0; point < templateSize; point++) {
67  i = ptTemplate[point].y;
68  j = ptTemplate[point].x;
69  X1[0] = j;
70  X1[1] = i;
71  Warp->computeDenom(X1, tp);
72  Warp->warpX(X1, X2, tp);
73 
74  j2 = X2[0];
75  i2 = X2[1];
76  if ((j2 < I.getWidth() - 1) && (i2 < I.getHeight() - 1) && (i2 > 0) && (j2 > 0)) {
77  Tij = ptTemplate[point].val;
78  if (!blur)
79  IW = I.getValue(i2, j2);
80  else
81  IW = BI.getValue(i2, j2);
82  moyTij += Tij;
83  moyIW += IW;
84  Nbpoint++;
85  }
86  }
87  ratioPixelIn = (double)Nbpoint / (double)templateSize;
88  if (!Nbpoint) {
89  throw(vpException(vpException::divideByZeroError, "Cannot get cost: size = 0"));
90  }
91 
92  moyTij = moyTij / Nbpoint;
93  moyIW = moyIW / Nbpoint;
94 
95  double nom = 0;
96  double var1 = 0, var2 = 0;
97  for (unsigned int point = 0; point < templateSize; point++) {
98  i = ptTemplate[point].y;
99  j = ptTemplate[point].x;
100  X1[0] = j;
101  X1[1] = i;
102  Warp->computeDenom(X1, tp);
103  Warp->warpX(X1, X2, tp);
104 
105  j2 = X2[0];
106  i2 = X2[1];
107  if ((j2 < I.getWidth() - 1) && (i2 < I.getHeight() - 1) && (i2 > 0) && (j2 > 0)) {
108  Tij = ptTemplate[point].val;
109  if (!blur)
110  IW = I.getValue(i2, j2);
111  else
112  IW = BI.getValue(i2, j2);
113  nom += (Tij - moyTij) * (IW - moyIW);
114  var1 += (IW - moyIW) * (IW - moyIW);
115  var2 += (Tij - moyTij) * (Tij - moyTij);
116 
117  Nbpoint++;
118  }
119  }
120  return -nom / sqrt(var1 * var2);
121 }
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:352
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ divideByZeroError
Division by zero.
Definition: vpException.h:82
unsigned int getWidth() const
Definition: vpImage.h:245
Type getValue(unsigned int i, unsigned int j) const
Definition: vpImage.h:1764
unsigned int getHeight() const
Definition: vpImage.h:184
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:259
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
vpTemplateTrackerZNCC(vpTemplateTrackerWarp *warp)
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
vpImage< double > BI
unsigned int templateSize