 |
OpenCV
3.2.0
Open Source Computer Vision
|
.2.0+dfsg_doc_tutorials_objdetect_cascade_classifier_cascade_classifier
Goal
In this tutorial you will learn how to:
- Use the cv::CascadeClassifier class to detect objects in a video stream. Particularly, we will use the functions:
Theory
Code
This tutorial code's is shown lines below. You can also download it from here . The second version (using LBP for face detection) can be found here
#include <iostream>
#include <stdio.h>
using namespace std;
void detectAndDisplay(
Mat frame );
String face_cascade_name =
"haarcascade_frontalface_alt.xml";
String eyes_cascade_name =
"haarcascade_eye_tree_eyeglasses.xml";
String window_name =
"Capture - Face detection";
int main( void )
{
if( !face_cascade.
load( face_cascade_name ) ){ printf(
"--(!)Error loading face cascade\n");
return -1; };
if( !eyes_cascade.
load( eyes_cascade_name ) ){ printf(
"--(!)Error loading eyes cascade\n");
return -1; };
if ( ! capture.
isOpened() ) { printf(
"--(!)Error opening video capture\n");
return -1; }
while ( capture.
read(frame) )
{
{
printf(" --(!) No captured frame -- Break!");
break;
}
detectAndDisplay( frame );
if( c == 27 ) { break; }
}
return 0;
}
void detectAndDisplay(
Mat frame )
{
std::vector<Rect> faces;
for (
size_t i = 0;
i < faces.size();
i++ )
{
Point center( faces[
i].x + faces[
i].width/2, faces[
i].y + faces[
i].height/2 );
ellipse( frame, center,
Size( faces[
i].width/2, faces[
i].height/2 ), 0, 0, 360,
Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[
i] );
std::vector<Rect> eyes;
for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[
i].x + eyes[j].x + eyes[j].width/2, faces[
i].y + eyes[j].y + eyes[j].height/2 );
int radius =
cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius,
Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
}
Explanation
Result
Here is the result of running the code above and using as input the video stream of a build-in webcam:
Remember to copy the files haarcascade_frontalface_alt.xml and haarcascade_eye_tree_eyeglasses.xml in your current directory. They are located in opencv/data/haarcascades
This is the result of using the file lbpcascade_frontalface.xml (LBP trained) for the face detection. For the eyes we keep using the file used in the tutorial.
bool load(const String &filename)
Loads a classifier from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:597
int waitKey(int delay=0)
Waits for a pressed key.
virtual bool read(OutputArray image)
Grabs, decodes and returns the next video frame.
Size2i Size
Definition: types.hpp:315
@ CASCADE_SCALE_IMAGE
Definition: objdetect.hpp:169
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:538
Cascade classifier class for object detection.
Definition: objdetect.hpp:221
bool empty() const
Returns true if the array has no elements.
void equalizeHist(InputArray src, OutputArray dst)
Equalizes the histogram of a grayscale image.
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:93
virtual bool open(const String &filename)
Open video file or a capturing device or a IP video stream for video capturing.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Scalar_< double > Scalar
Definition: types.hpp:606
n-dimensional dense array class
Definition: mat.hpp:741
for i
Definition: modelConvert.m:63
Definition: affine.hpp:52
Definition: cvstd.hpp:478
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple or thick elliptic arc or fills an ellipse sector.
@ circle
Definition: gr_skig.hpp:62
void detectMultiScale(InputArray image, std::vector< Rect > &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
Detects objects of different sizes in the input image. The detected objects are returned as a list of...