View Javadoc

1   /**
2    * This source code file is part of a direct port of Stan Birchfield's implementation
3    * of a Kanade-Lucas-Tomasi feature tracker. The original implementation can be found
4    * here: http://www.ces.clemson.edu/~stb/klt/
5    *
6    * As per the original code, the source code is in the public domain, available
7    * for both commercial and non-commercial use.
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   * The history of a set of tracked features through time
18   * 
19   * @author Stan Birchfield
20   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
21   */
22  public class FeatureHistory {
23  	Map<Integer, List<List<Feature>>> history = new TreeMap<Integer, List<List<Feature>>>();
24  	
25  	/**
26  	 * The feature history. Each element is a list over time.
27  	 */
28  	public List<Feature> [] currentState;
29  	
30  	/**
31  	 * Default constructor with given number of features
32  	 * @param nFeatures
33  	 */
34  	@SuppressWarnings("unchecked")
35  	public FeatureHistory(int nFeatures) {
36  		currentState = new List[nFeatures];
37  	}
38  	
39  	/**
40  	 * Record a list of features at a given time
41  	 * @param fl
42  	 * @param frame
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  				//was tracked
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  				//was lost
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  }