Repository navigation
Expand file tree
/
Copy pathvpMain.cpp
More file actions
223 lines (185 loc) · 4.44 KB
/
Copy pathvpMain.cpp
File metadata and controls
223 lines (185 loc) · 4.44 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
#include "stdafx.h"
#include "vpProcess.h"
#include "vpLed.h"
#include "vpCalib.h"
#include <algorithm>
using namespace std;
using namespace cv;
int main(int argc, char ** argv)
{
vpStereoCamera cam;
vpLed leds;
// startup the stereo cameras
if (!cam.init())
{
fprintf(stderr, "Camera initialization failed. Exiting.\n");
return 1;
}
vpConfig::setCamera(&cam);
vpConfig::init();
// connect with the LEDs and turn them on via serial
leds.open(L"COM3");
leds.on();
// load Haar classifiers
if (!vpProcess::init())
{
fprintf(stderr, "Failed to load Haar classifiers. Exiting.\n");
return 1;
}
// initialize tracker output images
vpProcess* process = new vpProcess(cam.getImageLeft());
// calibration handling struct
vpCalib* calib = NULL;
// did we just signal to capture an image?
bool capImage = false;
while (1)
{
int c = cvWaitKey(10);
if (c == 27) break;
if (c == 'd')
{
if (!calib)
{
// start
calib = new vpCalib(false);
}
}
if (c == 'c')
{
if (!calib)
{
// start
calib = new vpCalib();
}
else
{
if (calib->isCapturing())
{
// calculate positions of mirror in captured images
if (!calib->doCalibrate(VP_CALIB_BWIDTH,VP_CALIB_BHEIGHT,VP_CALIB_BDIM))
{
// calibration failed for some reason
delete calib;
calib = NULL;
}
}
else
{
// do final LED/screen position calculations
calib->finishCalibrate();
delete calib;
calib = NULL;
}
}
}
if (c == 'n' && calib)
calib->next();
if (c == 'p' && calib)
calib->previous();
if (c == 'x' && calib)
calib->clearpt();
if (c == ' ')
capImage = true;
if (c == '[')
leds.dim();
if (c == ']')
leds.on();
if (c == 'j')
cam.showSettingsLeft();
if (c == 'k')
cam.showSettingsRight();
// ascii codes magicks
if (c >= '1' && c <= '9' && calib)
calib->setTarget(c - '1');
if (cam.pollCamera())
{
Mat img[2];
img[0] = cam.getGrayLeft();
img[1] = cam.getGrayRight();
vpConfig::tick("Img Acq");
// are we calibrating? then calibrate, dammit...
if (calib)
{
// update with live image if capturing, or else with stored images for static
calib->updateImage();
// grab an image if spacebar pressed
if (capImage && calib->isCapturing())
{
capImage = false;
calib->addImage();
}
// skip the rest of the tracking stuff
continue;
}
// check to make sure we have found eyes for both cameras
for (int i=0; i<2; i++)
{
// set part of the image
process->setOutImage(img[i], i);
if (process->eyesFound[i])
continue;
// and now find them eyes
process->findEyes(i);
}
// draw where our found eyes are, if found
process->drawEyeDebug();
cvShowImage("Camera Images", &(CvMat)process->curImgOut);
// verify eye locations for consistency
if (!process->checkEyes())
continue;
vpConfig::tick("Img Manip");
// found eyes, now locate glints
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
process->eyes2[i][j].setImages(img[i]);
CvStarDetectorParams params = cvStarDetectorParams(10, 15, 10, 8, 8);
int nfound = process->eyes2[i][j].findGlints(params);
if (process->drawDebug)
process->eyes2[i][j].drawGlints();
if (nfound == 0)
process->eyesFound[i] = false;
}
}
vpConfig::tick("Glints");
// still ok to continue?
if (!process->checkEyes())
continue;
// find corneas, if we can
for (int i=0; i<2; i++)
{
process->eyes3[i].findCorneaCenter();
process->eyes3[i].drawCorneaCenter();
}
vpConfig::tick("Cornea");
// and pupils in each image
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
process->eyes2[i][j].findPupilRough();
process->eyes2[i][j].refinePupil();
process->eyes2[i][j].drawPupil();
}
}
vpConfig::tick("Pupil");
// now find the screen coords!
for (int i=0; i<2; i++)
{
process->eyes3[i].findPupilCenter();
process->eyes3[i].findScreenPoint();
}
Point2f avgPos = 0.5f*(process->eyes3[0].screenPos+process->eyes3[1].screenPos);
//SetCursorPos((int)avgPos.x,(int)avgPos.y);
vpConfig::tick("Screen");
cvShowImage("Eyes Tracked", &(CvMat)process->eyesOut);
// now lastly update eye positions
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
process->eyes2[i][j].setCenter(process->eyes3[j].corneaPos2[i]);
}
}
vpConfig::close();
return 0;
}