Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackerSynth.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 #ifdef WIN32
26 // WIN32 max definition conflicts with numeric_limits::max
27 # define NOMINMAX
28 # pragma warning(disable: 4251) // DLL interface
29 //# include <Base/Common/BIASpragma.hh>
30 #endif
31 
32 
33 #include "TrackerSynth.hh"
34 #include <Base/Math/Random.hh>
35 #include <Base/Common/BIASpragma.hh>
36 #include <limits>
37 
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 
43 template <class StorageType, class CalculationType>
46 {
47  Frame_ = 0;
48 }
49 
50 template <class StorageType, class CalculationType>
53 {
54 }
55 
56 template <class StorageType, class CalculationType>
61 {
62  // std::vector<std::vector<HomgPoint2D> > tmp = Points_;
63  if (Points_.size()>0){
64  for (int i=0; i<5; i++)
65  BIASCDOUT(D_TS_REPLACE, "Points_[PyIndex_]["<<i<<"] = "
66  <<Points_[PyIndex_][i]<<endl);
67  }
68 
69  int res = Tracker<StorageType, CalculationType>::Track(pim, gradx, grady);
70  // int res = 0;
71  // Points_ = tmp;
72 
73  Track_();
74  return res;
75 }
76 
77 
78 template <class StorageType, class CalculationType>
83  const std::vector<HomgPoint2D>& prediction,
84  const std::vector<Matrix2x2<KLT_TYPE> >& affineprediction)
85 {
86  // std::vector<std::vector<HomgPoint2D> > tmp = Points_;
87  if (Points_.size()>0){
88  for (int i=0; i<5; i++)
89  BIASCDOUT(D_TS_REPLACE, "Points_[*PyIndex_]["<<i<<"] = "
90  <<Points_[PyIndex_][i]<<endl);
91  }
93  Track(pim, gradx, grady, prediction, affineprediction);
94  //int res=0;
95  //Points_ = tmp;
96  Track_();
97  return res;
98 }
99 
100 
101 template <class StorageType, class CalculationType>
104 {
105  //AddDebugLevel(D_TS_REPLACE);
106  const int pyindex = PyIndex_;
107  int index;
108  double diff;
109  if ((int)Points_.size()>pyindex){
110  const int num = Points_[pyindex].size();
111  Frame_++;
112  cout << "correction : ";
113  for (int i=0; i<num; i++){
114  if (!Points_[pyindex][i].IsAtInfinity()){
115  index = FindNearest_(Points_[pyindex][i], Frame_);
116  diff = Points_[pyindex][i].Distance(Tracks_[Frame_][index]);
117  BIASCDOUT(D_TS_REPLACE, "old point "<< Tracks_[Frame_-1][index] <<" "
118  << "correcting: "<<Points_[pyindex][i]<<" --> "
119  <<Tracks_[Frame_][index]<<" = "<<diff<<endl);
120  cout << setprecision(3) << setw(8) << diff ;
121  // assert(diff<2.0);
122  Points_[pyindex][i] = Tracks_[Frame_][index];
123  }
124  }
125  cout << endl;
126  //AddGaussianNoise_();
127  //AddOutlier_();
128  }
129 }
130 
131 
132 template <class StorageType, class CalculationType>
135 {
136  Random ran;
137  const double Sigma = 0.05;
138  const int pyindex = PyIndex_;
139  if ((int)Points_.size()>pyindex){
140  const int num = (int)Points_[pyindex].size();
141  cout << "adding noise: "<<endl;
142  for (int i=0; i<num; i++){
143  if (!Points_[pyindex][i].IsAtInfinity()){
144  cout << setw(4) << i << " "<<Points_[pyindex][i]<<" --> ";
145  Points_[pyindex][i][0] += ran.GetNormalDistributed(0.0, Sigma);
146  Points_[pyindex][i][1] += ran.GetNormalDistributed(0.0, Sigma);
147  cout << Points_[pyindex][i] << endl;
148  }
149  }
150  }
151 }
152 
153 template <class StorageType, class CalculationType>
156 {
157  Random ran;
158  const double fraction = 0.025;
159  const double offs = 5.0;
160  const int pyindex = PyIndex_;
161  int act_num = 0;
162  vector<int> used;
163  if ((int)Points_.size()>pyindex){
164  const int num = (int)Points_[pyindex].size();
165  cout << "adding outlier: "<<endl;
166  for (int i=0; i<num; i++){
167  if (!Points_[pyindex][i].IsAtInfinity()){
168  used.push_back(i);
169  act_num++;
170  }
171  }
172  int num_outl = (int)rint(fraction * (double)act_num);
173  vector<unsigned> outl;
174  for (int i=0; i<num_outl; i++)
175  outl.push_back(ran.GetUniformDistributedInt(0u, (unsigned)(act_num-1)));
176 
177  int index = 0;
178  for (unsigned j=0; j<outl.size(); j++){
179  index = used[outl[j]];
180  assert(!Points_[pyindex][index].IsAtInfinity());
181  cout << setw(4)<<index<<" "<<Points_[pyindex][index] << " --> ";
182  Points_[pyindex][index][0] +=
183  ran.GetUniformDistributed(-offs, offs);
184  Points_[pyindex][index][1] +=
185  ran.GetUniformDistributed(-offs, offs);
186  cout << Points_[pyindex][index] << endl;
187  }
188 
189  }
190 
191 }
192 
193 template <class StorageType, class CalculationType>
195 ReadTracks_(const string& filename)
196 {
197  //AddDebugLevel(D_TS_READ);
198  ifstream is(filename.c_str());
199  if (!is.good()){
200  BIASERR("error opening \""<<filename<<"\"");
201  BIASABORT;
202  }
203 
204  string tmp;
205  int frame, num_tracks=0, num_frames=0;
206  is >> tmp >> num_tracks >> tmp >> num_frames >> tmp;
207  BIASCDOUT(D_TS_READ, "found "<<num_tracks<<" in "<<num_frames<<" frames\n");
208  const int num_c = 2048;
209  char *ttt = new char[num_c];
210  is.getline(ttt, num_c);
211  //cout << "tmp: "<<tmp<<endl;
212  //cout << "line: "<<ttt<<endl;
213  delete[] ttt;
214  Tracks_.resize(num_frames);
215  for (int f=0; f<num_frames; f++){
216  is >> frame;
217  //cout << "frame: "<<frame<<endl;
218  if (frame != f+1){
219  BIASERR("error reading \""<<filename<<"\"");
220  BIASABORT;
221  }
222  BIASCDOUT(D_TS_READ, setw(4)<< frame<<" ");
223  Tracks_[f].resize(num_tracks);
224  for (int t=0; t<num_tracks; t++){
225  is >> Tracks_[f][t][0] >> Tracks_[f][t][1];
226  BIASCDOUT(D_TS_READ, setw(7)<<Tracks_[f][t][0] <<" "
227  << setw(7)<<Tracks_[f][t][1]<< " ");
228  Tracks_[f][t][2] = 1.0;
229  if (!is.good()){
230  BIASERR("error reading \""<<filename<<"\"");
231  BIASABORT;
232  }
233  }
234  }
235  is.close();
236 }
237 
238 
239 template <class StorageType, class CalculationType>
241 FindNearest_(HomgPoint2D& p, int &nearest_frame_index,
242  int &nearest_track_index)
243 {
244  const int num_frames = (int)Tracks_.size();
245  double dist=0;
246  double nearest_dist = numeric_limits<double>::max();
247 
248  int t;
249  nearest_frame_index = -1;
250  nearest_track_index = -1;
251  for (int f=0; f<num_frames; f++){
252  t = FindNearest_(p, f);
253  dist = p.Distance(Tracks_[f][t]);
254  if (dist <= nearest_dist){
255  nearest_dist = dist;
256  nearest_frame_index = f;
257  nearest_track_index = t;
258  }
259  }
260  BIASASSERT(nearest_frame_index>0);
261  BIASASSERT(nearest_dist<0.1);
262 }
263 
264 template <class StorageType, class CalculationType>
266 FindNearest_(HomgPoint2D& p, const int frame)
267 {
268  int nearest_track_index = -1;
269  double dist=0;
270  double nearest_dist = numeric_limits<double>::max();
271  const int num_tracks = (int)Tracks_[frame].size();
272  for (int t=0; t<num_tracks; t++){
273  dist = p.Distance(Tracks_[frame][t]);
274  if (dist <= nearest_dist){
275  nearest_dist = dist;
276  nearest_track_index = t;
277  }
278  }
279  BIASASSERT(nearest_track_index>=0);
280  return nearest_track_index;
281 }
282 
283 
284 //////////////////////////////////////////////////////////////////////////
285 // instantiation
286 //////////////////////////////////////////////////////////////////////////
287 
288 namespace BIAS{
289 template class TrackerSynth<unsigned char, float>;
290 template class TrackerSynth<float, float>;
291 }
virtual int Track(PyramidImageInterface< CalculationType > &pim, PyramidImageInterface< CalculationType > &gradx, PyramidImageInterface< CalculationType > &grady)
Tracks all points from the internal data structure Points_ and afterwards replaces them by the neares...
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
returns ground truth tracks read from file, for debugging
Definition: TrackerSynth.hh:43
HOMGPOINT2D_TYPE Distance(const HomgPoint2D &point) const
Definition: HomgPoint2D.hh:367
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
void AddGaussianNoise_()
adds spatial gaussian noise to Points_[*PyIndex_]
virtual ~TrackerSynth()
virtual int Track(PyramidImageInterface< CalculationType > &pim, PyramidImageInterface< CalculationType > &gradx, PyramidImageInterface< CalculationType > &grady)
Tracks all points from the internal data structure Points_.
Definition: Tracker.cpp:874
void Track_()
for all succesfully tracked points p in Points_[*PyIndex] it calls FindNearest_(p, Frame_) and replaces Points_[*PyIndex] by the so found track.
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
void AddOutlier_()
adds outliers to Points_[*PyIndex_]
void FindNearest_(HomgPoint2D &p, int &nearest_frame_index, int &nearest_track_index)
finds the nearest point to p in all frames in Tracks_.
class for producing random numbers from different distributions
Definition: Random.hh:51
void ReadTracks_(const std::string &filename)
reads tracks from file and stores them in Tracks_