Example Track Point Features

From BoofCV
Revision as of 15:33, 6 November 2012 by Peter (talk | contribs)
Jump to navigationJump to search

Tracking Point Features

Tracked point features in an image sequence.
Tracked point features in an image sequence. Blue dots are older tracks and green dots are newly spawned tracks.

Tracking how point features move inside an image is used to extract the geometric structure and apparent motion of the scene. There are many different ways in which point features are tracked. BoofCV provides a basic tracker that hides much of this complexity and allows a variety of different trackers to be used with out modifying any of the code.

The example code below shows how to use the ImagePointTracker interface to process images and get a list of detected points. Which tracker is used can be changed by toggling comments in the main function.

Example Code:

Concepts:

  • Loading image sequences
  • Tracking point features abstractly
  • Displaying the location of point features

Relevant Applets:

Example Code

public class ExamplePointFeatureTracker< T extends ImageSingleBand, D extends ImageSingleBand>
{
	// type of input image
	Class<T> imageType;
	Class<D> derivType;

	// tracks point features inside the image
	ImagePointTracker<T> tracker;

	// displays the video sequence and tracked features
	ImagePanel gui = new ImagePanel();

	public ExamplePointFeatureTracker(Class<T> imageType) {
		this.imageType = imageType;
		this.derivType = GImageDerivativeOps.getDerivativeType(imageType);
	}

	/**
	 * Processes the sequence of images and displays the tracked features in a window
	 */
	public void process(SimpleImageSequence<T> sequence) {

		// Figure out how large the GUI window should be
		T frame = sequence.next();
		gui.setPreferredSize(new Dimension(frame.getWidth(),frame.getHeight()));
		ShowImages.showWindow(gui,"KTL Tracker");

		// process each frame in the image sequence
		while( sequence.hasNext() ) {
			frame = sequence.next();

			// tell the tracker to process the frame
			tracker.process(frame);

			// if there are too few tracks spawn more
			if( tracker.getActiveTracks(null).size() < 100 )
				tracker.spawnTracks();

			// visualize tracking results
			updateGUI(sequence);

			// wait for a fraction of a second so it doesn't process to fast
			BoofMiscOps.pause(100);
		}
	}

	/**
	 * Draw tracked features in blue, or red if they were just spawned.
	 */
	private void updateGUI(SimpleImageSequence<T> sequence) {
		BufferedImage orig = sequence.getGuiImage();
		Graphics2D g2 = orig.createGraphics();

		// draw active tracks as blue dots
		for( PointTrack p : tracker.getActiveTracks(null) ) {
			VisualizeFeatures.drawPoint(g2, (int)p.x, (int)p.y, Color.blue);
		}

		// draw tracks which have just been spawned green
		for( PointTrack p : tracker.getNewTracks(null) ) {
			VisualizeFeatures.drawPoint(g2, (int)p.x, (int)p.y, Color.green);
		}

		// tell the GUI to update
		gui.setBufferedImage(orig);
		gui.repaint();
	}

	/**
	 * A simple way to create a Kanade-Lucas-Tomasi (KLT) tracker.
	 */
	public void createKLT() {
		PkltManagerConfig<T, D> config = PkltManagerConfig.createDefault(imageType,derivType);
		config.maxFeatures = 200;
		config.featureRadius = 3;
		config.pyramidScaling = new int[]{1,2,4,8};

		tracker = FactoryPointSequentialTracker.klt(config);
	}

	/**
	 * Creates a SURF feature tracker.
	 */
	public void createSURF() {
		tracker = FactoryPointSequentialTracker.dda_FH_SURF(200, 80, 3, 4, imageType);
	}

	public static void main( String args[] ) throws FileNotFoundException {

		Class imageType = ImageFloat32.class;

		// loads an MJPEG video sequence
		VideoMjpegCodec codec = new VideoMjpegCodec();
		List<byte[]> data = codec.read(new FileInputStream("../data/applet/zoom.mjpeg"));
		SimpleImageSequence sequence = new JpegByteImageSequence(imageType,data,true);

		ExamplePointFeatureTracker app = new ExamplePointFeatureTracker(imageType);

		// Comment or un-comment to change the type of tracker being used
		app.createKLT();
//		app.createSURF();

		app.process(sequence);
	}
}