forked from alessandroferrari/MuHi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindEyeCenter.cpp
218 lines (195 loc) · 6.56 KB
/
findEyeCenter.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
218
#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"
// Pre-declarations
cv::Mat floodKillEdges(cv::Mat &mat);
#pragma mark Visualization
/*
template<typename T> mglData *matToData(const cv::Mat &mat) {
mglData *data = new mglData(mat.cols,mat.rows);
for (int y = 0; y < mat.rows; ++y) {
const T *Mr = mat.ptr<T>(y);
for (int x = 0; x < mat.cols; ++x) {
data->Put(((mreal)Mr[x]),x,y);
}
}
return data;
}
void plotVecField(const cv::Mat &gradientX, const cv::Mat &gradientY, const cv::Mat &img) {
mglData *xData = matToData<double>(gradientX);
mglData *yData = matToData<double>(gradientY);
mglData *imgData = matToData<float>(img);
mglGraph gr(0,gradientX.cols * 20, gradientY.rows * 20);
gr.Vect(*xData, *yData);
gr.Mesh(*imgData);
gr.WriteFrame("vecField.png");
delete xData;
delete yData;
delete imgData;
}*/
#pragma mark Helpers
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;
}
#pragma mark Main Algorithm
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);
// square and multiply by the weight
if (kEnableWeight) {
Or[cx] += dotProduct * dotProduct * (Wr[cx]/kWeightDivisor);
} else {
Or[cx] += dotProduct * dotProduct;
}
}
}
}
cv::Point findEyeCenter(cv::Mat face, cv::Rect eye) {
cv::Mat eyeROIUnscaled = face(eye);
cv::Mat eyeROI;
scaleToFastSize(eyeROIUnscaled, eyeROI);
//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);
//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;
}
}
}
//-- Create a blurred and inverted image for weighting
cv::Mat weight;
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]);
}
}
//-- 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.
//printf("Eye Size: %ix%i\n",outSum.cols,outSum.rows);
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) {
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);
//Find the maximum point
cv::Point maxP;
double maxVal;
cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP);
//Flood fill the edges
if(kEnablePostProcess) {
cv::Mat floodClone;
//double floodThresh = computeDynamicThreshold(out, 1.5);
double floodThresh = maxVal * kPostProcessThreshold;
cv::threshold(out, floodClone, floodThresh, 0.0f, cv::THRESH_TOZERO);
// if(kPlotVectorField) {
// //plotVecField(gradientX, gradientY, floodClone);
// imwrite("eyeFrame.png",eyeROIUnscaled);
// }
cv::Mat mask = floodKillEdges(floodClone);
// redo max
cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP,mask);
}
return unscalePoint(maxP,eye);
}
#pragma mark Postprocessing
bool floodShouldPushPoint(const cv::Point &np, const cv::Mat &mat) {
return inMat(np, mat.rows, mat.cols);
}
// returns a mask
cv::Mat floodKillEdges(cv::Mat &mat) {
//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;
}