In this article I will show you how to detect eyes using OpenCV's boosted cascade of haar-like features. Source code, XML eye classifier, and sample images are available for download.
This project was much inspired by the work of Dr. Matthias Wimmer and his team. They have a project named EyeFinder that looks great. Unfortunately they don't provide the source code, only the eye classifiers are available. So I try to write similar application to detect human eyes using the eye classifier they provided.
To detect human eyes, first we have to detect the face. We can easily do this using OpenCV face classifier. After we have successfully detect the face, we estimate the location of the eyes and perform eye detection. We need to estimate the eyes location in order to reduce false detections.
The main function is pretty simple. It initialize some variables, load the image, call the detectEyes function, and display the image. I will explain the eye detection function below.
Given an image, the function will try to detect human face in it. If it find any, it will continue with the eye detection. Otherwise it will stop.
Listing 1: Detect human face
The face detector will put the list of detected faces into faces. To make things simple, we assume that there is only one face in the image so we'll take the first detected face in faces.
Listing 2: Get the first detected face
Now that we have a detected human face, we estimate the location of the eyes before try to find them. We simply estimate the location as: 1/3 of the face height, a few pixels from the top.
Listing 3: Estimate the eyes' position
The code above sets the Region of Interest (ROI) of the image, so the next object detection will be performed only on that particular location. This will not only increase the speed, but also reduce false detections. Next we detect the eyes.
Listing 4: Detect the eyes
The object detector will put the list of detected eyes into eyes. Now we can simply draw a rectangle for each eye available in eyes.
Listing 5: Draw rectangles
Don't forget to reset the Region of Interest, otherwise OpenCV will only display the detected eyes when you call cvShowImage. And we don't want that!
Listing 6: Final touch
Here are some sample images that was tested using my code. In case you are curious, she's my gf.

