Difference between revisions of "Example Webcam Capture"

From BoofCV
Jump to navigationJump to search
m
m
 
Line 1: Line 1:
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 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 [https://github.com/lessthanoptimal/BoofCV/blob/master/integration/WebcamCapture/examples/boofcv/examples/ examples directory]
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 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 [https://github.com/lessthanoptimal/BoofCV/blob/v0.23/integration/WebcamCapture/examples/boofcv/examples/ examples directory]


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


Concepts:
Concepts:
Line 23: Line 23:
PkltConfig configKlt = new PkltConfig(3,new int[]{1,2,4,8});
PkltConfig configKlt = new PkltConfig(3,new int[]{1,2,4,8});


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


// Open a webcam at a resolution close to 640x480
// Open a webcam at a resolution close to 640x480
Line 37: Line 37:
while( true ) {
while( true ) {
BufferedImage image = webcam.getImage();
BufferedImage image = webcam.getImage();
ImageFloat32 gray = ConvertBufferedImage.convertFrom(image,(ImageFloat32)null);
GrayF32 gray = ConvertBufferedImage.convertFrom(image,(GrayF32)null);


tracker.process(gray);
tracker.process(gray);

Latest revision as of 04:53, 28 March 2016

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<GrayF32> tracker = FactoryPointTracker.klt(configKlt,configDetector,GrayF32.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();
			GrayF32 gray = ConvertBufferedImage.convertFrom(image,(GrayF32)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);
		}
	}
}