-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpupilDetect.cpp
More file actions
59 lines (48 loc) · 1.65 KB
/
pupilDetect.cpp
File metadata and controls
59 lines (48 loc) · 1.65 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
//
// pupilDetect.cpp
// FaceDetectionPOC
//
// Created by Sudeshna Roy on 14/03/13.
// Copyright (c) 2013 iCapps. All rights reserved.
//
#include "pupilDetect.h"
int detect2(cv::Mat src, cv::Point &Pupil, cv::Mat faceROIColor )
{
if (src.empty())
return -1;
// Invert the source image and convert to grayscale
cv::Mat gray;
cv::cvtColor(~src, gray, CV_BGR2GRAY);
// Convert to binary image by thresholding it
cv::threshold(gray, gray, 150, 255, cv::THRESH_BINARY);
// Find all contours
std::vector<std::vector<cv::Point> > contours;
cv::findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
/// Draw contours
cv::Mat drawing = cv::Mat::zeros( gray.size(), CV_8UC3 );
// for( int i = 0; i< contours.size(); i++ )
// {
// cv::Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// drawContours( drawing, contours, i, color, 2, 8, CV_RETR_EXTERNAL, 0, cv::Point() );
// }
// Fill holes in each contour
cv::drawContours(gray, contours, -1, CV_RGB(255,255,255), -1);
for (int i = 0; i < contours.size(); i++)
{
double area = cv::contourArea(contours[i]);
cv::Rect rect = cv::boundingRect(contours[i]);
int radius = rect.width/2;
// If contour is big enough and has round shape
// Then it is the pupil
if (area >= 30 &&
std::abs(1 - ((double)rect.width / (double)rect.height)) <= 0.2 &&
std::abs(1 - (area / (CV_PI * pow(radius, 2)))) <= 0.2)
{
cv::circle(src, cv::Point(rect.x + radius, rect.y + radius), 1, CV_RGB(255,0,0), 2);
Pupil.x=rect.x + radius;
Pupil.y=rect.y+radius;
break;
}
}
return 0;
}