Cascade classifier target

Cascade classifier
goal
in this section of the tutorial, you will learn:

Use CascadeClassifier class object is detected in the video stream Specifically, we will use the function:
Load to load an .xml file classifier It may Haar features may be LBP feature classifier..
DetectMultiScale to multi-image scale testing.
principles of
the code
in this tutorial code shown below. you can also download the point here. the second version (LBP using face detection) can be found here.

#include “opencv2/objdetect/objdetect.hpp”
#include “opencv2/highgui/highgui.hpp”
#include “opencv2/imgproc/imgproc.hpp”

#include
#include <stdio.h>

using namespace std;
using namespace cv;

/ ** function declarations * /
void detectAndDisplay (Mat Frame);

/** 全局变量 */
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);

/ ** @ main function /
int main (int argc, char const
* the argv)
{
the CvCapture * Capture;
Mat Frame;

// - 1. Load cascade classifier file
IF (face_cascade.load (face_cascade_name)!) {The printf ( "- () Error loading \ n-!"); Return -1;};
IF (eyes_cascade.load (! eyes_cascade_name)) {printf ( "- () Error loading \ n!"); return -1;};

// - Open the built-in camera video stream
Capture cvCaptureFromCAM = (-1);
IF (Capture)
{
the while (to true)
{
Frame = cvQueryFrame (Capture);

// - 3. The current frame is detected using a classifier
IF (frame.empty ()!)
{DetectAndDisplay (Frame);}
the else
{the printf ( "- () No Captured Frame - Break!!"); BREAK ;}

   int c = waitKey(10);
   if( (char)c == 'c' ) { break; }
  }

}
return 0;
}

/** @函数 detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector faces;
Mat frame_gray;

cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

// - Multi-size face detection
face_cascade.detectMultiScale (frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size (30, 30));

for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width0.5, faces[i].y + faces[i].height0.5 );
ellipse( frame, center, Size( faces[i].width0.5, faces[i].height0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;

//-- 在每张人脸上检测双眼
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

for( int 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( (eyes[j].width + eyes[i].height)*0.25 );
   circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
 }

}
// - shows the results of the image
imshow (window_name, Frame);
}
Code explanation
Results
FIG using the above code is the result of face detection built-in camera images of the video stream:

... / ... / ... / ... / _images / Cascade_Classifier_Tutorial_Result_Haar.jpg
attention copying classified files haarcascade_frontalface_alt.xml and haarcascade_eye_tree_eyeglasses.xml to lower your current directory. They OpenCV installation folder opencv / data / haarcascades inside.

FIG using the document classifier lbpcascade_frontalface.xml (LBP feature trained) for a detection result for detection of both eyes still using just used classifier.

... / ... / ... / ... / _images / Cascade_Classifier_Tutorial_Result_LBP.jpg
translator ¶

Released two original articles · won praise 0 · Views 30

Guess you like

Origin blog.csdn.net/xyzyjg/article/details/104423785