Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Tracker.hh
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 #ifndef __Tracker_hh__
26 #define __Tracker_hh__
27 #include <Base/Common/BIASpragmaStart.hh>
28 
29 #include <vector>
30 
31 #include <Base/Math/Vector.hh>
32 #include <Base/Geometry/HomgPoint2D.hh>
33 #include <Image/PyramidImageInterface.hh>
34 #include <Filter/GradientSobel3x3.hh>
35 #include <Filter/GradientGauss.hh>
36 #include <Filter/GradientGaussAsymmetric.hh>
37 #include <Filter/Gauss.hh>
38 #include <Filter/Mean.hh>
39 #include <Filter/FilterMask.hh>
40 
41 #include "TrackerBaseInterface.hh"
42 
43 #include <deque>
44 
45 #define D_TRACKER_INIT 0x00000001
46 #define D_TRACKER_TRACK 0x00000002
47 #define D_TRACKER_PARAM 0x00000004
48 #define D_TRACKER_WRITE_IM 0x00000008
49 #define D_TRACKER_FILL_UP 0x00000010
50 #define D_TRACKER_IMAGES 0x00000020
51 #define D_TRACKER_NUMITER 0x00000040
52 #define D_TRACKER_ERROR 0x00000080
53 
54 namespace BIAS{
55 
56  /** defines type or combination of tracker(s) used: simple/weighted estimate
57  a dx,dy, Affine estimate an affine transformation */
61 
62  /** @class Tracker
63  @ingroup g_tracker
64 
65  @brief High level tracking class. Provides a convenient interface for
66  tracking tasks using pyramid images.
67 
68  See ExampleTracker3 for brief exemplary usage.
69 
70  @author woelk */
71  template <class StorageType, class CalculationType>
72  class BIASMatcher2D_EXPORT Tracker : public Debug
73  {
74  public:
75  /** @name Initialization functions */
76  //@{
77 
78  /// Default constructor without param object
79  Tracker(int tracker_type = TT_Simple);
80  virtual ~Tracker();
81 
82  /** Clears the internal data structures, i.e. POints_, POints2_, NumIter_,
83  DisplInLastIter_, ResiduiMAD_, ResiduiMSD_, res_ and Cov_.
84  @author woelk 09/2004 */
85  void ClearPoints();
86 
87  /** Calls ClearPoints and sets pointer to last image and gradients to zero.
88  @author streckel 11/2004 */
89  void Reset();
90 
91  //@}
92 
93  /** @name Point manipulation functions */
94  //@{
95 
96  /** Replaces points to which no correspondence could be established in
97  the internal data structures. These "lost" points are marked in the
98  internal data structure by setting the last component (w) to
99  zero (0.0).
100  Traverses the argument list subsequently and makes use of as many
101  points as necessary. Only points further away than *MinBorderDist_
102  from roi border are added to internal data structures.
103 
104  If "ghost tracks" appear, try to make the corner detector aware of
105  points which are already tracked :
106  \code
107  CornerDetector cd;
108  Tracker tr;
109  vector<HomgPoint2D> pt
110  tr.GetPoints(pt);
111  cd.Detect(image, pt, qual);
112  tr.FillUpPoints(pt, qual);
113  \endcode
114  See also documentation of CornerDetectorGradient::Detect().
115 
116  The argument "qual" refers to the quality from the corner detector.
117 
118  @returns number of used points, negative on error.
119 
120  @author woelk 09/2004 */
121  int FillUpPoints(const std::vector<HomgPoint2D>& points,
122  const std::vector<float>& qual);
123 
124 
125  /** Place every point from points which is not at infinity, in the internal
126  data structure Points_.
127  @author woelk 09/2004 */
128  int ReplacePoints(const std::vector<HomgPoint2D>& points,
129  const std::vector<float>& qual);
130 
131  /** Place every point from points even those at infinity, in the internal
132  data structure Points_.
133  @author woelk 09/2004 */
134  int ReplaceAllPoints(const std::vector<HomgPoint2D>& points,
135  const std::vector<float>& qual);
136 
137  //@}
138 
139 
140  /** @name Tracking and image preparation functions
141  These functions need to be called for every new image. */
142  //@{
143 
144  /** Prepares the pyramid images:
145  Filters im to pim[0], downsamples pim[0] and
146  calculates the gradients gx and gy of pim in every pyramid level.
147  @author woelk 09/2004 */
148  int PreparePyramide(const Image<StorageType>& im,
154 
155  /** Prepare pyramid using default gradient and lowpass filter. */
156  int PreparePyramide(const Image<StorageType>& im,
160 
161  /** Prepares the pyramid images. Does neither lowpass filtering,
162  nor gradient computation. */
163  int PreparePyramide(const Image<StorageType>& im,
165 
166  /** Tracks all points from the internal data structure Points_.
167  @author woelk 09/2004 */
168  virtual int Track(PyramidImageInterface<CalculationType>& pim,
171 
172  /** tracks all points in Points_
173  using prediction as previous knowledge
174  @author woelk 09/2004 */
175  virtual int Track(PyramidImageInterface<CalculationType>& pim,
178  const std::vector<HomgPoint2D>& prediction,
179  const std::vector<Matrix2x2<KLT_TYPE> >&affineprediction=
180  std::vector<Matrix2x2<KLT_TYPE> >());
181 
182  /** Tracks all points from the internal data structure Points_.
183  @author woelk 09/2004 */
184  virtual int Track(PyramidImageInterface<CalculationType>& pim);
185 
186  /** tracks all points in Points_
187  using prediction as previous knowledge
188  @author woelk 09/2004 */
189  virtual int Track(PyramidImageInterface<CalculationType>& pim,
190  const std::vector<HomgPoint2D>& prediction,
191  const std::vector<Matrix2x2<KLT_TYPE> >&affineprediction=
192  std::vector<Matrix2x2<KLT_TYPE> >());
193 
194  //@}
195 
196  /** This function allows the replacement of the last images. It is intended
197  to be used when all points in the current image should be mapped
198  against different reference image for every point. A set of
199  "last images" is constructed from patches
200  @author woelk 02/2006 */
201  void ReplaceLastImages(PyramidImageInterface<CalculationType> *pim,
204 
205 
206  /** @name Result access functions
207  Functions for accessing results like point position, number of
208  iterations, ... */
209  //@{
210 
211  /** returns the point positions in the actual image
212  points at infinity (w=0.0) are returned, if tracking failed
213  @author woelk */
214  inline void GetPoints(std::vector<HomgPoint2D>& p) const
215  { p = Points_[PyIndex_]; }
216 
217  /** @brief returns the number of iterations used in the biggest
218  pyramid level
219  @author woelk 09/2004 */
220  inline void GetNumIter(std::vector<int>& numiter) const
221  { numiter = NumIter_[PyIndex_]; }
222 
223  /** @brief returns the displacement in the last iteration on the biggest
224  pyramid level.
225  @author woelk 09/2004 */
226  inline void GetError(std::vector<double>& error) const
227  { error = DisplInLastIter_[PyIndex_]; }
228 
229  /** @brief returns the mean grey level difference in the tracking window
230  on the biggest pyramid level.
231  @author woelk 09/2004 */
232  inline void GetResiduiMAD(std::vector<double>& residui) const
233  { residui = ResiduiMAD_[PyIndex_]; }
234 
235  /** @brief returns the squared mean grey level difference in the tracking
236  window on the biggest pyramid level.
237  @author woelk 09/2004 */
238  inline void GetResiduiMSD(std::vector<double>& residui) const
239  { residui = ResiduiMSD_[PyIndex_]; }
240 
241  /** @brief returns the results from the base tracker class on the
242  biggest pyramid level.
243  0 on success (error< maxerror) - the point was tracked
244  -1 when no spatial structure is present
245  -2 when point slid out of image
246  -3 when maxiter is reached an error is above maxerror
247  -4 when a point lies outside of valid region in images
248  -5 when the mean absolute difference between the two poinst
249  -6 when input point is at infinity
250  1 when error increased from last iteration
251  @author woelk 09/2004 */
252  inline void GetResults(std::vector<int>& results) const
253  { results = res_[PyIndex_]; }
254 
255  /** @brief returns estimated affine matrices */
256  inline void GetAffineEstimates(std::vector<Matrix2x2<double> >& results) {
257  results = AffineResults_[PyIndex_];
258  }
259 
260  /** @brief returns covariance matrices for biggest pyramid level.
261  @author woelk 06/2005 */
262  inline void GetCovariances(std::vector<Matrix<double> >& cov) const
263  { cov = Cov_[PyIndex_]; }
264 
265  //@}
266 
267  /** @brief write a track file in Birchfeld-style.
268 
269  The file is independent of internal data structures.
270  p[i][k] is point number k in image i and
271  results[i][k] is result for point number k in image i.
272 
273  @author woelk 09/2004 */
274  int WriteTrackFile(std::deque<std::vector<HomgPoint2D> >& p,
275  std::deque<std::vector<int> >& results,
276  const std::string& name);
277 
278  /////////////////////////////////////////////////////////////////////////
279 
280  /** @name Parameter access
281  Functions for accessing the parameter */
282  //@{
283 
284  /** @brief return trackerbase type currently active */
285  int GetTrackerType();
286 
287  void SetTrackerType(int tracker_type);
288 
289  inline enum ERejectionType GetRejectionType()
290  { return (enum ERejectionType)(tb_->GetRejectionType()); }
291 
292  inline void SetRejectionType(int rejection_type)
293  { tb_->SetRejectionType(rejection_type); }
294 
295  inline void SetAffineBrightnessInvariance(bool bi) {
296  tb_->SetAffineBrightnessInvariance(bi);
297  }
298 
299  inline const Vector<int>& GetHalfWinSize()
300  { return HalfWinSize_; }
301 
302  inline void SetHalfWinSize(const Vector<int>& hws)
303  { HalfWinSize_ = hws; }
304 
305  inline void SetWeightMatrix(const std::vector< Matrix<KLT_TYPE> >& matrices)
306  { WeightMatrices_ = matrices; }
307 
308  inline void SetMaxError(const Vector<double>& max_error)
309  { MaxError_ = max_error; }
310 
311  /** !!!
312  Also used for X84M as maximal residuum. That is features with a
313  residuum <= 'maxres' will be accept automatically!
314  !!!
315  **/
316  inline void SetMaxResiduumMAD(const Vector<KLT_TYPE>& maxres)
317  { MaxResiduumMAD_=maxres; }
318 
319  inline void SetMaxIterations(const Vector<int>& max_iter)
320  { MaxIter_ = max_iter; }
321 
322  inline void SetCoarsePointUsage(const bool &UseCoarse) {
323  UseCoarserPointIfLost_ = UseCoarse;
324  }
325 
326  inline void SetLowpassMask(const FilterMask& mask) {
327  LowpassFM_ = mask;
328  }
329  inline void SetGradXMask(const FilterMask& mask) {
330  GradXFM_ = mask;
331  }
332  inline void SetGradYMask(const FilterMask& mask) {
333  GradYFM_ = mask;
334  }
335 
336  inline void SetLowpassMask(Vector<double>& vec, bool separable) {
337  FilterMask fm;
338  Vector2FilterMask_(vec, fm, separable);
339  SetLowpassMask(fm);
340  }
341  inline void SetGradXMask(Vector<double>& vec, bool separable) {
342  FilterMask fm;
343  Vector2FilterMask_(vec, fm, separable);
344  SetGradXMask(fm);
345  }
346  inline void SetGradYMask(Vector<double>& vec, bool separable) {
347  FilterMask fm;
348  Vector2FilterMask_(vec, fm, separable);
349  SetGradYMask(fm);
350  }
351 
352  //@}
353 
354 #ifdef BIAS_DEBUG
355  void DumpResults(std::ostream& os=std::cout) {
356  const int pysize = (int)LastImage_->size()-1;
357  const int maxf = (int)Points_[PyIndex_].size();
358  //const int maxf=25;
359  const int pyind=PyIndex_;
360  for (int i=0; i<maxf; i++){
361  os << std::setw(5) << i << " : ";
362  for (int p=pysize; p>=pyind; p--){
363  os << " " << Points_[p][i] << std::setw(4) << res_[p][i];
364  }
365  os << std::endl;
366  }
367  }
368 #endif
369 
370  protected:
371  /// the base tracker
373 
374  /// the gradient filter
376 
377  /// the lowpass filter
379 
380  /// the parameter
385  int PyIndex_;
386  /// set if you want to keep track of points regardless of trackability
387  /// in highest resolution
389 
390  /// temporary data
391  std::vector<HomgPoint2D> p2tracked_;
392  /// result code for every point
393  std::vector<std::vector<int> > res_;
394  /// the prediction for every pyramid image
395  std::vector<std::vector<HomgPoint2D> > PredictedPoints_;
396  /// the point position in the actual image
397  std::vector<std::vector<HomgPoint2D> > Points_;
398  /// number of iterations used
399  std::vector<std::vector<int> > NumIter_;
400  /// displacement inlast iteration
401  std::vector<std::vector<double> > DisplInLastIter_;
402  /// mean absolute grey level difference (MAD) in tracking window
403  std::vector<std::vector<double> > ResiduiMAD_;
404  /// mean squared grey level difference (MSD) in tracking window
405  std::vector<std::vector<double> > ResiduiMSD_;
406  /// covariance matrices
407  std::vector<std::vector<Matrix<double> > > Cov_;
408  /// results of affine tracking
409  std::vector<std::vector<Matrix2x2<double> > > AffineResults_;
410  /// prediction for affine warping, same for all pyramid levels
411  std::vector<Matrix2x2<double> > AffinePrediction_;
412  // the filter masks
413  FilterMask LowpassFM_, GradXFM_, GradYFM_;
414  // vector for weight matrices for different pyramid levels
415  std::vector< BIAS::Matrix<KLT_TYPE> > WeightMatrices_;
416 
417  // the pyramid images from last call to Track
420 
421  // adapts the internal memory to new pyramid size if necessary
422  void ResizeInternals_(const int pysize);
423 
424  /** adapts the internal memory to new pyramid size and new
425  point count if necessary.
426  @author woelk 02/2006 */
427  void ResizeInternals_(const int pysize, const int num_points);
428 
429  // reserves point size space in every pyramid level
430  void ReserveInternals_(const int pysize, const int pointsize);
431 
432  /// sets all points with index>=start in the internal data structures
433  /// to infinity
434  void PadPoints_(const int start);
435 
436  /** logs reasons for lost tracks into tracker.log */
437  void WriteTrackLog_(int num);
438 
439  void DetermineROI_(int &tlx, int &tly, int &brx, int &bry) const ;
440 
441  void Vector2FilterMask_(Vector<double>& input, FilterMask& mask, bool separable);
442  };
443 }
444 
445 #include <Base/Common/BIASpragmaEnd.hh>
446 
447 #endif // __Tracker_hh__
448 
ETrackerType
defines type or combination of tracker(s) used: simple/weighted estimate a dx,dy, Affine estimate an ...
Definition: Tracker.hh:58
std::vector< HomgPoint2D > p2tracked_
temporary data
Definition: Tracker.hh:391
void GetResiduiMSD(std::vector< double > &residui) const
returns the squared mean grey level difference in the tracking window on the biggest pyramid level...
Definition: Tracker.hh:238
void DumpResults(std::ostream &os=std::cout)
Definition: Tracker.hh:355
Vector< int > MaxIter_
the parameter
Definition: Tracker.hh:381
void SetMaxIterations(const Vector< int > &max_iter)
Definition: Tracker.hh:319
FilterMask LowpassFM_
Definition: Tracker.hh:413
void SetWeightMatrix(const std::vector< Matrix< KLT_TYPE > > &matrices)
Definition: Tracker.hh:305
const Vector< int > & GetHalfWinSize()
Definition: Tracker.hh:299
std::vector< std::vector< double > > ResiduiMSD_
mean squared grey level difference (MSD) in tracking window
Definition: Tracker.hh:405
std::vector< Matrix2x2< double > > AffinePrediction_
prediction for affine warping, same for all pyramid levels
Definition: Tracker.hh:411
void GetResiduiMAD(std::vector< double > &residui) const
returns the mean grey level difference in the tracking window on the biggest pyramid level...
Definition: Tracker.hh:232
void GetResults(std::vector< int > &results) const
returns the results from the base tracker class on the biggest pyramid level.
Definition: Tracker.hh:252
TrackerBaseInterface< CalculationType > * tb_
the base tracker
Definition: Tracker.hh:372
PyramidImageInterface< CalculationType > * LastImage_
Definition: Tracker.hh:418
std::vector< std::vector< HomgPoint2D > > Points_
the point position in the actual image
Definition: Tracker.hh:397
bool UseCoarserPointIfLost_
set if you want to keep track of points regardless of trackability in highest resolution ...
Definition: Tracker.hh:388
Vector< KLT_TYPE > MaxError_
Definition: Tracker.hh:382
void SetCoarsePointUsage(const bool &UseCoarse)
Definition: Tracker.hh:322
void GetNumIter(std::vector< int > &numiter) const
returns the number of iterations used in the biggest pyramid level
Definition: Tracker.hh:220
Vector< int > HalfWinSize_
Definition: Tracker.hh:384
void SetGradYMask(const FilterMask &mask)
Definition: Tracker.hh:332
PyramidImageInterface< CalculationType > * LastGradY_
Definition: Tracker.hh:419
std::vector< std::vector< int > > res_
result code for every point
Definition: Tracker.hh:393
void SetMaxResiduumMAD(const Vector< KLT_TYPE > &maxres)
!!! Also used for X84M as maximal residuum.
Definition: Tracker.hh:316
void GetCovariances(std::vector< Matrix< double > > &cov) const
returns covariance matrices for biggest pyramid level.
Definition: Tracker.hh:262
void SetHalfWinSize(const Vector< int > &hws)
Definition: Tracker.hh:302
std::vector< std::vector< double > > ResiduiMAD_
mean absolute grey level difference (MAD) in tracking window
Definition: Tracker.hh:403
std::vector< std::vector< Matrix2x2< double > > > AffineResults_
results of affine tracking
Definition: Tracker.hh:409
void SetGradXMask(Vector< double > &vec, bool separable)
Definition: Tracker.hh:341
void SetGradYMask(Vector< double > &vec, bool separable)
Definition: Tracker.hh:346
void SetLowpassMask(const FilterMask &mask)
Definition: Tracker.hh:326
std::vector< std::vector< HomgPoint2D > > PredictedPoints_
the prediction for every pyramid image
Definition: Tracker.hh:395
void GetAffineEstimates(std::vector< Matrix2x2< double > > &results)
returns estimated affine matrices
Definition: Tracker.hh:256
void GetError(std::vector< double > &error) const
returns the displacement in the last iteration on the biggest pyramid level.
Definition: Tracker.hh:226
void SetGradXMask(const FilterMask &mask)
Definition: Tracker.hh:329
void SetAffineBrightnessInvariance(bool bi)
Definition: Tracker.hh:295
void SetMaxError(const Vector< double > &max_error)
Definition: Tracker.hh:308
The image template class for specific storage types.
Definition: Image.hh:78
FilterNTo2N< CalculationType, CalculationType > * grad_
the gradient filter
Definition: Tracker.hh:375
std::vector< BIAS::Matrix< KLT_TYPE > > WeightMatrices_
Definition: Tracker.hh:415
std::vector< std::vector< Matrix< double > > > Cov_
covariance matrices
Definition: Tracker.hh:407
void GetPoints(std::vector< HomgPoint2D > &p) const
returns the point positions in the actual image points at infinity (w=0.0) are returned, if tracking failed
Definition: Tracker.hh:214
void SetLowpassMask(Vector< double > &vec, bool separable)
Definition: Tracker.hh:336
std::vector< std::vector< double > > DisplInLastIter_
displacement inlast iteration
Definition: Tracker.hh:401
FilterNToN< StorageType, CalculationType > * lowpass_
the lowpass filter
Definition: Tracker.hh:378
void SetRejectionType(int rejection_type)
Definition: Tracker.hh:292
Vector< KLT_TYPE > MaxResiduumMAD_
Definition: Tracker.hh:383
A filter mask (or a kernel) used for convolution.
Definition: FilterMask.hh:61
High level tracking class.
Definition: Tracker.hh:72
std::vector< std::vector< int > > NumIter_
number of iterations used
Definition: Tracker.hh:399