Saturday, October 22, 2011

Face Recognition using OpenCV2 and HaarCascade an Intro Part 1

In this series of tutorial we will learn how to detect human face. OpenCV library provides very effictive method of object detection including face detection using  Haar Cascades.
The function -
CvSeq* cvHaarDetectObjects(const CvArr* image,CvHaarClassifierCascade* cascade,CvMemStorage* storage,double scale factor=1.1,int min neighbors=3,int flags=0,CvSize min size=cvSize(0,0) );

can detect objects in input image by using HaarCascade structure. But before we jump directly to program we first need to understand about it a bit.

Many approaches have been proposed for detecting and recognizing faces. One of them is a color based approach to face detection. Indeed, color is a low-level cue that can be implemented in a computationally fast and effective way for locating objects. Among the advantages of using color is the computational efficiency and robustness against some geometric changes, when the scene is observed under a uniform illumination field.

However, the main limitation with the use of color lies in its sensitivity to illumination changes. To overcome the limitations of the color based approach, well-known face detection algorithm was proposed by Paul Viola and Michel J.Jhones in 2001 (Paul Viola and Michel J.Jhones “Rapid Object Detection Using Haar-like Features with Cascade of Boosted Classifiers”,IEEE CVPR 2001.)

 It uses Haar-like features and AdaBoost learning algorithm. The Haar-like features are extracted using the notion of integral image which allows very fast feature extraction at different scales, while AdaBoost is used to select the most prominent features among a large number of extracted features and construct a strong classifier from boosting a set of weak classifiers. The use of a cascade of classifiers made this approach one of the first real-time frontal-view face detection methods.
First, a classifier (namely a cascade of boosted classifiers working with haar-like features) is trained with a few hundreds of sample views of a particular object (i.e., a face or a car), called positive examples, that are scaled to the same size (say, 20x20), and negative examples - arbitrary images of the same size.

After a classifier is trained, it can be applied to a region of interest (of the same size as used during the training) in an input image. The classifier gives outputs as "1" if the region is likely to show the object (i.e., face/car), and "0" otherwise. To search for the object in the whole image one can move the search window across the image and check every location using the classifier. The classifier is designed so that it can be easily "resized" in order to be able to find the objects of interest at different sizes, which is more efficient than resizing the image itself. So, to find an object of an unknown size in the image the scan procedure should be done several times at different scales.

The word "cascade" in the classifier name means that the resultant classifier consists of several simpler classifiers (stages) that are applied subsequently to a region of interest until at some stage the candidate is rejected or all the stages are passed. The word "boosted" means that the classifiers at every stage of the cascade are complex themselves and they are built out of basic classifiers using one of four different boosting techniques (weighted voting). Currently Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported. The basic classifiers are decision-tree classifiers with at least 2 leaves. Haar-like features are the input to the basic classifers, and are calculated as described below. The current algorithm uses the following Haar-like features:

In part 2 we will write a C program to detect faces in an image.