Human Eye Detection involves two steps:
1. Face Detection to obtain the rectangular area of a person's face
2. Human Eye detection in the face rectangle area
The following are some source code:
# Include "stdafx. H "<br/> # include" CV. H "<br/> # include" highgui. H "<br/> # include <assert. h> <br/> void detecteyes (iplimage * IMG) <br/> {<br/>/* allocate storage */<br/> cvmemstorage * storage = 0; <br/> storage = cvcreatememstorage (0); <br/>/* load face cascade */<br/> cvhaarclassifiercascade * cascade_f = 0; <br/> const char * cascade_name = "haarcascade_frontalface_alt2.xml"; <br/> cascade_f = (cvhaarclass Ifiercascade *) cvload (cascade_name, 0, 0, 0 ); </P> <p>/* detect faces */<br/> cvseq * faces = cvhaardetectobjects (<br/> IMG, /* The Source image */<br/> cascade_f,/* The face classifier */<br/> storage,/* memory buffer, created with cvmemstorage */<br/> 1.1, 3, 0,/* Special parameters, tune for your app */<br/> cvsize (40, 40) /* minimum detection scale */<br/>); </P> <p>/* return if not f Ound */<br/> If (faces-> total = 0) return; </P> <p>/* Get the first detected face */<br/> cvrect * Face = (cvrect *) cvgetseqelem (faces, 0 ); </P> <p>/* draw a red rectangle */<br/> cvrectangle (<br/> IMG, <br/> cvpoint (face-> X, face-> Y), <br/> cvpoint (<br/> face-> X + face-> width, <br/> face-> Y + face-> height <br/>), <br/> cv_rgb (255, 0, 0), <br/> 1, 8, 0 <br/>); </P> <p>/* reset buffer for the ne XT object detection */<br/> cvclearmemstorage (storage); <br/> // cvrelease (void **) cascade_f ); </P> <p>/* set the region of interest: estimate the eyes 'position */<br/> cvsetimageroi (<br/> IMG, /* The Source image */<br/> cvrect (<br/> face-> X, /* x = start from leftmost */<br/> face-> Y + (face-> height/5.5 ), /* y = a few pixels from the top */<br/> face-> width,/* width = same width with the FA Ce */<br/> face-> height/3.0/* Height = 1/3 of face height */<br/> ); </P> <p>/* load eye cascade */<br/> cvhaarclassifiercascade * cascade_e = 0; </P> <p> const char * cascade_name2 = "I: // IMGs // haarcascade_eye.xml "; <br/> cascade_e = (cvhaarclassifiercascade *) cvload (cascade_name2, 0, 0, 0); <br/> assert (cascade_e! = NULL); <br/> // storage = cvcreatememstorage (0 ); <br/>/* detect the eyes */<br/> cvseq * Eyes = cvhaardetectobjects (<br/> IMG,/* The Source image, with the <br/> estimated location defined */<br/> cascade_e,/* The Eye classifier */<br/> storage, /* memory buffer */<br/> 1.15, 3, 0,/* tune for your app */<br/> cvsize (25, 15) /* minimum detection scale */<br/>); </P> <p> int I; </P> <p>/* Dra W a rectangle for each detected eye */<br/> for (I = 0; I <(eyes? Eyes-> total: 0); I ++) {<br/>/* Get one eye */<br/> cvrect * eye = (cvrect *) cvgetseqelem (eyes, i); </P> <p>/* draw a red rectangle */<br/> cvrectangle (<br/> IMG, <br/> cvpoint (eye-> X, eye-> Y), <br/> cvpoint (eye-> X + eye-> width, eye-> Y + eye-> height), <br/> cv_rgb (255, 0, 0), <br/> 1, 8, 0 <br/> ); <br/>}</P> <p>/* reset region of interest */<br/> cvresetimageroi (IMG ); <br/>}< br/>/* end of detecteyes () */</P> <p> void detect_eye_and_display_pic (char * argv) <br/>{< br/> iplimage * IMG = cvloadimage (argv ); <br/> // opens a window on the screen that can <br/> // contain and display an image <br/> cvnamedwindow ("example1", cv_window_autosize ); <br/> // detect eyes in the image <br/> detecteyes (IMG ); </P> <p> // whenever we have an image in the form of an iplimage * pointer, we can display it in an <br/> // existing window with cvshowimage (). <br/> // The cvshowimage () function requires that a named <br/> // window already exist (created by cvnamedwindow ()). <br/> cvshowimage ("example1", IMG); <br/> // The cvwaitkey () function asks the program to stop and wait for a keystroke <br/> cvwaitkey (0); <br/> // free the allocated memory <br/> cvreleaseimage (& IMG ); <br/> cvdestroywindow ("example1"); <br/>}