ViSP  2.9.0
vpTemplateTrackerZNCC.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerZNCC.cpp 4666 2014-02-16 16:21:33Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Template tracker.
35  *
36  * Authors:
37  * Amaury Dame
38  * Aurelien Yol
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 #include <visp/vpTemplateTrackerZNCC.h>
43 
45  : vpTemplateTracker(warp), DI(), temp()
46 {
47  dW.resize(2,nbParam);
48  G.resize(nbParam);
51 
53 
54  X1.resize(2);
55  X2.resize(2);
56  DI.resize(2);
57 }
58 
59 
61 {
62  double IW,Tij;
63  int i,j;
64  double i2,j2;
65  int Nbpoint=0;
66 
67  Warp->computeCoeff(tp);
68 
69  double moyTij=0;
70  double moyIW=0;
71  for(unsigned int point=0;point<templateSize;point++)
72  {
73  i=ptTemplate[point].y;
74  j=ptTemplate[point].x;
75  X1[0]=j;X1[1]=i;
76  Warp->computeDenom(X1,tp);
77  Warp->warpX(X1,X2,tp);
78 
79  j2=X2[0];i2=X2[1];
80  if((j2<I.getWidth()-1)&&(i2<I.getHeight()-1)&&(i2>0)&&(j2>0))
81  {
82  Tij=ptTemplate[point].val;
83  if(!blur)
84  IW=I.getValue(i2,j2);
85  else
86  IW=BI.getValue(i2,j2);
87  //IW=getSubPixBspline4(I,i2,j2);
88  moyTij+=Tij;
89  moyIW+=IW;
90  Nbpoint++;
91  }
92  }
93  ratioPixelIn=(double)Nbpoint/(double)templateSize;
94  if(! Nbpoint) {
96  "Cannot get cost: size = 0")) ;
97  }
98 
99  moyTij=moyTij/Nbpoint;
100  moyIW=moyIW/Nbpoint;
101 
102  double nom=0,denom=0;
103  double var1=0,var2=0;
104  for(unsigned int point=0;point<templateSize;point++)
105  {
106  i=ptTemplate[point].y;
107  j=ptTemplate[point].x;
108  X1[0]=j;X1[1]=i;
109  Warp->computeDenom(X1,tp);
110  Warp->warpX(X1,X2,tp);
111 
112  j2=X2[0];i2=X2[1];
113  if((j2<I.getWidth()-1)&&(i2<I.getHeight()-1)&&(i2>0)&&(j2>0))
114  {
115  Tij=ptTemplate[point].val;
116  if(!blur)
117  IW=I.getValue(i2,j2);
118  else
119  IW=BI.getValue(i2,j2);
120  //IW=getSubPixBspline4(I,i2,j2);
121  nom+=(Tij-moyTij)*(IW-moyIW);
122  denom+=(Tij-moyTij)*(Tij-moyTij)*(IW-moyIW)*(IW-moyIW);
123  var1+=(IW-moyIW)*(IW-moyIW);
124  var2+=(Tij-moyTij)*(Tij-moyTij);
125 
126  Nbpoint++;
127  }
128  }
129  if(Nbpoint==0)return 10e10;
130  //return -nom/sqrt(denom);
131  return -nom/sqrt(var1*var2);
132 }
133 
134 
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:183
unsigned int getWidth() const
Definition: vpImage.h:159
vpTemplateTrackerPoint * ptTemplate
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
error that can be emited by ViSP classes.
Definition: vpException.h:76
Type getValue(double i, double j) const
Definition: vpImage.h:1029
double getCost(const vpImage< unsigned char > &I, vpColVector &tp)
vpImage< double > BI
vpTemplateTrackerZNCC(vpTemplateTrackerWarp *warp)
unsigned int templateSize
void resize(unsigned int i)
Set the size of the Row vector.
Definition: vpRowVector.h:91
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
unsigned int getHeight() const
Definition: vpImage.h:150
vpTemplateTrackerWarp * Warp
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94