You are on page 1of 22

Min Sun

COS429
Computer Vision
OpenCV
+
Face Detection
2
Today's outline
The OpenCV Library
Brief introduction
Getting started
Creating a face detector
How it's done
OpenCV implementation
sing a face detector
!"ample code# step$by$step
%
The OpenCV Library brie!
introduction
Open source computer &ision library written in C'C(
(# created and maintained by )ntel
Cross$platform and e"tremely portable
Targeted for real$time applications
*
The OpenCV Library "ettin"
started
+ownload OpenCV,
http,''sourceforge-net'pro.ects'openc&library'
/etting up
There's a comprehensi&e guide on setting up OpenCV
in &arious en&ironments at the official wi0i,
http,''openc&library-sourceforge-net'
1
Library #rchitecture
2
Modules Descriptions$
%i"h&'(
3unctionality O&er&iew
4/mart5 windows
)mage )'O# rendering
6rocessing 0eyboard and other e&ents#
timeouts
Trac0bars
7ouse callbac0s
Video )'O
8
)indo*s
9
(+O
:
)aitin" !or ,eys-
;<
The OpenCV Library "ettin"
started
Hello =orld>
//This code is taken from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-
//intro.html#!"T#$%&&&25&&&&&&&&&&&&&&&
////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// This is a simple' introductor( $pen") program. The program reads an
// image from a file' inverts it' and displa(s the result.
//
////////////////////////////////////////////////////////////////////////
#include *stdli+.h,
#include *stdio.h,
#include *math.h,
#include *cv.h,
#include *highgui.h,
int main-int argc' char .argv/01
2
#pl#mage. img 3 &4
int height'width'step'channels4
uchar .data4
int i'5'k4
if-argc*212
printf-67sage: main *image-file-name,8n89614
e:it-&14
;
// load an image
img3cv<oad#mage-argv/1014
if-=img12
printf-6"ould not load image file: >s8n6'argv/1014
e:it-&14
;
;;
The OpenCV Library "ettin"
started
Hello =orld> ?cont'd@
// get the image data
height 3 img-,height4
width 3 img-,width4
step 3 img-,widthtep4
channels 3 img-,n"hannels4
data 3 -uchar .1img-,image?ata4
printf-6@rocessing a >d:>d image with >d channels8n6'height'width'channels14
// create a window
cv%amedAindow-6mainAin6' ")BA#%?$ABC7T$#D!14
cvEoveAindow-6mainAin6' 1&&' 1&&14
// invert the image
for-i3&4i*height4iFF1 for-53&45*width45FF1 for-k3&4k*channels4kFF1
data/i.stepF5.channelsFk03255-data/i.stepF5.channelsFk04
// show the image
cvhow#mage-6mainAin6' img 14
// wait for a ke(
cvAaitGe(-&14
// release the image
cvHelease#mage-Iimg 14
return &4
;
;2
Creatin" a !ace detector
Original Viola$Aones paper ?CV6B# 2<<;@
http,''research-microsoft-com'C&iola'6ubs'+etect'&iolaAonesDCV6B2<<;-pdf
Collection of training images# positi&es and negati&es
Bun EdaBoost to distill a set of Haar$li0e features
which gi&e good classifiers
Combine the yielded classifiers appropriately into a
cascade
;%
Creatin" a !ace detector cont'd
Good news F OpenCV comes with an implementation of Viola$Aones>
E good reference $
http,''note-sonots-com'/ci/oftware'haartraining-html
Three tools to use F 4createsamples5# 4haartraining5 and
4performance5
createsamples
Tool from OpenCV to automate creation of training samples
3our functionalities
;- create training samples from one image applying distortions
2- create training samples from from a collection of images#
without distortions
%- create testing samples with ground truth from one image
applying distortions
*- show images from the -&ec internal format which contains a
collection of samples
Best to use a combination of the functionalities to create many
samples from many images with distortions and merge them
;*
Creatin" a !ace detector cont'd
haartraining
The software that performs the &iola$.ones algorithm and creates the cascade file
/ample run,
opencv-haartraining -data class -vec samples.vec -+g negs.t:t
-nstages 2& -nsplits 2 -minhitrate &.JJJ -ma:falsealarm &.5 -npos
KL2& -nneg LM&& -w 2L -h 2L -mem K&92 -mode NC#"
4data5 is the directory in which to store the output
4&ec5 is the -&ec file containing the positi&e images
4bg5 is a te"t file with a collection of paths to bac0ground ?negati&e@ images
4nstages5 is the number of stages of boosting
4nsplits5 is the number of split nodes in the decision trees which are the wea0
classifier for boosting
4minhitrate5 and 4ma"falsealarm5 are cutoff &alues for hit rate and false
alarm# per stage
4npos5 and 4nneg5 are the number of positi&e and negati&e images to be used
from the gi&en sets
4w5 and 4h5 are the width and the height of the sample
4mem5 is the amount of memory that should be used for precalculation- The
default is 2<<7B
4mode5 is either 4BE/)C5 or 4ELL5# stating whether ?ELL@ the full set of Haar
features should be used ?both upright and *1 degree rotated@ or ?BE/)C@ only
upright features
;1
Creatin" a !ace detector cont'd
performance
E tool to test the performance of the obtained face
detector# gi&en the testing set of annotated positi&es
and negati&es ?created with createsamples@
)nput is the haartraining output dir# and the description
file for testing samples generated by createsamples
Output is the numbers of correctly detected ob.ects#
not detected ob.ects# and false positi&es ?detected
ob.ects which do not e"ist@
;2
Creatin" a !ace detector cont'd
Good news F OpenCV also comes with se&eral
cascade files for detecting both frontal and profile
faces
Bad news F These wor0 with 4real5 photographs#
won't wor0 well for the cartoony frames in your final
pro.ect
Good news F you .ust learned how to train your own
cascade classifier
Bad news F it will ta0e days on modern computers
?e&en with multiple processors@
Good news F you get a face detector for wii a&atars
from us>
;8
Detector #l"orith.
;9
/on .a0i.a suppression
;:
'sin" a !ace detector 1code2
#include 6cv.h6
#include 6highgui.h6
#include *stdli+.h,
#ifdef B!i"
#define A#%K2
#endif
const char. cascadeBname 3 6wiiBfrontalfaceL.:ml64
int main - int argc' const char. argv/0 1
2
/. <oad the classifier data from the .:ml file ./
"vOaar"lassifier"ascade. cascade 3 -"vOaar"lassifier"ascade.1cv<oad- cascadeBname14

