TrackerStat.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #include "TrackerStat.h"
25 
26 using namespace std;
27 
28 namespace alvar {
29 using namespace std;
30 
31 TrackerStat::TrackerStat(int binsize) : f(100,90) {
32  hist.AddDimension(binsize); // x
33  hist.AddDimension(binsize); // y
34 }
35 
37  f.Reset();
38 }
39 
40 double TrackerStat::Track(IplImage *img)
41 {
42  if (img == NULL) return -1;
43  f.Track(img);
44  hist.Clear();
45  for (int p=0; p<f.prev_feature_count ; p++) {
46  for (int c=0; c<f.feature_count; c++) {
47  if (f.prev_ids[p] != f.ids[c]) continue;
48  float x = f.features[c].x - f.prev_features[p].x;
49  float y = f.features[c].y - f.prev_features[p].y;
50  hist.Inc(x, y);
51  }
52  }
53  xd = 0; yd = 0;
54  return hist.GetMax(&xd, &yd);
55 }
56 
57 void TrackerStat::Compensate(double *x, double *y) {
58  *x += xd; *y += yd;
59 }
60 
61 TrackerStatRot::TrackerStatRot(int binsize /*=8*/, int binsize_rot/*=3*/) : TrackerStat(binsize) {
62  hist_rot.AddDimension(binsize_rot);
63 }
64 
65 double TrackerStatRot::Track(IplImage *img)
66 {
67  if (img == NULL) return -1;
68  f.Track(img);
69  // Translation
70  hist.Clear();
71  for (int p=0; p<f.prev_feature_count ; p++) {
72  for (int c=0; c<f.feature_count; c++) {
73  if (f.prev_ids[p] != f.ids[c]) continue;
74  float x = f.features[c].x - f.prev_features[p].x;
75  float y = f.features[c].y - f.prev_features[p].y;
76  hist.Inc(x, y);
77  }
78  }
79  xd = 0; yd = 0;
80  double ret = hist.GetMax(&xd, &yd);
81  // Rotation
82  x_res = img->width;
83  y_res = img->height;
84  hist_rot.Clear();
85  for (int p=0; p<f.prev_feature_count ; p++) {
86  for (int c=0; c<f.feature_count; c++) {
87  if (f.prev_ids[p] != f.ids[c]) continue;
88  double x_pred = f.prev_features[p].x + xd;
89  double y_pred = f.prev_features[p].y + yd;
90  double x_curr = f.features[c].x;
91  double y_curr = f.features[c].y;
92  double x = x_curr - x_pred;
93  double y = y_curr - y_pred;
94  double theta_pred = atan2((double)y_pred-(y_res/2), (double)x_pred-(x_res/2))*180.0/3.1415926535;
95  double theta_curr = atan2((double)y_curr-(y_res/2), (double)x_curr-(x_res/2))*180.0/3.1415926535;
96  hist_rot.Inc(theta_curr-theta_pred);
97  }
98  }
99  rotd=0;
100  hist_rot.GetMax(&rotd);
101  return ret;
102 }
103 
104 void TrackerStatRot::Compensate(double *x, double *y)
105 {
106  double xx = *x - (x_res/2);
107  double yy = *y - (y_res/2);
108  double kosini = cos(rotd*3.1415926535/180);
109  double sini = sin(rotd*3.1415926535/180);
110  *x = ((kosini * xx) - (sini * yy)) + xd + (x_res/2);
111  *y = ((sini * xx) + (kosini * yy)) + yd + (y_res/2);
112 }
113 
114 } // namespace alvar
Main ALVAR namespace.
Definition: Alvar.h:174
CvPoint2D32f * features
Track result: current features
TrackerStatRot(int binsize=8, int binsize_rot=3)
Constructor.
Definition: TrackerStat.cpp:61
double Track(IplImage *img)
Track features.
f
int prev_feature_count
Track result: count of previous features
TFSIMD_FORCE_INLINE const tfScalar & y() const
void Clear()
Clear the histogram.
Definition: Util.cpp:312
HistogramSubpixel hist
Definition: TrackerStat.h:45
int * prev_ids
Track result: ID:s for previous features
CvPoint2D32f * prev_features
Track result: previous features
HistogramSubpixel hist_rot
Definition: TrackerStat.h:67
TrackerFeatures f
Definition: TrackerStat.h:44
This file implements a statistical tracker.
double Track(IplImage *img)
Translation tracker (the simplest possible)
Definition: TrackerStat.cpp:40
virtual void Compensate(double *x, double *y)
TFSIMD_FORCE_INLINE const tfScalar & x() const
int feature_count
Track result: count of current features
int * ids
Track result: ID:s for current features
int GetMax(double *dim0, double *dim1=0, double *dim2=0)
Get the maximum from the histogram This finds the maximum bin(s) and averages the original values con...
Definition: Util.cpp:332
void AddDimension(int binsize)
Add dimension with a binsize.
Definition: Util.cpp:282
void Inc(double dim0, double dim1=0, double dim2=0)
Increase the histogram for given dimensions.
Definition: Util.cpp:318
double Track(IplImage *img)
Translation + rotation tracker.
Definition: TrackerStat.cpp:65
TrackerStat deduces the optical flow based on tracked features using Seppo Valli&#39;s statistical tracki...
Definition: TrackerStat.h:42
double rotd
Track result rotation in degrees
Definition: TrackerStat.h:70
double xd
Track result x-translation in pixels
Definition: TrackerStat.h:48
double yd
Track result y-translation in pixels
Definition: TrackerStat.h:50
virtual void Compensate(double *x, double *y)
Definition: TrackerStat.cpp:57
void Reset()
Reset.
Definition: TrackerStat.cpp:36


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04