-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
101 lines (63 loc) · 1.94 KB
/
stack.cpp
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<iostream>
using namespace std;
int detectEye();
void trackEye();
int main()
{
cv::VideoCapture cap(0);
cv::Mat frame;
cv::Mat eye_tpl; // The eye template
cv::Rect eye_bb; // The eye bounding box
while(cv::waitKey(15) != 'q')
{
cap >> frame;
cv::Mat gray;
cv::cvtColor(frame, gray, CV_BGR2GRAY);
if (eye_bb.width == 0 && eye_bb.height == 0)
detectEye(gray, eye_tpl, eye_bb);
else
{
trackEye(gray, eye_tpl, eye_bb);
cv::rectangle(frame, eye_bb, CV_RGB(0,255,0));
}
cv::imshow("video", frame);
}
return 0;
}
int detectEye(cv::Mat& im, cv::Mat& tpl, cv::Rect& rect)
{
std::vector<cv::Rect> faces, eyes;
face_cascade.detectMultiScale(im, faces, 1.1, 2,
CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
for (int i = 0; i < faces.size(); i++)
{
cv::Mat face = im(faces[i]);
eye_cascade.detectMultiScale(face, eyes, 1.1, 2,
CV_HAAR_SCALE_IMAGE, cv::Size(20,20));
if (eyes.size())
{
rect = eyes[0] + cv::Point(faces[i].x, faces[i].y);
tpl = im(rect);
}
}
return eyes.size();
}
/
void trackEye(cv::Mat& im, cv::Mat& tpl, cv::Rect& rect)
{
cv::Size size(rect.width * 2, rect.height * 2);
cv::Rect window(rect + size - cv::Point(size.width/2, size.height/2));
window &= cv::Rect(0, 0, im.cols, im.rows);
cv::Mat dst(window.width - tpl.rows + 1, window.height - tpl.cols + 1, CV_32FC1);
cv::matchTemplate(im(window), tpl, dst, CV_TM_SQDIFF_NORMED);
double minval, maxval;
cv::Point minloc, maxloc;
cv::minMaxLoc(dst, &minval, &maxval, &minloc, &maxloc);
if (minval <= 0.2)
{
rect.x = window.x + minloc.x;
rect.y = window.y + minloc.y;
}
else
rect.x = rect.y = rect.width = rect.height = 0;
}