Learning OpenCV: 一个简单的人眼检测程序

人眼检测 分两步骤:

 

1. 人脸检测,得到一个人脸矩形区域

2. 在人脸矩形区域进行人眼检测

 

以下是部分源代码:

 

#include "stdafx.h" #include "cv.h" #include "highgui.h" #include <assert.h> void detectEyes(IplImage *img) { /*allocate storage*/ CvMemStorage* storage = 0; storage = cvCreateMemStorage(0) ; /*load face cascade*/ CvHaarClassifierCascade* cascade_f = 0; const char* cascade_name = "haarcascade_frontalface_alt2.xml"; cascade_f = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); /* detect faces */ CvSeq *faces = cvHaarDetectObjects( img, /* the source image */ cascade_f, /* the face classifier */ storage, /* memory buffer, created with cvMemStorage */ 1.1, 3, 0, /* special parameters, tune for your app */ cvSize(40, 40) /* minimum detection scale */ ); /* return if not found */ if (faces->total == 0) return; /* get the first detected face */ CvRect *face = (CvRect*)cvGetSeqElem(faces, 0); /* draw a red rectangle */ cvRectangle( img, cvPoint(face->x, face->y), cvPoint( face->x + face->width, face->y + face->height ), CV_RGB(255, 0, 0), 1, 8, 0 ); /* reset buffer for the next object detection */ cvClearMemStorage(storage); //cvRelease((void**)cascade_f); /* Set the Region of Interest: estimate the eyes' position */ cvSetImageROI( img, /* the source image */ cvRect( face->x, /* x = start from leftmost */ face->y + (face->height/5.5), /* y = a few pixels from the top */ face->width, /* width = same width with the face */ face->height/3.0 /* height = 1/3 of face height */ ) ); /*load eye cascade*/ CvHaarClassifierCascade* cascade_e = 0; const char* cascade_name2 = "I://imgs//haarcascade_eye.xml"; cascade_e = (CvHaarClassifierCascade*)cvLoad( cascade_name2, 0, 0, 0 ); assert(cascade_e != NULL); //storage = cvCreateMemStorage(0) ; /* detect the eyes */ CvSeq *eyes = cvHaarDetectObjects( img, /* the source image, with the estimated location defined */ cascade_e, /* the eye classifier */ storage, /* memory buffer */ 1.15, 3, 0, /* tune for your app */ cvSize(25, 15) /* minimum detection scale */ ); int i; /* draw a rectangle for each detected eye */ for( i = 0; i < (eyes ? eyes->total : 0); i++ ) { /* get one eye */ CvRect *eye = (CvRect*)cvGetSeqElem(eyes, i); /* draw a red rectangle */ cvRectangle( img, cvPoint(eye->x, eye->y), cvPoint(eye->x + eye->width, eye->y + eye->height), CV_RGB(255, 0, 0), 1, 8, 0 ); } /* reset region of interest */ cvResetImageROI(img); } /* end of detectEyes() */ void detect_eye_and_display_pic(char *argv) { IplImage* img = cvLoadImage( argv ); //opens a window on the screen that can //contain and display an image cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE ); //detect eyes in the image detectEyes(img); //Whenever we have an image in the form of an IplImage* pointer, we can display it in an //existing window with cvShowImage(). //The cvShowImage() function requires that a named //window already exist (created by cvNamedWindow()). cvShowImage( "Example1", img ); //The cvWaitKey() function asks the program to stop and wait for a keystroke cvWaitKey(0); //free the allocated memory cvReleaseImage( &img ); cvDestroyWindow( "Example1" ); } 

你可能感兴趣的:(object,image,function,buffer,Parameters,each)