1
2
3
4
5
6
7
8
9 package org.openimaj.video.tracking.klt;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.TreeMap;
15
16
17
18
19
20
21
22 public class FeatureHistory {
23 Map<Integer, List<List<Feature>>> history = new TreeMap<Integer, List<List<Feature>>>();
24
25
26
27
28 public List<Feature> [] currentState;
29
30
31
32
33
34 @SuppressWarnings("unchecked")
35 public FeatureHistory(int nFeatures) {
36 currentState = new List[nFeatures];
37 }
38
39
40
41
42
43
44 public void record(FeatureList fl, int frame) {
45 for (int i=0; i<fl.features.length; i++) {
46 Feature f = fl.features[i];
47
48 if (f.val>=0) {
49
50 if (currentState[i] == null) {
51 List<Feature> ff = new ArrayList<Feature>();
52 List<List<Feature>> hist;
53 if (history.containsKey(frame)) {
54 hist = history.get(frame);
55 } else {
56 hist = new ArrayList<List<Feature>>();
57 history.put(frame, hist);
58 }
59 hist.add(ff);
60 currentState[i] = ff;
61 }
62 if (currentState[i].size() == 0 || !currentState[i].get(currentState[i].size()-1).equals(f))
63 currentState[i].add(f.clone());
64 } else {
65
66 currentState[i] = null;
67 }
68 }
69 }
70
71 @Override
72 public String toString() {
73 String s = "FeatureHistory[\n";
74 for (int startframe : history.keySet()) {
75 List<List<Feature>> tracks = history.get(startframe);
76 s += "Starting frame: " + startframe + ":\n";
77 for (int i=0; i<tracks.size(); i++) {
78 s += "\t" + i +" " + tracks.get(i) + "\n";
79 }
80 }
81 return s+"]";
82 }
83 }