Introduction
The open CV (Open Source Computer Vision) is a popular computer vision library started by Intel in 1999. The cross-platform library sets its focus on real-time image processing and includes patent-free implementations of the latest computer vision algorithms. Open CV is released under a BSD license. so it is used in academic projects and commercial products alike. Open CV uses a type of face detector called a Haar Cascade classifier Haar feature-based cascade is an effective object detection method incorporated in our application.
Haar feature-based cascade
This approach to detecting objects in images combines four key concepts:
• Simple rectangular features called Haar features
• An Integral Image for rapid feature detection
• The Ada Boost machine-learning method
• A cascaded classifier to combine many features efficiently
Haar classifiers are single wavelength square waves (one high interval and one low interval). In two dimensions, a square wave is a pair of adjacent rectangles - one light and one dark. The actual rectangle combinations used for visual object detection are not true Haar classifiers. Instead, they contain rectangle combinations better suited to visual recognition tasks. Because of that difference, these features are called Haar features or Haar-like features, rather than Haar wavelets.
The presence of a Haar feature is determined by subtracting the average dark-region pixel value from the average light-region pixel value. If the difference is above a threshold (set during learning), that feature is said to be present.
To determine the presence or absence of hundreds of Haar features at every image location and at several scales efficiently, Viola and Jones used a technique called an Integral Image. In this case, the small units are pixel values. The integral value for each pixel is the sum of all the pixels above it and to its left. Starting at the top left and traversing to the right and down, the entire image can be integrated with a few integer operations per pixel.
Ada Boost algorithm combines many "weak" classifiers to create one "strong" classifier. Ada Boost selects a set of weak classifiers to combine and assigns a weight to each. This weighted combination is a strong classifier.
The classifier uses data stored in an XML file to decide how to classify each image location. The Open CV download includes flavours of XML data for frontal face detection and one for profile faces.
Implementation
1. Initializing the detector
Load the XML data into Cascade, you can use the cvLoad() function. cvLoad() is a general-purpose function for loading data from files. It takes up to three input parameters. For this example, you'll only need the first parameter. This is the path to an XML file containing a valid Haar Cascade.
public static extern IntPtr cvLoad([In][MarshalAs(UnmanagedType.LPStr)]string filename, IntPtr storage, IntPtr name, IntPtr real_name);
Before detecting faces in images, you'll also need to instantiate a CvMemStorage object. This is a memory buffer that expands automatically, as needed. The face detector will put the list of detected faces into this buffer. Since the buffer is expandable, you won't need to worry about overflowing it.
public static extern IntPtr cvCreateMemStorage(int block_size = 0);
2. Running the detector
Instantiate cvHaarDetectObjects () to run the face detector. This function takes up to seven parameters. The first three are the image pointer, XML data, and memory buffer. The remaining four parameters are set to their C++ defaults. These last four parameters are described below, in the section, "Parameters and Tuning."
public static extern IntPtr cvHaarDetectObjects(IntPtr image, IntPtr cascade, IntPtr mem_storage, double scale_factor, int min_neighbours, int flags, CvSize min_size, CvSize max_size);
3. Viewing the results
To pass an image for display, call cvShowImage() with the name you previously assigned the window, and the image you want it to display. The cvWaitKey() call at Line 48 pauses the application until you close the window. If the window fails to close by clicking its close icon, click inside the window's display area, then press a keyboard key. Also, make sure your program calls cvDestroyWindow() to close the window.
Face detections are stored as a list of CvRect struct pointers, access each detection rectangle and add its outline to the pInpImg variable, which holds the in-memory image loaded from a file.
4. Releasing resources
Release the resources used by the input image, the XML data, and the storage buffer. If you'll be detecting faces in multiple images, you don't need to release the XML data or the buffer until after you're done detecting faces.
public static extern void cvReleaseMemStorage(ref IntPtr storage); public static extern void cvReleaseHaarClassifierCascade(ref IntPtr cascade);
Parameter Tuning
Detect object(s) in the current image depending on the current cascade description.
Detect Objects (Intel Image source Image, double scale_factor = 1.1, int min_neighbours = 3, Detection Flags flags = DetectionFlags.FindBiggestObject, CvSize min_size = default (CvSize), CvSize max_size = default(CvSize))
This method finds rectangular regions in the current image that are likely to contain objects the cascade has been trained to recognize. It returns found regions as a sequence of rectangles.
The default parameters (scale=1.1, min_neighbours=3, flags=0) are tuned for accurate (but slow) object detection. For a faster operation on real-time images, the more preferable settings are: scale=1.2, min_neighbours=2, flags=HAAR_DO_CANNY_PRUNING, min_size= (for example, ~1/4 to 1/16 of the image area in case of video conferencing).
scale: Float: The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing the window by 10%. min_neighbours
Int: The minimum number (minus 1) of neighbour rectangles that make up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbours is 0, the function does not have any grouping at all and returns all the detected.
flags Int: Mode of operation. It can be a combination of zero or more of the above flags.
min_width Int: Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection).
min_height Int: Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection).
flags Int: Mode of operation. It can be a combination of zero or more of the above flags.
min_width Int: Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection).
min_height Int: Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection).
Mode of operation flags:
HAAR_SCALE_IMAGE
-For each scale factor used the function will downscale the image rather than "zoom" the feature coordinates in the classifier cascade. Currently, the option can only be used alone, i.e. the flag cannot be set together with the others.
HAAR_DO_CANNY_PRUNING
-If it is set, the function uses a canny edge detector to reject some image regions that contain too few or too many edges and thus cannot contain the searched object. The particular threshold values are tuned for face detection and in this case, the pruning speeds up the processing.
HAAR_FIND_BIGGEST_OBJECT
-If it is set, the function finds the largest object (if any) in the image. That is, the output sequence will contain one (or zero) element(s).
HAAR_DO_ROUGH_SEARCH
-It should be used only when CV_HAAR_FIND_BIGGEST_OBJECT is set and min_neighbours > 0. If the flag is set, the function does not look for candidates of a smaller size as soon as it has found the object (with enough neighbour candidates) at the current scale. Typically, when min_neighbours is fixed, the mode yields a less accurate (a bit larger) object rectangle than the regular single-object mode (flags=HAAR_FIND_BIGGEST_OBJECT), but it is much faster, up to an order of magnitude. A greater value of min_neighbours may be specified to improve accuracy.
Source Code: git clone BucketLst@bitbucket.org/BucketLst/opencv-fa..