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.examples;
10  
11  import java.io.IOException;
12  
13  import org.openimaj.image.DisplayUtilities;
14  import org.openimaj.image.FImage;
15  import org.openimaj.image.ImageUtilities;
16  import org.openimaj.video.tracking.klt.FeatureList;
17  import org.openimaj.video.tracking.klt.FeatureTable;
18  import org.openimaj.video.tracking.klt.KLTTracker;
19  import org.openimaj.video.tracking.klt.TrackingContext;
20  
21  /**
22   * KLTTracker Example 1
23   */
24  public class Example3 {
25  	/**
26  	 * @param args
27  	 * @throws IOException
28  	 */
29  	public static void main(String [] args) throws IOException {
30  		int nFeatures = 150, nFrames = 10;
31  		boolean replace = false;
32  		int i;
33  
34  		TrackingContext tc = new TrackingContext();
35  		FeatureList fl = new FeatureList(nFeatures);
36  		FeatureTable ft = new FeatureTable(nFeatures);
37  		KLTTracker tracker = new KLTTracker(tc, fl);
38  
39  		tc.setSequentialMode(true);
40  		tc.setWriteInternalImages(false);
41  		tc.setAffineConsistencyCheck(-1);  /* set this to 2 to turn on affine consistency check */
42  
43  		FImage img1 = ImageUtilities.readF(Example1.class.getResourceAsStream("img0.pgm"));
44  
45  		tracker.selectGoodFeatures(img1);
46  		ft.storeFeatureList(fl, 0);
47  
48  		DisplayUtilities.display(fl.drawFeatures(img1));
49  
50  		for (i = 1 ; i < nFrames ; i++)  {
51  			String fnamein = String.format("img%d.pgm", i);
52  			FImage img2 = ImageUtilities.readF(Example1.class.getResourceAsStream(fnamein));
53  			tracker.trackFeatures(img1, img2);
54  
55  			if (replace)
56  				tracker.replaceLostFeatures(img2);
57  
58  			ft.storeFeatureList(fl, i);
59  			DisplayUtilities.display(fl.drawFeatures(img2));
60  		}
61  		ft.writeFeatureTable(null, "%5.1f");
62  	}
63  }