Opencv learning notes () -- Object Detection objdect Based on cascading Classifier

Source: Internet
Author: User
Document directory
  • 1) load the cascade classifier
  • 2) read Video Streams
  • 3) use this classifier for each frame
  • 4) display the target

The target detection method supported by opencv is the classifier training based on the Haar feature of the sample to obtain the cascade boosted classification ). Note: In addition to haar features, the new C ++ interface can also use the HSV features.

First, we will introduce the relevant structure. featureevaluator, a basic class for the feature value calculation of the cascade classifier, provides read operations, copy clone, and getfeaturetype, allocate the setimage and setwindow operations of the image allocation window, calculate the ordered feature calcord, calculate the absolute feature calccat, and create the structure of the classifier feature. Cascade classifier class cascadeclassifier. Group function grouprectangles of the target cascading rectangle.

Next, I try to use cascadeclassifier to detect the target in the video stream (the target supported by Haar is human face, human eye, mouth, nose, and body. Try more mature faces and glasses here ). Use the load function to load the XML classifier file (currently, the classifier includes the Haar classifier and The KNN classifier (with a small amount of data). The specific steps are as follows:

Here I will add one more point: Later I conducted some experiments on the front face classifier. There were 4 in total, including ALT, alt2, alt_tree, and default. The comparison shows that alt and alt2 work better, alt_tree takes a long time, and default is a lightweight, often with incorrect detection. We recommend that you use haarcascade_frontalface_atl.xml and haarcascade_frontalface_atl2.xml.

1) load the cascade classifier

Call the load implementation of the cascadeclassifier class member function. The code is:

CascadeClassifier face_cascade;face_cascade.load("haarcascade_frontalface_alt.xml");

2) read Video Streams

This part is relatively basic ~~ Read image sequences from files, read video files, read video streams from cameras, and read my previous articles.

3) use this classifier for each frame

Here, we first turn the image into a grayscale image, apply histogram equalization to it, and do some preprocessing work. Next, we will detect the human face and call the detectmultiscale function. This function detects objects at different scales of the input image. The parameter image is the input grayscale image, and the objects is the rectangular frame vector group of the object to be detected, scalefactor is the scale parameter in each image scale. The default value is 1.1. The minneighbors parameter is the number of adjacent values that should be retained for each cascading rectangle (this parameter cannot be understood,-_-| ), the default value is 3. Flags is useless for new classifiers (but currently Haar classifiers are old versions. cv_haar_do_canny_pruning uses the Canny edge detector to exclude image regions with few or many edges, cv_haar_scale_image is a proportional normal detection. cv_haar_find_biggest_object only detects the largest object, and cv_haar_do_rough_search only performs initial detection. The default value is 0. minsize and maxsize are used to limit the range of the target region. The Code called here is as follows:

face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

4) display the target

This is also relatively simple. Call the ellips function to display the obtained faces rectangular boxes.

Furthermore, you can obtain the human eye position in each obtained face. The called classifier file is haarcascade_eye_tree_eyeglasses.xml. Select the face area as the region of interest and repeat the appeal steps, I will not go into details here. Of course, if you are interested, you can try other XML files as a classifier. Although the size of the XML file is small, the effect is still acceptable, however, I did not perform too many tests. Simply do not practice fake scripts, and finally paste them with the source code


Code: http://download.csdn.net/detail/yang_xian521/3800468

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.