Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackerBaseWeighted.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 "TrackerBaseWeighted.hh"
26 
27 using namespace BIAS;
28 using namespace std;
29 
30 #define SMALL_DET 1e-8
31 
32 template <class StorageType>
35 {
36  // set new matrix
37  _weight = weight;
38  _useGaussianWeightMatrix = false;
39 }
40 
41 template <class StorageType>
44  Vector2<KLT_TYPE>& result, KLT_TYPE& error,
45  int &iter,
46  const Matrix2x2<KLT_TYPE>& AffinePred,
47  Matrix2x2<KLT_TYPE>& AffineResult)
48 {
49  // check if a weight matrix was set before
50  if (_useGaussianWeightMatrix)
51  CalcGaussianWeightMatrix_();
52  else
53  {
54  // test if setted weight matrix has correct number of rows and cols
55  if ((int)_weight.GetRows() != _WinSize ||
56  (int)_weight.GetCols() != _WinSize)
57  {
58  BIASERR("Matrix height and width does not match window size!");
59  BIASABORT;
60  }
61  }
62 
63  BIASCDOUT(D_TRACKERB_KLT,"p1: " << p1[0] << ", " << p1[1]
64  << "\tp2: " <<p2[0] << ", " << p2[1]<<endl);
65 
66  int res=TRACKER_SUCCESS;
67  int rh, ch;
68 
69  register KLT_TYPE dI, ex, ey, dx, dy, det;
70  //KLT_TYPE lsterror=FLT_MAX;
71  const int hws=_HalfWinSize;
72  const int winsize=_WinSize;
73  const int maxiter=_MaxIterations;
74  const KLT_TYPE maxerr=_MaxError;
75 
76  if (p1[0]<_minx1 || p1[0]>=_maxx1 ||
77  p1[1]<_miny1 || p1[1]>=_maxy1 ||
78  p2[0]<_minx2 || p2[0]>=_maxx2 ||
79  p2[1]<_miny2 || p2[1]>=_maxy2 ){
80  BIASCDOUT(D_TRACKERB_KLT, "p1 ("<<p1[0]<<", "<<p1[1]<<") p2 ("<<p2[0]
81  <<", "<<p2[1]<<")\n");
82  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
83  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
84  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
85  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
86  BIASCDOUT(D_TRACKERB_KLT, "points do not lie in ROI, returning -4\n");
87  return TRACKER_OUTOFROI;
88  } else {
89  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im1 : ( "<<_minx1<<", "<<_miny1
90  <<") <--> ( "<<_maxx1<<", "<<_maxy1<<")\n");
91  BIASCDOUT(D_TRACKERB_KLT, "tracking roi im2 : ( "<<_minx2<<", "<<_miny2
92  <<") <--> ( "<<_maxx2<<", "<<_maxy2<<")\n");
93  }
94 
95  iter=0;
96 
97  result[0]=p2[0];
98  result[1]=p2[1];
99  // MyBilinearRegion(_ida1,_gradx1,_grady1,p1[0],p1[1],hws,_bl1,_gx1,_gy1);
100  BilinearRegion1_(p1[0],p1[1],hws);
101  do {
102  _gxx=_gxy=_gyy=ex=ey=0.0;
103  // MyBilinearRegion(_ida2,_gradx2,_grady2,result[0],result[1],hws,
104  // _bl2,_gx2,_gy2);
105  BilinearRegion2_(result[0],result[1],hws);
106  // initialize G and e
107  for (rh=0; rh<winsize; rh++){
108  for (ch=0; ch<winsize; ch++){
109  _gsx[rh][ch] =
110  _weight[rh][ch] * (_gx1[rh][ch] + _gx2[rh][ch]);
111  _gsy[rh][ch] =
112  _weight[rh][ch] * (_gy1[rh][ch] + _gy2[rh][ch]);
113  _gxx += _gsx[rh][ch]*_gsx[rh][ch];
114  _gyy += _gsy[rh][ch]*_gsy[rh][ch];
115  _gxy += _gsx[rh][ch]*_gsy[rh][ch];
116  dI = _weight[rh][ch] * (_bl1[rh][ch] - _bl2[rh][ch]);
117  ex += _gsx[rh][ch]*dI;
118  ey += _gsy[rh][ch]*dI;
119  }
120  }
121  BIASCDOUT(D_TRACKERB_KLT, "_gxx: "<<_gxx<<" _gxy: "<<_gxy
122  <<" _gyy: "<<_gyy<<" ex: "<<ex<<" ey: "<<ey<<endl);
123  det = _gxx*_gyy-_gxy*_gxy;
124  if (fabs(det)<SMALL_DET) {
125  BIASCDOUT(D_TRACKERB_KLT,
126  "unable to invert G, det: "<<det<<endl);
127 
128  res = TRACKER_NOSTRUCTURE;
129  break;
130  }
131  _idet=1.0/det;
132  dx = (_gyy*ex - _gxy*ey)*_idet;
133  dy = (_gxx*ey - _gxy*ex)*_idet;
134 
135  BIASCDOUT(D_TRACKERB_KLT, "det: "<<det<<" _idet: "<<_idet
136  <<" dx: "<<dx<<" dy: "<<dy<<endl);
137 
138  //error = sqrt(dx*dx + dy*dy);
139  error = (fabs(dx)>fabs(dy))?fabs(dx):fabs(dy);
140  /*
141  if (error>lsterror){
142  BIASCDOUT(D_TRACKERB_KLT, "error increased ("
143  <<lsterror<<" -> "<<error
144  <<"), stopping iteration and returning 1\n");
145  res = TRACKER_ERRORINCREASED;
146  error=lsterror;
147  break;
148  }
149  */
150  //lsterror=error;
151 
152  result[0] += dx;
153  result[1] += dy;
154 
155  BIASCDOUT(D_TRACKERB_KLT,"disp after "<<iter<<" iteration: "
156  <<result[0]-p2[0]<<", "<<result[1]-p2[1]<<" error: "<< error
157  << " ("<<result[0]<<", "<<result[1]<<")\n");
158 
159  iter++;
160 
161  if (result[0]<_minx2 || result[0]>=_maxx2 ||
162  result[1]<_miny2 || result[1]>=_maxy2){
163  BIASCDOUT(D_TRACKERB_KLT,
164  "feature slid out of image, returning -2\n");
165  res = TRACKER_OUTOFIMAGE;
166  break;
167  }
168  } while (error>maxerr && iter<maxiter);
169 
170  if (res==0 && error>maxerr) {
171  res = TRACKER_TOOMANYITER;
172  BIASCDOUT(D_TRACKERB_KLT,"error > maxerror : returning -3\n");
173  } else if (res==0) {
174  BilinearRegion2_(result[0],result[1],hws);
175  }
176 
177 #ifdef TIMING
178  timer.Stop();
179  cerr << "i "<<iter<<" res "<<res<<" ";
180  cerr << "TrackWeighted_ took "<<timer.GetRealTime()<<" us = "
181  <<timer.GetCycleCount()<<" cycles\n";
182 #endif
183 
184  return res;
185 }
186 
187 template <class StorageType>
190 {
191  _weight.newsize(_WinSize, _WinSize);
192  // now calculate the gaussian weight matrix
193  double sigma = ((double)_WinSize)/6.0;
194  double fac = 1.0 / (2.0 * sigma * sigma);
195  double sum=0.0, tmp;
196  register int i, j, x, y, xp, xm, yp, ym;
197  const int hws=_HalfWinSize;
198  for (i= hws; i >= 0; i--){
199  for (j= hws; j >= 0; j--){
200  tmp=exp(-(double)(i*i+j*j)*fac);
201  yp=hws+i;
202  ym=hws-i;
203  xp=hws+j;
204  xm=hws-j;
205  _weight[ym][xm] =_weight[yp][xm] = _weight[ym][xp] = _weight[yp][xp] =
206  (KLT_TYPE)(tmp);
207  }
208  }
209 
210  for (y=0; y<_WinSize; y++){
211  for (x=0; x<_WinSize; x++){
212  sum+=_weight[y][x];
213  }
214  }
215  BIASCDOUT(D_TRACKERB_INIT, "_weight: "<<_weight<<endl);
216  BIASCDOUT(D_TRACKERB_INIT, "_weight sum before normalization: "<<sum<<endl);
217 
218  //normalization
219  sum=1.0/sum; // division is slow
220  for (i= hws; i >= 0; i--){
221  for (j= hws; j >= 0; j--){
222  yp=hws+i;
223  ym=hws-i;
224  xp=hws+j;
225  xm=hws-j;
226  tmp=_weight[ym][xm];
227  _weight[ym][xm] = _weight[yp][xm] = _weight[ym][xp] =
228  _weight[yp][xp] = tmp * sum;
229  }
230  }
231  BIASCDOUT(D_TRACKERB_INIT, "_weight: "<<_weight<<endl);
232 #ifdef BIAS_DEBUG
233  sum=0.0;
234  for (y=0; y<_WinSize; y++){
235  for (x=0; x<_WinSize; x++){
236  sum+=_weight[y][x];
237  }
238  }
239  BIASCDOUT(D_TRACKERB_INIT,"_weight sum after normalization: "<<sum<<endl);
240 #endif
241 }
242 
243 
244 //////////////////////////////////////////////////////////////////////////
245 // instantiation
246 //////////////////////////////////////////////////////////////////////////
247 namespace BIAS{
248 template class TrackerBaseWeighted<unsigned char>;
249 template class TrackerBaseWeighted<float>;
250 
251 // fill in instances as required
252 #ifdef BUILD_IMAGE_INT
253 template class TrackerBaseWeighted<int>;
254 #endif
255 #ifdef BUILD_IMAGE_CHAR
256 template class TrackerBaseWeighted<char>;
257 #endif
258 #ifdef BUILD_IMAGE_SHORT
259 template class TrackerBaseWeighted<short>;
260 #endif
261 #ifdef BUILD_IMAGE_USHORT
263 #endif
264 #ifdef BUILD_IMAGE_UINT
265 template class TrackerBaseWeighted<unsigned int>;
266 #endif
267 #ifdef BUILD_IMAGE_DOUBLE
268 #endif
269 }
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
void SetWeightMatrix(const Matrix< KLT_TYPE > &weight)
Overwritten virtual function from TrackerBaseInterface.
maxiter is reached and error is above maxerror