OpenCV Tutorials--cascade Classifier

Source: Internet
Author: User

Haar feature-based Cascade Classifier for Object Detection

First, a classifier (namely a cascade of boosted classifiers working with haar-like features) are trained with a f EW hundred sample views of a particular object (i.e., a face or a car), called positive examples, that is scaled to the S  Ame size (say, 20x20), and negative examples-arbitrary images of the same size. Training with positive and negative samples of the same specifications

After a classifier was 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 outputs a "1" if the region was 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, it can be easily ' resized ' in order to being able to find the objects of interest at Different sizes, which is more efficient than resizing the image itself. So, to find a object of an unknown size in the image the scan procedure should is done several times at different scales. Sliding windows at different scales ~ ~ Scale the model instead of scaling the image

the word "cascade" in the classifier name means that the resultant classifier consists of several simpler classifiers (stages) that was applied subsequently to a region of interest until at some stage, the candidate is rejected or a ll the stages is passed. Cascading classifiers are compositions of multiple simple classifiers

the word "boosted" means the classifiers at every stage of the cascade is complex themselves and they are built Out of the basic classifiers using one of four different boosting techniques (weighted voting).  Currently discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported. Simple classifier weighted voting for each stage

The basic classifiers is Decision-tree classifiers with at least 2 leaves. Haar-like features is the input to the basic classifiers, and is calculated as described below. Two-level decision tree, Haar feature as the initial feature input, calculates the following middle features

The feature used in a particular classifier are specified by their shape (1a, 2b etc), position within the region of INT Erest and the scale of the same as the scale used on the detection stage, though these, scales is mult iplied).

For example, in the case of the third line feature (2c) The response is calculated as the difference between the sum of IM Age pixels under the rectangle covering the whole feature (including the Both white stripes and the black stripe in the mid DLE) and the sum of the image pixels under the black stripe multiplied by 3 in order to compensate for the differences in The size of areas. The sums of pixel values over a rectangular regions is calculated rapidly using integral images (see below and the Tegral () description).

Cascadeclassifier::load

Loads a classifier from a file.

Cascadeclassifier::read

Reads a classifier from a filestorage node.

Cascadeclassifier::d Etectmultiscale

Detects objects of different sizes in the input image. The detected objects is returned as a list of rectangles.

void cascadeclassifier::detectmultiscale(const mat& image, Vector<rect >& Objects, double scalefactor=1.1, intminneighbors=3, int flags=0, Size minSize=size (), Size maxSize=size ())

Cascadeclassifier::setimage

The function is automatically called by & objects, double scalefactor, int minneighbors, int flags, Size minSize, Size maxSize) ">cascadeclassifier::d Etectmultiscale () at every image scale. But if you want to test various locations manually using & Feval, point pt, double& weight) ">cascadeclassif Ier::runat (),

Cascadeclassifier::runat

The function returns 1 if the Cascade classifier detects an object in the given location. Otherwise, it returns negated index of the stage at which the candidate had been rejected.

Code

#include "stdafx.h" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include " Opencv2/imgproc/imgproc.hpp "#include <iostream> #include <stdio.h>using namespace std;using namespace CV ;/** Function Headers */void detectanddisplay (Mat frame);/** Global variables */string face_cascade_name = "Haarcascade_ Frontalface_alt.xml "; String eyes_cascade_name = "Haarcascade_eye_tree_eyeglasses.xml"; Cascadeclassifier Face_cascade; Cascadeclassifier eyes_cascade;string window_name = "Capture-face detection"; RNG rng (12345);/** @function main */int main (int argc, const char** argv) {cvcapture* capture; Mat frame;//--1. Load the Cascadesif (!face_cascade.load (face_cascade_name)) {printf ("--(!) Error loading\n "); return-1; };if (!eyes_cascade.load (eyes_cascade_name)) {printf ("--(!) Error loading\n "); return-1; };//--2. Read the video streamcapture = Cvcapturefromcam (-1); if (capture) {while (true) {frame = Cvqueryframe (capture);//--3. Apply the ClasSifier to the Frameif (!frame.empty ()) {Detectanddisplay (frame);} else{printf ("--(!) No captured Frame--break! "); Break }int C = Waitkey (n); if ((char) c = = ' C ') {Break;}}} return 0;} /** @function detectanddisplay */void detectanddisplay (Mat frame) {std::vector<rect> faces; Mat Frame_gray;cvtcolor (frame, frame_gray, Cv_bgr2gray); Equalizehist (Frame_gray, Frame_gray);//histogram equalization, enhanced contrast//-- Detect Facesface_cascade.detectmultiscale (Frame_gray, Faces, 1.1, 2, 0| Cv_haar_scale_image, Size (+)); for (size_t i = 0; i < faces.size (); i++) {Point Center (faces[i].x + Faces[i].widt h*0.5, Faces[i].y + faces[i].height*0.5); ellipse (frame, center, Size (faces[i].width*0.5, faces[i].height*0.5), 0, 0, 36 0, Scalar (255, 0, 255), 4, 8, 0); Mat Faceroi = Frame_gray (Faces[i]);std::vector<rect> eyes;//--in each face, detect Eyeseyes_cascade.detectmultis Cale (Faceroi, Eyes, 1.1, 2, 0 | Cv_haar_scale_image, Size ((+)); for (size_t j = 0; J < Eyes.size (), j + +) {point CEnter (faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5), int radius = Cvround (ey Es[j].width + eyes[j].height) *0.25); Circle (frame, center, radius, Scalar (255, 0, 0), 4, 8, 0);}} --Show you Gotimshow (window_name, frame);}

OpenCV Tutorials--cascade Classifier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.