As we can see from the images above, the eyes were successfully detected. Not so perfect, but enough I think.
Learning OpenCV: Computer Vision with the OpenCV Library
By: Gary Bradski, Adrian Kaehler
This book is the "de facto" OpenCV User's Manual. It provides a practical, pragmatic, accessible book on computer vision, with algorithmic explanation and concrete example code snippets. Written by the creators of OpenCV, no doubt you should obtain a copy.
Machine Vision: Theory, Algorithms, Practicalities (Signal Processing and its Applications)
By: E. R. Davies
This book provides a solid and concrete foundation to computer vision from engineering point of view. Use Learning OpenCV from Gary Bradsky to learn how to use OpenCV, and use this book to understand how OpenCV works behind the screen.
C Programming Language (2nd Edition)
By: Brian W. Kernighan
For you who new to C programming, this is a must-have book for you. Written by the C creator himself, this book is concise and powerful, just like C itself.
Nash on Feb 18, 2009:
Krishna Chaitanya Kotra on Feb 17, 2009:
Kalani on Mar 4, 2009:
scliem on Mar 5, 2009:
Nash on Mar 5, 2009:
Nash on Mar 5, 2009:
gcc port for Windows. Note that Dev-C++ is a text editor, not a compiler. But they provide a package with MinGW included.gcc -I"C:\OpenCV\cv\include" -I"C:\OpenCV\cxcore\include" -I"C:\OpenCV\otherlibs\highgui" eyedetect.c -o eyedetect -L"C:\OpenCV\lib" -lcxcore -lcv -lhighgui vasanth on Mar 5, 2009:
Nash on Mar 5, 2009:
Kalani on Mar 16, 2009:
Nash on Mar 16, 2009:
Kalani on Mar 18, 2009:
hany on Mar 18, 2009:
Nash on Mar 18, 2009:
Nash on Mar 18, 2009:
vamsee damarla on Mar 19, 2009:
Nash on Mar 19, 2009:
eyedetect myphoto.jpgassert(argc == 2);img = cvLoadImage(argv[1], 1);img = cvLoadImage("myphoto.jpg", 1);myphoto.jpg without reading the command line parameters. You can also specify the image's path like C:/images/myphoto.jpg.hany on Mar 20, 2009:
Nash on Mar 21, 2009:
cascade2xml.c provided in the files section of the yahoo opencv group.Annie Sun on Mar 25, 2009:
Nash on Mar 26, 2009:
vamsee damarla on Mar 26, 2009:
Jap on Apr 2, 2009:
Nash on Apr 3, 2009:
/opt/opencv-1.0/lib,$ LD_LIBRARY_PATH=/opt/opencv-1.0/lib:$LD_LIBRARY_PATH
$ export LD_LIBRARY_PATH$ gcc -Wall -static `pkg-config --cflags opencv` eyedetect.c -o eyedetect `pkg-config --libs opencv`r = (CvRect*)cvGetSeqElem( eyes, i );r is a CvRect type. the x,y,w,h are used to draw red boxes by the lines below it.Jap on Apr 3, 2009:
Nash on Apr 4, 2009:
cvNamedWindow(argv[1], 1);
cvShowImage(argv[1], img);
cvDestroyWindow(argv[1]); jap on Apr 6, 2009:
Nash on Apr 6, 2009:
printf("x:%d;y:%d;w:%d;h:%dn", r->x, r->y, r->width, r->height);Jap on Apr 7, 2009:
exec("/path/to/lib/opencv-1.0.0/eyedetect $path_to_img_file", $coordinates, $status);Shwet on Apr 11, 2009:
Najib on May 19, 2009:
Nash on May 19, 2009:
Nicolas Bourré on May 27, 2009:
ProfessionalAmatuer on Jun 2, 2009:
Pisal on Jun 4, 2009:
jen on Jun 15, 2009:
Nash on Jun 15, 2009:
chocobo on Jun 22, 2009:
Nash on Jun 22, 2009:
felix on Jun 24, 2009:
Nash on Jun 24, 2009:
felix on Jun 24, 2009:
Rishi Anand on Jun 26, 2009:
IplImage *img=cvLoadImage("imagename.jpg", CV_LOAD_IMAGE_COLOR);img = cvLoadImage(argv[1]);Nash on Jun 26, 2009:
C:\vision\images\photo.jpg, then load the image with:IplImage* img = cvLoadImage("C:/vision/images/photo.jpg", CV_LOAD_IMAGE_COLOR);eyedetect image1.jpg image2.jpg image3.jpgmain():htchien on Jun 29, 2009:
CvRect *r = (cvRect*)cvGetSeqElem(faces, 0);CvRect *r = (CvRect*)cvGetSeqElem(faces, 0);cvRect() is a function call in OpenCV to make a instance of CvRect object. That's why VC++ told you it's a illegal use.k09 on Jul 3, 2009:
Nash on Jul 4, 2009:
Nasir on Oct 15, 2009:
CvSeq* eyes = cvHaarDetectObjects(
img,
cascade_e,
storage,
1.15,
3,
0,
cvSize ( 25, 15 )
);Nasir on Oct 19, 2009:
Nash on Oct 19, 2009:
rameez on Oct 20, 2009:
Nash on Oct 20, 2009:
rameez on Oct 21, 2009:
Nash on Oct 26, 2009:
Bobby on Nov 1, 2009:
Nash on Nov 1, 2009:
Nirav on Feb 18, 2010:
Nash on Feb 19, 2010:
Nirav on Feb 22, 2010:
Md. Abdul Momin on Feb 28, 2010:
sumon on Mar 2, 2010:
Nash on Mar 3, 2010:
sumon on Mar 4, 2010:
Md. Abdul Momin on Mar 4, 2010:
sabrina on Mar 4, 2010:
cvRectangle(
img,
cvPoint(eye->x, eye->y),
cvPoint(eye->x + eye->width, eye->y + eye->height),
CV_RGB(255, 0, 0),
1, 8, 0
);neeha on Mar 4, 2010:
Nash on Mar 5, 2010:
zetha on May 8, 2010:
eye_region = cvGetSubRect(image, cvRect(face.x, int(face.y + (face.height/4)), face.width, int(face.height/2)))
eyes = cvHaarDetectObjects(eye_region, eyeCascade, memo, 1.15, 3, 0, cvSize(25,15))
for e in eyes:
# drawing rectangle
steve on May 28, 2010:
steve on May 31, 2010:
vipin on Nov 11, 2010:
Nash on Nov 12, 2010:
lazarus on Nov 27, 2010:
| ICQ | 489571630 |
| Skype | dede_bl4ckheart |
| Yahoo | dede_bl4ckheart |
| nashruddin.amin |
ABHAY SHANKAR on Feb 17, 2009:
now the main problem where iam stuck is to compare another face with those that are already present in the actual picture. i need to calculate that how much( in percentage) that second picture matches the first one containing lots of faces. i need to develop a wrapper (in c or c++) for that. can u help me.