0% found this document useful (0 votes)
296 views4 pages

Cascade Classifier - OpenCV 2.4.6

This document discusses using a cascade classifier in OpenCV to detect objects like faces in a video stream. It explains loading Haar or LBP classifiers, detecting objects using detectMultiScale, and displaying the detections on the video frame. The code provided loads face and eye classifiers, detects faces in each frame and eyes in each face region, and displays the results.

Uploaded by

ThànhVũ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views4 pages

Cascade Classifier - OpenCV 2.4.6

This document discusses using a cascade classifier in OpenCV to detect objects like faces in a video stream. It explains loading Haar or LBP classifiers, detecting objects using detectMultiScale, and displaying the detections on the video frame. The code provided loads face and eye classifiers, detects faces in each frame and eyes in each face region, and displays the results.

Uploaded by

ThànhVũ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

28/09/2013

Cascade Classifier OpenCV 2.4.6.0 documentation

Cascade Classifier
Goal
In this tutorial you will learn how to: Use the CascadeClassifier class to detect objects in a video stream. Particularly, we will use the functions: load to load a .xml classifier file. It can be either a Haar or a LBP classifer detectMultiScale to perform the detection.

Theory Code
This tutorial codes is shown lines below. You can also download it from here . The second version (using LBP for face detection) can be found here
# i n c l u d e" o p e n c v 2 / o b j d e t e c t / o b j d e t e c t . h p p " # i n c l u d e" o p e n c v 2 / h i g h g u i / h i g h g u i . h p p " # i n c l u d e" o p e n c v 2 / i m g p r o c / i m g p r o c . h p p " # i n c l u d e< i o s t r e a m > # i n c l u d e< s t d i o . h > u s i n gn a m e s p a c es t d ; u s i n gn a m e s p a c ec v ; / * *F u n c t i o nH e a d e r s* / v o i dd e t e c t A n d D i s p l a y (M a tf r a m e) ; / * *G l o b a lv a r i a b l e s* / S t r i n gf a c e _ c a s c a d e _ n a m e=" h a a r c a s c a d e _ f r o n t a l f a c e _ a l t . x m l " ; S t r i n ge y e s _ c a s c a d e _ n a m e=" h a a r c a s c a d e _ e y e _ t r e e _ e y e g l a s s e s . x m l " ; C a s c a d e C l a s s i f i e rf a c e _ c a s c a d e ; C a s c a d e C l a s s i f i e re y e s _ c a s c a d e ; s t r i n gw i n d o w _ n a m e=" C a p t u r e-F a c ed e t e c t i o n " ; R N Gr n g ( 1 2 3 4 5 ) ; / * *@ f u n c t i o nm a i n* / i n tm a i n (i n ta r g c ,c o n s tc h a r * *a r g v) { C v C a p t u r e *c a p t u r e ; M a tf r a m e ;

/ / -1 .L o a dt h ec a s c a d e s i f (! f a c e _ c a s c a d e . l o a d (f a c e _ c a s c a d e _ n a m e)) {p r i n t f ( " ( ! ) E r r o rl o a d i n g \ n " ) ;r e t u r n i f (! e y e s _ c a s c a d e . l o a d (e y e s _ c a s c a d e _ n a m e)) {p r i n t f ( " ( ! ) E r r o rl o a d i n g \ n " ) ;r e t u r n


docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier 1/4

28/09/2013

Cascade Classifier OpenCV 2.4.6.0 documentation

/ / -2 .R e a dt h ev i d e os t r e a m c a p t u r e=c v C a p t u r e F r o m C A M (1) ; i f (c a p t u r e) { w h i l e (t r u e) { f r a m e=c v Q u e r y F r a m e (c a p t u r e) ; / / -3 .A p p l yt h ec l a s s i f i e rt ot h ef r a m e i f (! f r a m e . e m p t y ( )) {d e t e c t A n d D i s p l a y (f r a m e) ;} e l s e {p r i n t f ( "( ! )N oc a p t u r e df r a m e-B r e a k ! " ) ;b r e a k ;} i n tc=w a i t K e y ( 1 0 ) ; i f (( c h a r ) c= =' c '){b r e a k ;} } } r e t u r n0 ; } / * *@ f u n c t i o nd e t e c t A n d D i s p l a y* / v o i dd e t e c t A n d D i s p l a y (M a tf r a m e) { s t d : : v e c t o r < R e c t >f a c e s ; M a tf r a m e _ g r a y ; c v t C o l o r (f r a m e ,f r a m e _ g r a y ,C V _ B G R 2 G R A Y) ; e q u a l i z e H i s t (f r a m e _ g r a y ,f r a m e _ g r a y) ; / / -D e t e c tf a c e s f a c e _ c a s c a d e . d e t e c t M u l t i S c a l e (f r a m e _ g r a y ,f a c e s ,1 . 1 ,2 ,0 | C V _ H A A R _ S C A L E _ I M A G E ,S i z e f o r (i n ti=0 ;i<f a c e s . s i z e ( ) ;i + +) { P o i n tc e n t e r (f a c e s [ i ] . x+f a c e s [ i ] . w i d t h * 0 . 5 ,f a c e s [ i ] . y+f a c e s [ i ] . h e i g h t * 0 . 5 e l l i p s e (f r a m e ,c e n t e r ,S i z e (f a c e s [ i ] . w i d t h * 0 . 5 ,f a c e s [ i ] . h e i g h t * 0 . 5 ) ,0 ,0 ,3 6 0 M a tf a c e R O I=f r a m e _ g r a y (f a c e s [ i ]) ; s t d : : v e c t o r < R e c t >e y e s ; / / -I ne a c hf a c e ,d e t e c te y e s e y e s _ c a s c a d e . d e t e c t M u l t i S c a l e (f a c e R O I ,e y e s ,1 . 1 ,2 ,0| C V _ H A A R _ S C A L E _ I M A G E ,S i z e f o r (i n tj=0 ;j<e y e s . s i z e ( ) ;j + +) { P o i n tc e n t e r (f a c e s [ i ] . x+e y e s [ j ] . x+e y e s [ j ] . w i d t h * 0 . 5 ,f a c e s [ i ] . y+e y e s [ j i n tr a d i u s=c v R o u n d (( e y e s [ j ] . w i d t h+e y e s [ j ] . h e i g h t ) * 0 . 2 5) ; c i r c l e (f r a m e ,c e n t e r ,r a d i u s ,S c a l a r (2 5 5 ,0 ,0) ,4 ,8 ,0) ; } } / / -S h o ww h a ty o ug o t i m s h o w (w i n d o w _ n a m e ,f r a m e) ; }

Explanation
docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier 2/4

28/09/2013

Cascade Classifier OpenCV 2.4.6.0 documentation

Result
1. 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 2. 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.

docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier

3/4

28/09/2013

Cascade Classifier OpenCV 2.4.6.0 documentation

Help and Feedback


You did not find what you were looking for? Ask a question on the Q&A forum. If you think something is missing or wrong in the documentation, please file a bug report.

docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier

4/4

You might also like