Hardware Reference
In-Depth Information
Color Recognition Using a Webcam
In this project, you'll get a firsthand
look at how computer vision works. The
Processing sketch shown here uses a
video camera to generate a digital image,
looks for pixels of a specific color, and
then marks them on the copy of the
image that it displays onscreen. The
OpenCV library for Processing enables
you to capture the image from a webcam
attached to your computer and analyze
the pixels.
MATERIALS
» Personal computer with uSb or FireWire port
» uSb or FireWire webcam
» Colored objects
The following Processing sketch is an example of color
tracking using the OpenCV library. To use this, you'll need
to have a camera attached to your computer, and also
have the drivers installed. The camera you used in Chapter
3 for the cat camera should do the job fine. You'll also
need some small colored objects—stickers or toy balls
work well.
Before the setup() , set up a few
global variables, including an instance
of the OpenCV library, an array to
hold the pixel color values, and a color
variable for the color you want to track.
8
/*
ColorTracking with openCV
Context: Processing
Based on an example by Daniel Shiffman
*/
The setup() sets the initial condi-
tions as usual, in this case, initial-
izing OpenCV using the first camera
available to your computer, sizing the
window, and initializing smooth, anti-
alised graphics.
// import the opencv library:
import hypermedia.video.*;
OpenCV opencv; // opencv instance
int[] pixelArray; // array to copy the pixel array to
color trackColor; // the color you're looking for
void setup() {
// initialize the window:
size( 640, 480 );
NOTE: Because the OpenCV application
uses the first camera available, you may
have problems if you have both a built-in
camera on a laptop and an external webcam.
One solution is to open the camera you
don't want to use in another application,
so only the one you want is available to
OpenCV. It's a crude solution, but it works.
// initialize opencv
opencv = new OpenCV( this );
opencv.capture( width, height );
// Start off tracking for red
trackColor = color(255, 0, 0);
// draw smooth edges:
smooth();
}
Search WWH ::




Custom Search