-
Notifications
You must be signed in to change notification settings - Fork 2
/
eye_center.cpp
217 lines (196 loc) · 7.02 KB
/
eye_center.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
//#include <mgl2/mgl.h>
#include <iostream>
#include <queue>
#include <stdio.h>
//#include "constants.h"
//#include "helpers.h"
const int kFastEyeWidth = 50; //resize eye region , keep aspect ratio
const double kGradientThreshold = 50.0;//calc gradient binary map
const int kWeightBlurSize = 5; //gausian kernel size
const float kPostProcessThreshold = 0.97;// remove
// Pre-declarations
cv::Mat floodKillEdges(cv::Mat &mat);
#pragma mark Visualization
#pragma mark Helpers
double computeDynamicThreshold(const cv::Mat &mat, double stdDevFactor) {
cv::Scalar stdMagnGrad, meanMagnGrad;
cv::meanStdDev(mat, meanMagnGrad, stdMagnGrad);
double stdDev = stdMagnGrad[0] / sqrt(mat.rows*mat.cols);
return stdDevFactor * stdDev + meanMagnGrad[0];
}
cv::Mat matrixMagnitude(const cv::Mat &matX, const cv::Mat &matY) {
cv::Mat mags(matX.rows,matX.cols,CV_64F);
for (int y = 0; y < matX.rows; ++y) {
const double *Xr = matX.ptr<double>(y), *Yr = matY.ptr<double>(y);
double *Mr = mags.ptr<double>(y);
for (int x = 0; x < matX.cols; ++x) {
double gX = Xr[x], gY = Yr[x];
double magnitude = sqrt((gX * gX) + (gY * gY));
Mr[x] = magnitude;
}
}
return mags;
}
/***scale sys ROI to small region , for fater computing *****/
cv::Point unscalePoint(cv::Point p, cv::Rect origSize) {
float ratio = (((float)kFastEyeWidth)/origSize.width);
int x = round(p.x / ratio);
int y = round(p.y / ratio);
return cv::Point(x,y);
}
void scaleToFastSize(const cv::Mat &src,cv::Mat &dst) {
cv::resize(src, dst, cv::Size(kFastEyeWidth,(((float)kFastEyeWidth)/src.cols) * src.rows));
}
cv::Mat computeMatXGradient(const cv::Mat &mat) {
cv::Mat out(mat.rows,mat.cols,CV_64F);
for (int y = 0; y < mat.rows; ++y) {
const uchar *Mr = mat.ptr<uchar>(y);
double *Or = out.ptr<double>(y);
Or[0] = Mr[1] - Mr[0];
for (int x = 1; x < mat.cols - 1; ++x) {
Or[x] = (Mr[x+1] - Mr[x-1])/2.0;
}
Or[mat.cols-1] = Mr[mat.cols-1] - Mr[mat.cols-2];
}
return out;
}
void testPossibleCentersFormula(int x, int y, const cv::Mat &weight,double gx, double gy, cv::Mat &out) {
// for all possible centers
for (int cy = 0; cy < out.rows; ++cy) {
double *Or = out.ptr<double>(cy);
const unsigned char *Wr = weight.ptr<unsigned char>(cy);
for (int cx = 0; cx < out.cols; ++cx) {
if (x == cx && y == cy) {
continue;
}
// create a vector from the possible center to the gradient origin
double dx = x - cx;
double dy = y - cy;
// normalize d
double magnitude = sqrt((dx * dx) + (dy * dy));
dx = dx / magnitude;
dy = dy / magnitude;
double dotProduct = dx*gx + dy*gy;
dotProduct = std::max(0.0,dotProduct);//if dotProduct, inside white outside black, not belong to eye
// square and multiply by the weight
Or[cx] += dotProduct * dotProduct * (Wr[cx]);
}
}
}
cv::Point findEyeCenter(cv::Mat face, cv::Rect eye, std::string debugWindow) {
cv::Mat eyeROIUnscaled = face(eye);
cv::Mat eyeROI;
scaleToFastSize(eyeROIUnscaled, eyeROI);
// draw eye region
// rectangle(face,eye,1234);
//-- Find the gradient
cv::Mat gradientX = computeMatXGradient(eyeROI);
cv::Mat gradientY = computeMatXGradient(eyeROI.t()).t();
//-- Normalize and threshold the gradient
// compute all the magnitudes
cv::Mat mags = matrixMagnitude(gradientX, gradientY);
//compute the threshold
double gradientThresh = computeDynamicThreshold(mags, kGradientThreshold);
//double gradientThresh = kGradientThreshold;
//double gradientThresh = 0;
//normalize
for (int y = 0; y < eyeROI.rows; ++y) {
double *Xr = gradientX.ptr<double>(y), *Yr = gradientY.ptr<double>(y);
const double *Mr = mags.ptr<double>(y);
for (int x = 0; x < eyeROI.cols; ++x) {
double gX = Xr[x], gY = Yr[x];
double magnitude = Mr[x];
if (magnitude > gradientThresh) {
Xr[x] = gX/magnitude;
Yr[x] = gY/magnitude;
} else {
Xr[x] = 0.0;
Yr[x] = 0.0;
}
}
}
cv::imshow(debugWindow, gradientX);
// cv::waitKey(0);
//-- Create a blurred and inverted image for weighting
cv::Mat weight;
cv::GaussianBlur( eyeROI, weight, cv::Size( kWeightBlurSize, kWeightBlurSize ), 0, 0 );
for (int y = 0; y < weight.rows; ++y) {
unsigned char *row = weight.ptr<unsigned char>(y);
for (int x = 0; x < weight.cols; ++x) {
row[x] = (255 - row[x]);
}
}
cv::imshow(debugWindow, weight);
// cv::waitKey(0);
//-- Run the algorithm!
cv::Mat outSum = cv::Mat::zeros(eyeROI.rows,eyeROI.cols,CV_64F);
// for each possible gradient location
// Note: these loops are reversed from the way the paper does them
// it evaluates every possible center for each gradient location instead of
// every possible gradient location for every center.
for (int y = 0; y < weight.rows; ++y) {
const double *Xr = gradientX.ptr<double>(y), *Yr = gradientY.ptr<double>(y);
for (int x = 0; x < weight.cols; ++x) {
double gX = Xr[x], gY = Yr[x];
if (gX == 0.0 && gY == 0.0) {//low computity
continue;
}
testPossibleCentersFormula(x, y, weight, gX, gY, outSum);
}
}
// scale all the values down, basically averaging them
double numGradients = (weight.rows*weight.cols);
cv::Mat out;
outSum.convertTo(out, CV_32F,1.0/numGradients);
cv::imshow(debugWindow,out);
// cv::waitKey(0);
//-- Find the maximum point
cv::Point maxP;
double maxVal;
cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP); //calc maxVal and maxPos
//-- post processing, remove too high reaction and border of image
cv::Mat floodClone;
//double floodThresh = computeDynamicThreshold(out, 1.5);
double floodThresh = maxVal * kPostProcessThreshold;
cv::threshold(out, floodClone, floodThresh, 0.0f, cv::THRESH_TOZERO);
cv::Mat mask = floodKillEdges(floodClone);
//imshow(debugWindow + " Mask",mask);
//imshow(debugWindow,out);
// redo max
cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP,mask);
// return maxP;
return unscalePoint(maxP,eye);
}
bool floodShouldPushPoint(const cv::Point &np, const cv::Mat &mat) {
return np.x>=0 && np.x<mat.cols && np.y>=0 && np.y<mat.rows;
}
// returns a mask
cv::Mat floodKillEdges(cv::Mat &mat) {
cv::rectangle(mat,cv::Rect(0,0,mat.cols,mat.rows),255);
cv::Mat mask(mat.rows, mat.cols, CV_8U, 255);
std::queue<cv::Point> toDo;
toDo.push(cv::Point(0,0));
while (!toDo.empty()) {
cv::Point p = toDo.front();
toDo.pop();
if (mat.at<float>(p) == 0.0f) {
continue;
}
// add in every direction
cv::Point np(p.x + 1, p.y); // right
if (floodShouldPushPoint(np, mat)) toDo.push(np);
np.x = p.x - 1; np.y = p.y; // left
if (floodShouldPushPoint(np, mat)) toDo.push(np);
np.x = p.x; np.y = p.y + 1; // down
if (floodShouldPushPoint(np, mat)) toDo.push(np);
np.x = p.x; np.y = p.y - 1; // up
if (floodShouldPushPoint(np, mat)) toDo.push(np);
// kill it
mat.at<float>(p) = 0.0f;
mask.at<uchar>(p) = 0;
}
return mask;
}