-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEyeDetection.cpp
More file actions
90 lines (73 loc) · 2.76 KB
/
Copy pathEyeDetection.cpp
File metadata and controls
90 lines (73 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "EyeDetection.h"
#include "ImageUtility.h"
EyeDetection::EyeDetection(const char* cascadeFileName)
{
storage = cvCreateMemStorage(0);
// Load the HaarClassifier Cascade
cascade = (CvHaarClassifierCascade*)cvLoad(cascadeFileName, 0, 0, 0 );
//if(!cascade)
// printf("Fail to load the Cascade XML file\n");
}
EyeDetection::~EyeDetection()
{
if(cascade != NULL)
cvFree(&cascade);
if(storage != NULL)
cvReleaseMemStorage(&storage);
}
/**
* @param scales 4: 2048x2048(MBGC Data), 2: 1024x1024, 1: 512x512
*
* @note/w = 200, h = 45: Set the minimum size of the two eye regions to avoid
* false detection
*/
EyeDetection::RESULT* EyeDetection::detect(IplImage* img, int scales, int val, int w, int h)
{
if(storage != NULL)
cvClearMemStorage(storage);
if(!cascade)
return NULL;
int space; // for the nose bridge
CvPoint pt1, pt2;// for retangles
// Downsample input image to boost a performance (about 7 times faster)
IplImage* imgSmall = NULL;
imgSmall = cvCreateImage(cvSize(img->width/scales,img->height/scales), img->depth, img->nChannels );
cvResize(img, imgSmall);
// There can be more than one eye in an image. Therefore, create a
// growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* eyes = 0;
// Use downscaled image
eyes = cvHaarDetectObjects(imgSmall, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40));
// Something was detected
for(int i = 0; i < (eyes ? eyes->total : 0); i++ )
{
CvRect r = *(CvRect*)cvGetSeqElem(eyes, i);
// Use the original size again
r.x = r.x*scales;
r.y = r.y*scales;
r.width = r.width*scales;
r.height = r.height*scales;
// An eye; ignore noise; values are depended on your image resolution
// 800 (=200x4) and 180 (=45x4) are for MBGC FaceNIRVideo dataset
if(r.width > w*scales && r.height > h*scales)
{
// Create a new rectangle for extracting the eye(s)
pt1.x = r.x;
pt2.x = (r.x + r.width);//devide 2.5 1.5
pt1.y = r.y;
pt2.y = (r.y + r.height);
// Remove the nose bridge region
space = cvRound((r.width/5) / 2);
// Extract the left and right eye images out of the frame
result.rightImg = ImageUtility::extractImagePart(img, result.rightRect, pt1.x, pt1.y+val, r.width/2-space, r.height-(val*2));
result.leftImg = ImageUtility::extractImagePart(img, result.leftRect, pt1.x+(r.width/2+space), pt1.y+val, r.width/2-space, r.height-(val*2));
result.bothImg = ImageUtility::extractImagePart(img, result.bothRect, pt1.x, pt1.y, r.width, r.height);
return &result; // FIXME
}
}
if( imgSmall != NULL )
cvReleaseImage( &imgSmall);
return NULL;
}