Difference between revisions of "Example Webcam Capture"

From BoofCV
Jump to navigationJump to search
(Created page with "Demonstration of how to stream images from [http://webcam-capture.sarxos.pl/ Webcam Capture] and process them in BoofCV. Webcam Capture is an easy to use library for accessing...")
 
m
Line 2: Line 2:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/master/integration/WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java ExampleTrackingKlt.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.19/integration/WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java ExampleTrackingKlt.java]


Concepts:
Concepts:
Line 28: Line 28:
Webcam webcam = UtilWebcamCapture.openDefault(640,480);
Webcam webcam = UtilWebcamCapture.openDefault(640,480);


// Create the panel used to display the image and
// Create the panel used to display the image and feature tracks
ImagePanel gui = new ImagePanel();
ImagePanel gui = new ImagePanel();
gui.setPreferredSize(webcam.getViewSize());
gui.setPreferredSize(webcam.getViewSize());


ShowImages.showWindow(gui,"KLT Tracker");
ShowImages.showWindow(gui,"KLT Tracker",true);


int minimumTracks = 100;
int minimumTracks = 100;

Revision as of 20:45, 19 September 2015

Demonstration of how to stream images from Webcam Capture and process them in BoofCV. Webcam Capture is an easy to use library for accessing webcams in Java. In the example below the default webcam is streamed and KLT features are tracked. More examples can be found in the webcam capture examples directory

Example Code:

Concepts:

  • Webcams

Example Code

/**
 * Processes a video feed and tracks points using KLT
 *
 * @author Peter Abeles
 */
public class ExampleTrackingKlt {

	public static void main(String[] args) {

		// tune the tracker for the image size and visual appearance
		ConfigGeneralDetector configDetector = new ConfigGeneralDetector(-1,8,1);
		PkltConfig configKlt = new PkltConfig(3,new int[]{1,2,4,8});

		PointTracker<ImageFloat32> tracker = FactoryPointTracker.klt(configKlt,configDetector,ImageFloat32.class,null);

		// Open a webcam at a resolution close to 640x480
		Webcam webcam = UtilWebcamCapture.openDefault(640,480);

		// Create the panel used to display the image and feature tracks
		ImagePanel gui = new ImagePanel();
		gui.setPreferredSize(webcam.getViewSize());

		ShowImages.showWindow(gui,"KLT Tracker",true);

		int minimumTracks = 100;
		while( true ) {
			BufferedImage image = webcam.getImage();
			ImageFloat32 gray = ConvertBufferedImage.convertFrom(image,(ImageFloat32)null);

			tracker.process(gray);

			List<PointTrack> tracks = tracker.getActiveTracks(null);

			// Spawn tracks if there are too few
			if( tracks.size() < minimumTracks ) {
				tracker.spawnTracks();
				tracks = tracker.getActiveTracks(null);
				minimumTracks = tracks.size()/2;
			}

			// Draw the tracks
			Graphics2D g2 = image.createGraphics();

			for( PointTrack t : tracks ) {
				VisualizeFeatures.drawPoint(g2,(int)t.x,(int)t.y,Color.RED);
			}

			gui.setBufferedImageSafe(image);
		}
	}
}