-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
262 lines (188 loc) · 5.92 KB
/
Main.cpp
File metadata and controls
262 lines (188 loc) · 5.92 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#define _USE_MATH_DEFINES
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include <vector>
#include <fstream>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
using std::cout;
double angulo = 0;
int tampleX = 0;
cv::Mat temple;
int zoom = 4;
int thresh = 66;
int matrix[1000][3];
bool visualization = false;
typedef struct _Eye {
Point* center;
float pupilSize;
} Eye;
Eye getPupilCenter(Mat frame)
{
Point* defPoint = new Point(0, 0);
float defSize = 0;
Eye eye = { defPoint, defSize };
Mat subFrame, frame_gray, tempMAT;
//Corte da imagem e conversão de RGB para tons de cinza
int roiX = frame.cols * 0.30;
int roiY = frame.rows * 0.20;
int roiW = frame.cols * 0.50;
int roiH = frame.rows * 0.60;
Rect ROI = Rect(roiX, roiY, roiW, roiH);
subFrame = frame(ROI);
cvtColor(subFrame, frame_gray, CV_RGB2GRAY);
//Limiar
threshold(frame_gray, frame_gray, thresh, 255, 1);
//Canny
Canny(frame_gray, frame_gray, 100, 255);
//Morfologia
Mat kernel;
int tamanho = 5;
kernel = cv::getStructuringElement(CV_SHAPE_RECT, cv::Size(tamanho, tamanho));
dilate(frame_gray, frame_gray, kernel, Point(-1, -1), 2, 1, 1);
erode(frame_gray, frame_gray, kernel, Point(-1, -1), 2, 1, 1);
//Detecção de bordas
vector<vector<Point> > contornos;
vector<Vec4i> hierarquia;
findContours(frame_gray, contornos, hierarquia, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(roiX, roiY));
if (contornos.empty())
return eye;
int indiceDoContorno, areaDoContorno = 0, temp;
vector<Point> contorno;
for (unsigned int i = 0; i < contornos.size(); i++)
{
contorno = contornos[i];
temp = contourArea(contorno);
if (temp >= areaDoContorno)
{
indiceDoContorno = i;
areaDoContorno = temp;
}
}
Mat contourMat(frame.rows, frame.cols, CV_8UC3);
//BOUNDING-BOX
Rect aux = boundingRect(contornos[indiceDoContorno]);
drawContours(contourMat, contornos, indiceDoContorno, Scalar(255, 255, 255), 1, 8, hierarquia, 0);
if (contourArea(contornos[indiceDoContorno]) > 80)
{
//Desenho contornos e bounding-box
rectangle(contourMat, aux, Scalar(0, 255, 0), 1);
Point centro(aux.x + aux.width / 2, aux.y + aux.height / 2);
eye.pupilSize = aux.width / 2;
eye.center = ¢ro;
if (eye.pupilSize == 0) eye.pupilSize = aux.width / 2;
}
else{
//cout << contourArea(contornos[indiceDoContorno]);
//imshow("Problema", contourMat);
//waitKey(0);
}
return eye;
}
Eye getPupilCenterNew(Mat frame)
{
Point* defPoint = new Point(0, 0);
float defSize = 0;
Eye eye = { defPoint, defSize };
Mat subFrame, frame_gray, tempMAT;
// Set ROI
int roiX = frame.cols * 0.30;
int roiY = frame.rows * 0.20;
int roiW = frame.cols * 0.50;
int roiH = frame.rows * 0.60;
subFrame = frame(Rect(roiX, roiY, roiW, roiH));
//Corte da imagem e conversão de RGB para tons de cinza
cvtColor(subFrame, frame_gray, CV_RGB2GRAY);
//Limiar
threshold(frame_gray, frame_gray, thresh, 255, 1);
//Canny
//Canny(frame_gray, frame_gray, 100, 255);
//Morfologia
//Mat kernel;
//int tamanho = 5;
//kernel = cv::getStructuringElement(CV_SHAPE_RECT, cv::Size(tamanho, tamanho));
//dilate(frame_gray, frame_gray, kernel, Point(-1, -1), 2, 1, 1);
//erode(frame_gray, frame_gray, kernel, Point(-1, -1), 2, 1, 1);
//Detecção de bordas
vector<vector<Point> > contornos;
vector<Vec4i> hierarquia;
findContours(frame_gray, contornos, hierarquia, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(roiX, roiY));
if (contornos.empty()) return eye;
int indiceDoContorno, areaDoContorno = 0, temp;
vector<Point> contorno;
for (unsigned int i = 0; i < contornos.size(); i++)
{
contorno = contornos[i];
temp = contourArea(contorno);
if (temp >= areaDoContorno)
{
indiceDoContorno = i;
areaDoContorno = temp;
}
}
Mat contourMat(frame.rows, frame.cols, CV_8UC3);
//BOUNDING-BOX
Rect aux = boundingRect(contornos[indiceDoContorno]);
drawContours(contourMat, contornos, indiceDoContorno, Scalar(255, 255, 255), 1, 8, hierarquia, 0);
if (contourArea(contornos[indiceDoContorno]) > 80)
{
//Desenho contornos e bounding-box
rectangle(contourMat, aux, Scalar(0, 255, 0), 1);
Point centro(aux.x + aux.width / 2, aux.y + aux.height / 2);
eye.pupilSize = aux.width / 2;
eye.center->x = centro.x;
eye.center->y = centro.y;
if (eye.pupilSize == 0) eye.pupilSize = aux.width / 2;
}
return eye;
}
void compareCenter(VideoCapture capture)
{
Mat frame;
capture.read(frame);
while (true)
{
// Pupila usando técnica original
Mat frame_original = frame.clone();
Eye original;
original = getPupilCenter(frame_original);
// cout << original.center.x << original.center.y << original.pupilSize*2 << endl;;
circle(frame_original, *(original.center), original.pupilSize, Scalar(0, 0, 255), -1);
imshow("Original", frame_original);
// Pupila usando nova técnica
Mat frame_novo = frame.clone();
Eye novo;
novo = getPupilCenterNew(frame_novo);
// cout << novo.center.x << novo.center.y << novo.pupilSize*2 << endl;;
circle(frame_novo, *(novo.center), novo.pupilSize, Scalar(0, 0, 255), -1);
imshow("Novo", frame_novo);
// Ler resultado da avaliação
int key = cv::waitKey(0) - 49;
cout << key << endl;
// Continuar avaliação
if (!capture.read(frame)) break;
}
cv::waitKey(0);
}
int main()
{
VideoCapture capture("./Video1.avi");
if (!capture.isOpened()) { printf("Couldn't load video!"); cv::waitKey(0); return -1; }
compareCenter(capture);
return 0;
}
/*
- Video1 Works
- Video2 Doesn't
- Video3 Works
- Video4 Doesn't
- Video5 Works
*/