Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpTemplateTrackerSSD.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 
40 #include <visp3/tt/vpTemplateTrackerSSD.h>
41 
42 BEGIN_VISP_NAMESPACE
44 {
45  dW.resize(2, nbParam);
46  G.resize(nbParam);
49 
51 
52  X1.resize(2);
53  X2.resize(2);
54  DI.resize(2);
55 }
56 
58 {
59  double erreur = 0;
60  double IW;
61  int Nbpoint = 0;
62 
63  Warp->computeCoeff(tp);
64  for (unsigned int point = 0; point < templateSize; point++) {
65  int i = ptTemplate[point].y;
66  int j = ptTemplate[point].x;
67  X1[0] = j;
68  X1[1] = i;
69  Warp->computeDenom(X1, tp);
70  Warp->warpX(X1, X2, tp);
71 
72  double j2 = X2[0];
73  double i2 = X2[1];
74  if ((i2 >= 0) && (j2 >= 0) && (i2 < I.getHeight() - 1) && (j2 < I.getWidth() - 1)) {
75  double Tij = ptTemplate[point].val;
76  if (!blur)
77  IW = I.getValue(i2, j2);
78  else
79  IW = BI.getValue(i2, j2);
80  erreur += (Tij - IW) * (Tij - IW);
81  Nbpoint++;
82  }
83  }
84  ratioPixelIn = static_cast<double>(Nbpoint) / static_cast<double>(templateSize);
85 
86  if (Nbpoint == 0)
87  return 10e10;
88  return erreur / Nbpoint;
89 }
90 
92 {
93  double erreur = 0;
94  double IW;
95  unsigned int Nbpoint = 0;
96 
97  if (pyrInitialised) {
100  }
101 
102  Warp->computeCoeff(tp);
103  for (unsigned int point = 0; point < templateSize; point++) {
104  int i = ptTemplate[point].y;
105  int j = ptTemplate[point].x;
106  X1[0] = j;
107  X1[1] = i;
108  Warp->computeDenom(X1, tp);
109  Warp->warpX(X1, X2, tp);
110 
111  double j2 = X2[0];
112  double i2 = X2[1];
113  if ((j2 < I.getWidth() - 1) && (i2 < I.getHeight() - 1) && (i2 > 0) && (j2 > 0)) {
114  double Tij = ptTemplate[point].val;
115  IW = I.getValue(i2, j2);
116  erreur += (Tij - IW) * (Tij - IW);
117  Nbpoint++;
118  }
119  }
120  if (Nbpoint == 0)
121  return 10e10;
122  return erreur / Nbpoint;
123 }
124 END_VISP_NAMESPACE
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
unsigned int getWidth() const
Definition: vpImage.h:242
Type getValue(unsigned int i, unsigned int j) const
unsigned int getHeight() const
Definition: vpImage.h:181
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:287
double getSSD(const vpImage< unsigned char > &I, const vpColVector &tp)
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
VP_EXPLICIT vpTemplateTrackerSSD(vpTemplateTrackerWarp *warp)
virtual void warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)=0
vpTemplateTrackerPoint ** ptTemplatePyr
unsigned int * templateSizePyr
vpTemplateTrackerPoint * ptTemplate
vpTemplateTrackerWarp * Warp
vpImage< double > BI
unsigned int templateSize