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.io.DataOutputStream;
12  import java.io.File;
13  import java.io.FileOutputStream;
14  import java.io.IOException;
15  import java.io.PrintWriter;
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.SortedMap;
19  import java.util.TreeMap;
20  
21  /**
22   * A table storing features per frame
23   * 
24   * @author Stan Birchfield
25   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
26   */
27  public class FeatureTable {
28  	/**
29  	 * The table of features
30  	 */
31  	public SortedMap<Integer, List<Feature>> features;
32  	
33  	/**
34  	 * The number of features 
35  	 */
36  	public int nFeatures;
37  
38  	/*********************************************************************
39  	 * KLTCreateFeatureTable
40  	 * @param nFeatures 
41  	 *
42  	 */
43  	public FeatureTable(int nFeatures) {
44  		features = new TreeMap<Integer, List<Feature>>();
45  		this.nFeatures = nFeatures;
46  	}
47  
48  	/**
49  	 * Store a list of features for the given frame number
50  	 * @param fl
51  	 * @param frame
52  	 */
53  	public void storeFeatureList(FeatureList fl, int frame) {
54  		ArrayList<Feature> list = new ArrayList<Feature>(fl.features.length);
55  		
56  		for (Feature f : fl.features)
57  			list.add(f.clone());
58  		
59  		features.put(frame, list);
60  	}
61  
62  	/**
63  	 * Convert to a string representation.
64  	 * @param fmt
65  	 * @param comments
66  	 * @return the string representation.
67  	 */
68  	public String toString(String fmt, boolean comments) {
69  		String [] setup = IOUtils.setupTxtFormat(fmt);
70  		String format = setup[0];
71  		String type = setup[1];
72  
73  		String s = IOUtils.getHeader(format, IOUtils.StructureType.FEATURE_TABLE, features.size(), nFeatures, comments);
74  
75  		for (int j = 0 ; j < nFeatures; j++) {
76  			s += String.format("%7d | ", j);
77  			for (int i = 0 ; i < features.size(); i++)
78  				s += features.get(i).get(j).toString(format, type);
79  			s += "\n";
80  		}
81  
82  		return s;
83  	}
84  
85  	@Override
86  	public String toString() {
87  		return toString("%3d", false);
88  	}
89  
90  	/**
91  	 * Write feature table to a file.
92  	 * @param fname
93  	 * @param fmt
94  	 * @throws IOException
95  	 */
96  	public void writeFeatureTable(File fname, String fmt) throws IOException
97  	{
98  		if (fmt != null) {  /* text file or stderr */ 
99  			if (fname != null) {
100 				PrintWriter bw = new PrintWriter(new FileOutputStream(fname)); 
101 				bw.write(toString(fmt, true));
102 				bw.close();
103 			} else {
104 				System.out.print(toString(fmt, false));
105 			}
106 		} else {  /* binary file */
107 			DataOutputStream dos = new DataOutputStream(new FileOutputStream(fname));
108 
109 			dos.write(IOUtils.binheader_ft.getBytes("US-ASCII"));
110 			dos.writeInt(features.size());
111 			dos.writeInt(nFeatures);
112 
113 			for (int j = 0 ; j < nFeatures ; j++)  {
114 				for (int i = 0 ; i < features.size() ; i++)  {
115 					features.get(j).get(i).writeFeatureBin(dos);
116 				}
117 			}
118 
119 			dos.close();
120 		}
121 	}
122 }
123