Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackerBaseWeighted1D.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "TrackerBaseWeighted1D.hh"
26 
27 using namespace BIAS;
28 using namespace std;
29 
30 #define SMALL_DET 1e-8
31 
32 template <class StorageType>
35  Vector2<KLT_TYPE>& result, KLT_TYPE& error,
36  int &iter,
37  const Matrix2x2<KLT_TYPE>& AffinePred,
38  Matrix2x2<KLT_TYPE>& AffineResult)
39 {
40  CalcGaussianWeightMatrix_();
41  BIASCDOUT(D_TRACKERB_KLT,"p1: " << p1[0] << ", " << p1[1]
42  << "\tp2: " <<p2[0] << ", " << p2[1]<<endl);
43 
44  int res=TRACKER_SUCCESS;
45  int rh, ch;
46 
47  register KLT_TYPE dI, ex, ey, dx, det;
48  //KLT_TYPE lsterror=FLT_MAX;
49  const int hws=_HalfWinSize;
50  const int winsize=_WinSize;
51  const int maxiter=_MaxIterations;
52  const KLT_TYPE maxerr=_MaxError;
53 
54  if (p1[0]<_minx1 || p1[0]>=_maxx1 ||
55  p1[1]<_miny1 || p1[1]>=_maxy1 ||
56  p2[0]<_minx2 || p2[0]>=_maxx2 ||
57  p2[1]<_miny2 || p2[1]>=_maxy2 ){
58  BIASCDOUT(D_TRACKERB_KLT, "p1 ("<<p1[0]<<", "<<p1[1]<<") p2 ("<<p2[0]
59  <<", "<<p2[1]<<")\n");
60  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
61  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
62  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
63  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
64  BIASCDOUT(D_TRACKERB_KLT, "points do not lie in ROI, returning -4\n");
65  return TRACKER_OUTOFROI;
66  } else {
67  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
68  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
69  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
70  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
71  }
72 
73  iter=0;
74 
75  result[0]=p2[0];
76  result[1]=p2[1];
77  // MyBilinearRegion(_ida1,_gradx1,_grady1,p1[0],p1[1],hws,_bl1,_gx1,_gy1);
78  BilinearRegion1_(p1[0],p1[1],hws);
79  do {
80  _gxx=_gxy=_gyy=ex=ey=0.0;
81  // MyBilinearRegion(_ida2,_gradx2,_grady2,result[0],result[1],hws,
82  // _bl2,_gx2,_gy2);
83  BilinearRegion2_(result[0],result[1],hws);
84  // initialize G and e
85  for (rh=0; rh<winsize; rh++){
86  for (ch=0; ch<winsize; ch++){
87  _gsx[rh][ch] =
88  _weight[rh][ch] * (_gx1[rh][ch] + _gx2[rh][ch]);
89  _gxx += _gsx[rh][ch]*_gsx[rh][ch];
90  dI = _weight[rh][ch] * (_bl1[rh][ch] - _bl2[rh][ch]);
91  ex += _gsx[rh][ch]*dI;
92  }
93  }
94  BIASCDOUT(D_TRACKERB_KLT, "_gxx: "<<_gxx<<" _gxy: "<<_gxy
95  <<" _gyy: "<<_gyy<<" ex: "<<ex<<" ey: "<<ey<<endl);
96 
97  // regularize gradient
98  if (this->_AffineBrightnessInvariance) {
99  det = _gxx + 0.5;
100  } else {
101  det = _gxx + 10.0;
102  }
103  if (fabs(det)<SMALL_DET) {
104  BIASCDOUT(D_TRACKERB_KLT,
105  "unable to invert G, det: "<<det<<endl);
106 
107  res = TRACKER_NOSTRUCTURE;
108  break;
109  }
110  _idet = 1.0/_gxx;
111  dx = ex*_idet;
112 
113  BIASCDOUT(D_TRACKERB_KLT, "det: "<<det<<" _idet: "<<_idet
114  <<" dx: "<<dx<<endl);
115 
116  //error = sqrt(dx*dx + dy*dy);
117  error = fabs(dx);
118  /*
119  if (error>lsterror){
120  BIASCDOUT(D_TRACKERB_KLT, "error increased ("
121  <<lsterror<<" -> "<<error
122  <<"), stopping iteration and returning 1\n");
123  res = TRACKER_ERRORINCREASED;
124  error=lsterror;
125  break;
126  }
127  */
128  //lsterror=error;
129 
130  result[0] += dx;
131 
132  BIASCDOUT(D_TRACKERB_KLT,"disp after "<<iter<<" iteration: "
133  <<result[0]-p2[0]<<", "<<result[1]-p2[1]<<" error: "<< error
134  << " ("<<result[0]<<", "<<result[1]<<")\n");
135 
136  iter++;
137 
138  if (result[0]<_minx2 || result[0]>=_maxx2 ||
139  result[1]<_miny2 || result[1]>=_maxy2){
140  BIASCDOUT(D_TRACKERB_KLT,
141  "feature slid out of image, returning -2\n");
142  res = TRACKER_OUTOFIMAGE;
143  break;
144  }
145  } while (error>maxerr && iter<maxiter);
146 
147  if (res==0 && error>maxerr) {
148  res = TRACKER_TOOMANYITER;
149  BIASCDOUT(D_TRACKERB_KLT,"error > maxerror : returning -3\n");
150  } else if (res==0) {
151  BilinearRegion2_(result[0],result[1],hws);
152  }
153 
154 #ifdef TIMING
155  timer.Stop();
156  cerr << "i "<<iter<<" res "<<res<<" ";
157  cerr << "TrackWeighted_ took "<<timer.GetRealTime()<<" us = "
158  <<timer.GetCycleCount()<<" cycles\n";
159 #endif
160 
161  return res;
162 }
163 
164 template <class StorageType>
167 {
168  _weight.newsize(_WinSize, _WinSize);
169  // now calculate the gaussian weight matrix
170  double sigma = ((double)_WinSize)/6.0;
171  double fac = 1.0 / (2.0 * sigma * sigma);
172  double sum=0.0, tmp;
173  register int i, j, x, y, xp, xm, yp, ym;
174  const int hws=_HalfWinSize;
175  for (i= hws; i >= 0; i--){
176  for (j= hws; j >= 0; j--){
177  tmp=exp(-(double)(i*i+j*j)*fac);
178  yp=hws+i;
179  ym=hws-i;
180  xp=hws+j;
181  xm=hws-j;
182  _weight[ym][xm] =_weight[yp][xm] = _weight[ym][xp] = _weight[yp][xp] =
183  (KLT_TYPE)(tmp);
184  }
185  }
186 
187  for (y=0; y<_WinSize; y++){
188  for (x=0; x<_WinSize; x++){
189  sum+=_weight[y][x];
190  }
191  }
192  BIASCDOUT(D_TRACKERB_INIT, "_weight: "<<_weight<<endl);
193  BIASCDOUT(D_TRACKERB_INIT, "_weight sum before normalization: "<<sum<<endl);
194 
195  //normalization
196  sum=1.0/sum; // division is slow
197  for (i= hws; i >= 0; i--){
198  for (j= hws; j >= 0; j--){
199  yp=hws+i;
200  ym=hws-i;
201  xp=hws+j;
202  xm=hws-j;
203  tmp=_weight[ym][xm];
204  _weight[ym][xm] = _weight[yp][xm] = _weight[ym][xp] =
205  _weight[yp][xp] = tmp * sum;
206  }
207  }
208  BIASCDOUT(D_TRACKERB_INIT, "_weight: "<<_weight<<endl);
209 #ifdef BIAS_DEBUG
210  sum=0.0;
211  for (y=0; y<_WinSize; y++){
212  for (x=0; x<_WinSize; x++){
213  sum+=_weight[y][x];
214  }
215  }
216  BIASCDOUT(D_TRACKERB_INIT,"_weight sum after normalization: "<<sum<<endl);
217 #endif
218 }
219 
220 
221 //////////////////////////////////////////////////////////////////////////
222 // instantiation
223 //////////////////////////////////////////////////////////////////////////
224 namespace BIAS{
226 template class TrackerBaseWeighted1D<float>;
227 
228 // fill in instances as required
229 #ifdef BUILD_IMAGE_INT
230 template class TrackerBaseWeighted1D<int>;
231 #endif
232 #ifdef BUILD_IMAGE_CHAR
233 template class TrackerBaseWeighted1D<char>;
234 #endif
235 #ifdef BUILD_IMAGE_SHORT
236 template class TrackerBaseWeighted1D<short>;
237 #endif
238 #ifdef BUILD_IMAGE_USHORT
240 #endif
241 #ifdef BUILD_IMAGE_UINT
243 #endif
244 #ifdef BUILD_IMAGE_DOUBLE
245 #endif
246 }
point slid out of image
virtual int Track_(Vector2< KLT_TYPE > &p1, Vector2< KLT_TYPE > &p2, Vector2< KLT_TYPE > &result, KLT_TYPE &error, int &iter, const Matrix2x2< KLT_TYPE > &AffinePred, Matrix2x2< KLT_TYPE > &AffineResult)
Interface of all tracking algorithms implemented in derived classes.
success (error &lt; maxerror)
no spatial structure is present
double GetCycleCount() const
return number of cycles between all subsequent calls to Start() and Stop() since last call to Reset()...
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
a point lies outside of valid region in images
maxiter is reached and error is above maxerror