/. create a window with handle result ./
cv%amedAindow- 6result6 14
/. read the input image ./
#pl#mage. image 3 cv<oad#mage- argv/10' 1 14

/.
create a +Iw image of the same siPe o+5ect as a placeholder for now
- cviPe-width' height1 is the o+5ect representing dimensions of the image
- M stands for M-+it depth
- 1 means one-channel -+Iw1
Oence a 2L+it HQN image of siPe M&&:R&& would +e created with
cv"reate#mage- cviPe-M&&' R&&1' M' K14
./
#pl#mage. gra( 3 cv"reate#mage- cviPe-image-,width'image-,height1' M' 1 14

/. now convert the input image into +Iw and store it in the placeholder ./
cv"vt"olor- image' gra(' ")BNQH2QHCS 14
/. create memor( storage to +e used +( cvOaar?etect$+5ects ./
"vEemtorage. storage 3 cv"reateEemtorage-14

2<
'sin" a !ace detector 1code2 cont'd

/. used cvOaar?etect$+5ects ./

/. M-+it depth HQN representation of color red ./
static "vcalar H!? 3 2&' &' 255;4

/. go through all the detected faces' and draw them into the input image ./
for -int i 3 &4 i * -faces T faces-,total : &14 iFF1
2
"vHect .r 3 -"vHect.1cvQeteU!lem- faces' i 14
"v@oint ul4 "v@oint lr4
ul.: 3 r-,:4 ul.( 3 r-,(4
lr.: 3 r-,: F r-,width4 lr.( 3 r-,( F r-,height4

/. draws a rectangle with given coordinates of the upper left
and lower right corners into an image ./
cvHectangle-image' ul' lr' H!?' K' M' &14
;
2;
'sin" a !ace detector 1code2 cont'd
/. free up the memor( ./
cvHelease#mage- Igra( 14
/. show the result and wait for a ke(stroke form user +efore finishing ./
cvhow#mage- 6result6' image 14
cvAaitGe(-&14
cvHelease#mage- Iimage 14
cv?estro(Aindow-6result614

return &4
;
22
'sin" a !ace detector 1code2 cont'd
result,

You might also like