| |
|
Seeing With OpenCV, Part 3: Follow that Face!
(Continued)
|
|
|
|
This article originally appeared in
SERVO Magazine, March 2007.
Reprinted by permission of T & L Publications, Inc.
|
| |
The Simple Camshift Wrapper
OpenCV includes source code for camshiftdemo, but it's not easy to adapt, since it combines user-input handlers and view toggling with the steps for face tracking.
If you're programming in C++, rather than in C, you could use the CvCamShiftTracker class, defined in cvaux.hpp. Again, however, this class is fairly complex, with many interfaces, and is only available to C++ programmers.
To make the Camshift tracker more accessible, I've written a wrapper for it in C with four main interfaces:
- createTracker() pre-allocates internal data structures
- releaseTracker() releases these resources
- startTracking() initiates tracking from an image plus a rectangular region
- track() tracks the object in this region from frame to frame using Camshift
There are two additional interfaces for setting the parameters vmin and smin:
- setVmin()
- setSmin()
The Camshift wrapper is online at
www.cognotics.com/opencv/downloads/camshift_wrapper/index.html.
Combining Face Detection and Tracking
In camshiftdemo, you needed to manually initialize tracking with the mouse. For a robotics application, it would be much nicer to initialize tracking automatically, using a face detection that the Haar detector returned.
(See last month's article
for details on implementing face detection.)
This section shows how to do that using the Camshift wrapper described above. The program described here detects a face in a live video stream, then tracks it with Camshift. The source for code for the complete program, called "Track Faces," is also available online at
www.cognotics.com/opencv/downloads/camshift_wrapper/index.html.
The Main Program
Figure 3
shows the main program listing for detecting a face in a live video stream, then tracking it using the Camshift wrapper API. (This portion is in TrackFaces.c in the download.) There are three main program segments:
- Detect a face
- Start the tracker
- Track the face
|
|
|
|
|
Figure 3. The main program listing for detecting a face in a live video stream, then tracking it using the
Camshift wrapper API.
|
|
 |
|
1. Detect a face: Lines 15-27 implement a loop to examine video frames until a face is detected.
The call to captureVideoFrame() invokes a helper method to bring in the next video frame and create a copy of it. (Recall from
Part 1
of this series that it's never safe to modify the original video image!) The working copy is stored as pVideoFrameCopy, declared at line 6.
2. Start the tracker: When a face is detected, the code exits this loop (line 26) and starts the tracker (line 30), passing it the face rectangle from the Haar detector.
3. Track the face: Lines 33-48 contain the face-tracking loop. Each call to the wrapper's track() method (line 41) invokes Camshift to find the face location in the current video frame. The Camshift result is returned as an OpenCV datatype called CvBox2D. This datatype represents a rectangle with a rotation angle. The call to cvEllipseBox(), at lines 44-45, draws the ellipse defined by this box.
|