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.FeatureTable;
018import org.openimaj.video.tracking.klt.KLTTracker;
019import org.openimaj.video.tracking.klt.TrackingContext;
020
021/**
022 * KLTTracker Example 1
023 */
024public class Example3 {
025        /**
026         * @param args
027         * @throws IOException
028         */
029        public static void main(String [] args) throws IOException {
030                int nFeatures = 150, nFrames = 10;
031                boolean replace = false;
032                int i;
033
034                TrackingContext tc = new TrackingContext();
035                FeatureList fl = new FeatureList(nFeatures);
036                FeatureTable ft = new FeatureTable(nFeatures);
037                KLTTracker tracker = new KLTTracker(tc, fl);
038
039                tc.setSequentialMode(true);
040                tc.setWriteInternalImages(false);
041                tc.setAffineConsistencyCheck(-1);  /* set this to 2 to turn on affine consistency check */
042
043                FImage img1 = ImageUtilities.readF(Example1.class.getResourceAsStream("img0.pgm"));
044
045                tracker.selectGoodFeatures(img1);
046                ft.storeFeatureList(fl, 0);
047
048                DisplayUtilities.display(fl.drawFeatures(img1));
049
050                for (i = 1 ; i < nFrames ; i++)  {
051                        String fnamein = String.format("img%d.pgm", i);
052                        FImage img2 = ImageUtilities.readF(Example1.class.getResourceAsStream(fnamein));
053                        tracker.trackFeatures(img1, img2);
054
055                        if (replace)
056                                tracker.replaceLostFeatures(img2);
057
058                        ft.storeFeatureList(fl, i);
059                        DisplayUtilities.display(fl.drawFeatures(img2));
060                }
061                ft.writeFeatureTable(null, "%5.1f");
062        }
063}