Sie sind auf Seite 1von 4

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace std;


using namespace cv;

Vec3f getEyeball(Mat &eye, vector<Vec3f> &circles)


{
vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
Point center((int)round(circles[i][0]),
(int)round(circles[i][1]));
int radius = (int)round(circles[i][2]);
if (pow(x - center.x, 2) + pow(y - center.y, 2) <
pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}

int smallestSum = 9999999;


int smallestSumIndex = -1;
for (int i = 0; i < circles.size(); i++)
{
if (sums[i] < smallestSum)
{
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles[smallestSumIndex];
}

Rect getLeftmostEye(vector<Rect> &eyes)


{
int leftmost = 99999999;
int leftmostIndex = -1;
for (int i = 0; i < eyes.size(); i++)
{
if (eyes[i].tl().x < leftmost)
{
leftmost = eyes[i].tl().x;
leftmostIndex = i;
}
}
return eyes[leftmostIndex];
}

Rect getRightmostEye(vector<Rect> &eyes)


{
int rightmost = 0;
int rightmostIndex = -1;
for (int i = 0; i < eyes.size(); i++)
{
if (eyes[i].tl().x > rightmost)
{
rightmost = eyes[i].tl().x;
rightmostIndex = i;
}
}
return eyes[rightmostIndex];
}

vector<Point> centers;

Point stabilize(vector<Point> &points, int windowSize)


{
float sumX = 0;
float sumY = 0;
int count = 0;
for (int i = max(0, (int)(points.size() - windowSize)); i < points.size(); i+
+)
{
sumX += points[i].x;
sumY += points[i].y;
++count;
}
if (count > 0)
{
sumX /= count;
sumY /= count;
}
return Point(sumX, sumY);
}

void detectEyes(Mat &frame,CascadeClassifier &faceCascade,CascadeClassifier


&eyeCascade)
{
Mat grayscale;
cvtColor(frame, grayscale, CV_BGR2GRAY);
equalizeHist(grayscale, grayscale);
vector<Rect> faces;
faceCascade.detectMultiScale(grayscale, faces, 1.1, 2, 0 |
CV_HAAR_SCALE_IMAGE, Size(150, 150));
if (faces.size() == 0) return;
Mat face = grayscale(faces[0]);
vector<Rect> eyes;
eyeCascade.detectMultiScale(face, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,
Size(30, 30));
rectangle(frame, faces[0].tl(), faces[0].br(),Scalar(0, 0, 0), 2);
if (eyes.size() != 2) return;
for (Rect &eye : eyes)
{
rectangle(frame, faces[0].tl() + eye.tl(), faces[0].tl() + eye.br(),
Scalar(0, 255, 0), 2);
}
Rect eyeRect = getLeftmostEye(eyes);
Mat eye = face(eyeRect);
equalizeHist(eye, eye);
Rect eyeRect1 = getRightmostEye(eyes);
Mat eye1 = face(eyeRect1);
equalizeHist(eye1, eye1);

vector<Vec3f> circles;
HoughCircles(eye, circles, CV_HOUGH_GRADIENT, 1, eye.cols / 8, 250, 15,
eye.rows / 8, eye.rows / 3);

if (circles.size() > 0)
{
Vec3f eyeball = getEyeball(eye, circles);
Point center(eyeball[0], eyeball[1]);
centers.push_back(center);
center = stabilize(centers, 10);
int radius = (int)eyeball[2];
int radius1 = (int)eyeball[2];
circle(frame, faces[0].tl() + eyeRect.tl() + center, radius,
Scalar(255, 0, 0), 2);
circle(eye, center, radius, Scalar(255, 255, 255), 2);
circle(frame, faces[0].tl() + eyeRect1.tl() + center, radius,
Scalar(255, 0, 0), 2);
circle(eye1, center, radius, Scalar(255, 255, 255), 2);

int main(int argc, char **argv)


{

CascadeClassifier faceCascade;
CascadeClassifier eyeCascade;
if (!
faceCascade.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.x
ml"))
{
cerr << "Could not load face detector." << endl;
return -3;
}
if (!
eyeCascade.load("C:/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasse
s.xml"))
{
cerr << "Could not load eye detector." << endl;
return -4;
}

VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Webcam not detected." << endl;
return -1;
}
Mat frame;

while (1)
{
cap >> frame;
cap.read(frame);
if (!frame.data) break;
detectEyes(frame, faceCascade, eyeCascade);
imshow("Webcam", frame);
if (waitKey(70) >= 0) break;
}
return 0;
}