001/**
002 * This source code file is part of a direct port of Stan Birchfield's implementation
003 * of a Kanade-Lucas-Tomasi feature tracker. The original implementation can be found
004 * here: http://www.ces.clemson.edu/~stb/klt/
005 *
006 * As per the original code, the source code is in the public domain, available
007 * for both commercial and non-commercial use.
008 */
009package org.openimaj.video.tracking.klt.examples;
010
011import java.io.IOException;
012
013import org.openimaj.image.DisplayUtilities;
014import org.openimaj.image.FImage;
015import org.openimaj.image.ImageUtilities;
016import org.openimaj.video.tracking.klt.FeatureList;
017import org.openimaj.video.tracking.klt.KLTTracker;
018import org.openimaj.video.tracking.klt.TrackingContext;
019
020
021/**
022 * KLTTracker Example 1
023 */
024public class Example1 {
025
026        /**
027         * @param args
028         * @throws IOException
029         */
030        public static void main(String [] args) throws IOException {
031                  FImage img1, img2;
032                  
033                  int nFeatures = 100;
034                  TrackingContext tc = new TrackingContext();
035                  FeatureList fl = new FeatureList(nFeatures);
036                  KLTTracker tracker = new KLTTracker(tc, fl);
037                  
038                  System.out.println(tc);
039                  
040                  img1 = ImageUtilities.readF(Example1.class.getResourceAsStream("img0.pgm"));
041                  img2 = ImageUtilities.readF(Example1.class.getResourceAsStream("img1.pgm"));
042
043                  tracker.selectGoodFeatures(img1);
044
045                  System.out.println("\nIn first image:\n");
046                  for (int i = 0 ; i < fl.features.length ; i++)  {
047                          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);
048                  }
049
050                  DisplayUtilities.display(fl.drawFeatures(img1));
051                  fl.writeFeatureList(null, "%3d");
052
053                  tracker.trackFeatures(img1, img2);
054
055                  System.out.println("\nIn second image:\n");
056                  for (int i = 0; i < fl.features.length; i++)  {
057                          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);
058                  }
059
060                  DisplayUtilities.display(fl.drawFeatures(img2));
061                  fl.writeFeatureList(null, "%5.1f");
062        }
063}