Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackerBaseSimple.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 "TrackerBaseSimple.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 
41 #ifdef TIMING
42  TimeMeasure timer;
43  timer.Start();
44 #endif
45 
46  if (_HalfWinSize != _LastHalfWinSize){
47  this->Resize_(_HalfWinSize);
48  }
49  BIASCDOUT(D_TRACKERB_KLT,"trying to track p1: (" << p1[0] << ", " << p1[1]
50  << ") <--> p2: (" <<p2[0] << ", " << p2[1]<<")\n");
51 
52  int res=0;
53  int rh, ch;
54  register KLT_TYPE dI, ex, ey, dx, dy, det;
55  //KLT_TYPE lsterror=FLT_MAX;
56  const int hws=_HalfWinSize;
57  const int winsize=_WinSize;
58  const int maxiter=_MaxIterations;
59  const KLT_TYPE maxerr=_MaxError;
60 
61  BIASCDOUT(D_TRACKERB_INIT, "using hws: "<<hws<<"\tmaxiter: "<<maxiter
62  <<"\tmaxerr: "<<maxerr<<endl);
63 
64  if (p1[0]<_minx1 || p1[0]>=_maxx1 ||
65  p1[1]<_miny1 || p1[1]>=_maxy1 ||
66  p2[0]<_minx2 || p2[0]>=_maxx2 ||
67  p2[1]<_miny2 || p2[1]>=_maxy2 ){
68  BIASCDOUT(D_TRACKERB_KLT, "p1 ("<<p1[0]<<", "<<p1[1]<<") p2 ("<<p2[0]
69  <<", "<<p2[1]<<")\n");
70  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
71  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
72  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
73  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
74  BIASCDOUT(D_TRACKERB_KLT, "points do not lie in ROI, returning -4\n");
75  return TRACKER_OUTOFROI;
76  } else {
77  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
78  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
79  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
80  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
81  }
82 
83  iter=0;
84 
85  result[0]=p2[0];
86  result[1]=p2[1];
87  BilinearRegion1_(p1[0],p1[1],hws);
88  BIASCDOUT(D_TRACKERB_BILINEAR, "_bl1: "<<_bl1<<"\n_gx1: "<<_gx1
89  <<"\n_gy1: "<<_gy1<<endl);
90 #ifdef TIMING
91  timer.Stop();
92  cerr << "TrackerBase MyBilinearRegion1_ took "<<timer.GetRealTime()
93  <<" us = "<<timer.GetCycleCount()<<" cycles \n";
94  timer.Reset();
95  timer.Start();
96 #endif
97 
98  do {
99  _gxx=_gxy=_gyy=ex=ey=0.0;
100  BilinearRegion2_(result[0],result[1],hws);
101  BIASCDOUT(D_TRACKERB_BILINEAR, "_bl2: "<<_bl2<<"\n_gx2: "<<_gx2
102  <<"\n_gy2: "<<_gy2<<endl);
103 
104  // initialize G and e
105  for (rh=0; rh<winsize; rh++){
106  for (ch=0; ch<winsize; ch++){
107  _gsx[rh][ch] = _gx1[rh][ch] + _gx2[rh][ch];
108  _gsy[rh][ch] = _gy1[rh][ch] + _gy2[rh][ch];
109  _gxx += _gsx[rh][ch]*_gsx[rh][ch];
110  _gyy += _gsy[rh][ch]*_gsy[rh][ch];
111  _gxy += _gsx[rh][ch]*_gsy[rh][ch];
112  dI = _bl1[rh][ch] - _bl2[rh][ch];
113  ex += _gsx[rh][ch]*dI;
114  ey += _gsy[rh][ch]*dI;
115 #ifdef BIAS_DEBUG
116  if (BIAS_ISNAN(_gxx) || BIAS_ISINF(_gxx) ||
117  BIAS_ISNAN(_gxy) || BIAS_ISINF(_gxy) ||
118  BIAS_ISNAN(_gyy) || BIAS_ISINF(_gyy) ) {
119  cout << "_gx1: "<<_gx1<<"\n_gx2: "<<_gx2
120  << "\n_gy1: "<<_gy1<<"\n_gy2: "<<_gy2
121  << "\n_bl1: "<<_bl1<<"\n_bl2: "<<_bl2
122  << "\np1: "<<p1[0]<<" "<<p1[1]<<endl
123  << "\np2: "<<p2[0]<<" "<<p2[1]<<endl
124  << "\nroi1 ("<<_minx1<<", "<<_miny1<<") <--> ("
125  <<_maxx1<<", "<<_maxy1<<")"
126  << "\nroi2 ("<<_minx2<<", "<<_miny2<<") <--> ("
127  <<_maxx2<<", "<<_maxy2<<")\n";
128  }
129  BIASASSERT(!BIAS_ISNAN(_gxx) && !BIAS_ISINF(_gxx));
130  BIASASSERT(!BIAS_ISNAN(_gxy) && !BIAS_ISINF(_gxy));
131  BIASASSERT(!BIAS_ISNAN(_gyy) && !BIAS_ISINF(_gyy));
132 #endif
133  }
134  }
135  BIASCDOUT(D_TRACKERB_KLT, "iter: "<<iter<<" _gxx: "<<_gxx<<" _gxy: "<<_gxy
136  <<" _gyy: "<<_gyy<<" ex: "<<ex<<" ey: "<<ey<<endl);
137  det = _gxx*_gyy-_gxy*_gxy;
138  if (fabs(det)<SMALL_DET) {
139  BIASDOUT(D_TRACKERB_KLT,
140  "unable to invert G, det: "<<det);
141  res = TRACKER_NOSTRUCTURE;
142  break;
143  }
144  _idet=1.0/det;
145  dx = (_gyy*ex - _gxy*ey)*_idet;
146  dy = (_gxx*ey - _gxy*ex)*_idet;
147 
148  BIASCDOUT(D_TRACKERB_KLT, " det: "<<det<<" _idet: "<<_idet
149  <<" dx: "<<dx<<" dy: "<<dy<<endl);
150 
151  //error = sqrt(dx*dx + dy*dy);
152  error = (fabs(dx)>fabs(dy))?fabs(dx):fabs(dy);
153 
154  /*
155  if (error>lsterror){
156  BIASCDOUT(D_TRACKERB_KLT,
157  "error increased ("
158  <<lsterror<<" -> "<<error
159  <<"), stopping iteration and returning 1\n");
160  res = TRACKER_ERRORINCREASED;
161  error=lsterror;
162  break;
163  } */
164  //lsterror=error;
165 
166  result[0] += dx;
167  result[1] += dy;
168 
169  BIASCDOUT(D_TRACKERB_KLT," disp after "<<iter<<" iteration: "
170  <<result[0]-p2[0]<<", "<<result[1]-p2[1]<<" error: "<< error
171  << " ("<<result[0]<<", "<<result[1]<<")\n");
172 
173  //#define D_TRACK
174 #ifdef D_TRACK
175  fprintf(stdout, "iter: %i\t_gxx: %f\t_gxy: %f\t_gyy: %f\tex: %f\tey: %f\n",
176  iter, _gxx, _gxy, _gyy, ex, ey);
177  fprintf(stdout, " \tdx: %f\tdy: %f\tcoo: (%f, %f)\n",
178  dx, dy, result[0], result[1]);
179 #endif
180 
181  iter++;
182 
183  if (result[0]<_minx2 || result[0]>=_maxx2 ||
184  result[1]<_miny2 || result[1]>=_maxy2){
185  BIASCDOUT(D_TRACKERB_KLT,
186  "feature slid out of image, returning -2");
187  res = TRACKER_OUTOFIMAGE;
188  break;
189  }
190  } while (error>maxerr && iter<maxiter);
191 
192  if (res==0 && error>maxerr && iter==maxiter) {
193  res = TRACKER_TOOMANYITER;
194  BIASCDOUT(D_TRACKERB_KLT,"error > "<<maxerr
195  <<" and max iteration count reached: "<<iter<<" returning -3");
196  } else if (res==0) {
197  BilinearRegion2_(result[0],result[1],hws);
198  }
199 
200 #ifdef TIMING
201  timer.Stop();
202  cerr << "i "<<iter<<" res "<<res<<" ";
203  cerr << "TrackSimple_ took "<<timer.GetRealTime()<<" us = "
204  <<timer.GetCycleCount()<<" cycles\n";
205 #endif
206  return res;
207 }
208 
209 //////////////////////////////////////////////////////////////////////////
210 // instantiation
211 //////////////////////////////////////////////////////////////////////////
212 namespace BIAS{
213 template class TrackerBaseSimple<unsigned char>;
214 template class TrackerBaseSimple<float>;
215 
216 // fill in instances as required
217 #ifdef BUILD_IMAGE_INT
218 template class TrackerBaseSimple<int>;
219 #endif
220 #ifdef BUILD_IMAGE_CHAR
221 template class TrackerBaseSimple<char>;
222 #endif
223 #ifdef BUILD_IMAGE_SHORT
224 template class TrackerBaseSimple<short>;
225 #endif
226 #ifdef BUILD_IMAGE_USHORT
227 template class TrackerBaseSimple<unsigned short>;
228 #endif
229 #ifdef BUILD_IMAGE_UINT
230 template class TrackerBaseSimple<unsigned int>;
231 #endif
232 #ifdef BUILD_IMAGE_DOUBLE
233 #endif
234 }
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.
point slid out of image
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
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111
maxiter is reached and error is above maxerror