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.KLTTracker;
18  import org.openimaj.video.tracking.klt.TrackingContext;
19  
20  
21  /**
22   * KLTTracker Example 1
23   */
24  public class Example1 {
25  
26  	/**
27  	 * @param args
28  	 * @throws IOException
29  	 */
30  	public static void main(String [] args) throws IOException {
31  		  FImage img1, img2;
32  		  
33  		  int nFeatures = 100;
34  		  TrackingContext tc = new TrackingContext();
35  		  FeatureList fl = new FeatureList(nFeatures);
36  		  KLTTracker tracker = new KLTTracker(tc, fl);
37  		  
38  		  System.out.println(tc);
39  		  
40  		  img1 = ImageUtilities.readF(Example1.class.getResourceAsStream("img0.pgm"));
41  		  img2 = ImageUtilities.readF(Example1.class.getResourceAsStream("img1.pgm"));
42  
43  		  tracker.selectGoodFeatures(img1);
44  
45  		  System.out.println("\nIn first image:\n");
46  		  for (int i = 0 ; i < fl.features.length ; i++)  {
47  			  System.out.format("Feature #%d:  (%f,%f) with value of %d\n", i, fl.features[i].x, fl.features[i].y, fl.features[i].val);
48  		  }
49  
50  		  DisplayUtilities.display(fl.drawFeatures(img1));
51  		  fl.writeFeatureList(null, "%3d");
52  
53  		  tracker.trackFeatures(img1, img2);
54  
55  		  System.out.println("\nIn second image:\n");
56  		  for (int i = 0; i < fl.features.length; i++)  {
57  			  System.out.format("Feature #%d:  (%f,%f) with value of %d\n", i, fl.features[i].x, fl.features[i].y, fl.features[i].val);
58  		  }
59  
60  		  DisplayUtilities.display(fl.drawFeatures(img2));
61  		  fl.writeFeatureList(null, "%5.1f");
62  	}
63  }