forked from barak/quantumminigolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebcamTracker.cpp
305 lines (270 loc) · 7.81 KB
/
WebcamTracker.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Quantum Minigolf, a computer game illustrating quantum mechanics
Copyright (C) 2007 Friedemann Reinhard <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "WebcamTracker.h"
//WebcamTracker
//the constructor, copy arguments, load the calibration file and init the camera
WebcamTracker::WebcamTracker (int w, int h, int ix, int iy, int rball, float vmax, Renderer * renderer):Tracker (w, h, ix, iy, rball, vmax,
renderer)
{
cam = 0;
bool colour = false;
int
fps = 30, sourceDepth = 8;
this->cw = this->ch = 0;
ifstream calfile;
calfile.open ("result.dat");
if (calfile.fail ())
{
cam = 0;
return;
}
float
tx,
ty;
calfile >> cx;
calfile >> cy;
calfile >> tx;
calfile >> ty;
calfile >> tx;
calfile >> ty;
calfile >> xx;
calfile >> xy;
calfile >> yx;
calfile >> yy;
calfile.close ();
camera = cameraTool::findCamera ();
if (camera == NULL)
{
cam = 0;
return;
}
if (sourceDepth == 24)
colour = true;
bool success = camera->initCamera (cw, ch, colour);
if (success)
{
this->cw = cw = camera->getWidth ();
this->ch = ch = camera->getHeight ();
fps = camera->getFps ();
printf ("camera: %s\n", camera->getName ());
printf ("format: %dx%d, %dfps\n\n", cw, ch, fps);
}
else
{
printf ("could not initialize camera.\n");
camera->closeCamera ();
delete camera;
cam = 0;
camera = NULL;
return;
}
success = camera->startCamera ();
if (!success)
{
printf ("cannot start camera.\n");
camera->closeCamera ();
delete camera;
cam = 0;
camera = NULL;
return;
}
cam = 1;
camera->pauseCamera ();
}
WebcamTracker::~WebcamTracker (void)
{
if (cam)
{
camera->closeCamera ();
delete camera;
}
}
// GetHit
// extract a hit: Track the motion of the club until it swings over
// the driving point
// The motion is tracked by searching the brightest pixel of the camera image
// and then taking the intensity-weihgted position average
// in a square around this pixel
//
void
WebcamTracker::GetHit (float *v, float *phi)
{
Uint32 clock = 0, clocklast = 0;
SDL_Event dummyevent;
// do nothing if no camera is present
if (cam == 0)
return;
unsigned char *image; // the frame captured from the webcam
float pos[2], poslast[2]; // position of the club in the current and last frame
int maxpos[2]; // position of the brightest pixel
unsigned char max = 0; // brightness of the brightest pixel
float sum = 0; // brightness integral in the integrating square
//around the brightest pixel
int mw = 5; //halfwidth of the integrating square
int i, j;
bool skipped = true; // flag indicating whether the last frame was skipped
// because the club was not visible
camera->startCamera ();
while (SDL_PollEvent (&dummyevent) == 0)
{ // SDL_PollEvent : emergency exit if the user presses any key
sum = 0;
poslast[0] = pos[0];
poslast[1] = pos[1];
// acquire an image from the camera
if ((image = camera->getFrame ()) == NULL)
{
continue;
}
clocklast = clock;
clock = SDL_GetTicks ();
// find the brightest pixel
max = 0;
for (i = 0; i < cw; i++)
{
for (j = 0; j < ch; j++)
{
unsigned char pixel = image[j * cw + i];
if (pixel >= max)
{
max = pixel;
maxpos[0] = i;
maxpos[1] = j;
}
}
}
//skip the frame if it is clear that the brightest point cannot be the club
if (max < 50)
{
skipped = true;
continue;
}
if (sqrt ((maxpos[0] - cx) * (maxpos[0] - cx) +
(maxpos[1] - cy) * (maxpos[1] - cy)) > 10 * 10)
{
skipped = true;
continue;
}
// now compute the club position by taking the intnsity-weighted
// position average
// of the area around the brightest pixel
pos[0] = pos[1] = 0;
int xlower = maxpos[0] - mw;
if (xlower < 0)
xlower = 0;
int xupper = maxpos[0] + mw;
if (xupper > cw)
xupper = cw;
int ylower = maxpos[1] - mw;
if (ylower < 0)
ylower = 0;
int yupper = maxpos[1] + mw;
if (yupper > ch)
yupper = ch;
for (int i = xlower; i < xupper; i++)
{
for (int j = ylower; j < yupper; j++)
{
unsigned char pixel = image[j * cw + i];
sum += (float) pixel;
pos[0] += i * (float) pixel;
pos[1] += j * (float) pixel;
}
}
pos[0] = pos[0] / sum;
pos[1] = pos[1] / sum;
Uint32 clock_after_pos = SDL_GetTicks ();
//cout << pos[0] << " " << pos[1] << " " << (float)max << endl;
// do not look for a hit if the last frame had been skipped
if (skipped)
{
skipped = false;
continue;
}
// float target[2]; target[0] = cx; target[1] = cy;
// float lastnow[2]; lastnow[0]=pos[0]-poslast[0]; lastnow[1]=pos[1]-poslast[1];
// float tarlast[2]; tarlast[0]=target[0]-poslast[0];tarlast[1]=target[1]-poslast[1];
// float tarnow[2]; tarnow[0]=target[0]-pos[0];tarnow[1]=target[1]-pos[1];
// create float values for screen coordinates of
//club position in the current frame
//club position in the last frame
//target position
float fn[2];
fn[0] = pos[0];
fn[1] = pos[1];
float fl[2];
fl[0] = poslast[0];
fl[1] = poslast[1];
float ft[2];
ft[0] = cx;
ft[1] = cy;
// float ft[2]; ft[0]=target[0];ft[1]=target[1];
cam2scr (fn, fn + 1);
cam2scr (fl, fl + 1);
cam2scr (ft, ft + 1);
// compute difference vectors between
// fln: last and current position
// ftl: last position and target
// ftn: cur. position and target
float fln[2];
fln[0] = fn[0] - fl[0];
fln[1] = fn[1] - fl[1];
float ftl[2];
ftl[0] = fl[0] - ft[0];
ftl[1] = fl[1] - ft[1];
float ftn[2];
ftn[0] = fn[0] - ft[0];
ftn[1] = fn[1] - ft[1];
float nfln = sqrt (fln[0] * fln[0] + fln[1] * fln[1]);
// float flny[2];
// compute projection of tl onto ln and its orthognal rest
float nflnx = -(ftl[0] * fln[0] + ftl[1] * fln[1]) / nfln;
float nflny = sqrt (ftl[0] * ftl[0] + ftl[1] * ftl[1] - nflnx * nflnx);
// finally, look for a hit
if (ftl[0] * fln[0] + ftl[1] * fln[1] < 0 &&
ftn[0] * fln[0] + ftn[1] * fln[1] > 0 && nflny < 50)
{
camera->pauseCamera ();
*v = sqrt (fln[0] * fln[0] + fln[1] * fln[1]) / (clock - clocklast);
*phi = atan (fln[1] / fln[0]);
if (*v > vmax)
*v = vmax;
return;
}
}
}
// Dummy animator - if we are playing with a real club, we do not need
// to animate the hit.
void
WebcamTracker::AnimateHit (Uint32 duration, float v, float phi)
{
}
//cam2scr
// convert from camera coordinates to screen coordinates.
void
WebcamTracker::cam2scr (float *x, float *y)
{
float tx = *x - cx;
float ty = *y - cy;
*x = (float) ix + xx * tx + xy * ty;
*y = (float) iy + yx * tx + yy * ty;
}
//cam2scr
// convert from camera coordinates to screen coordinates.
// not operational yet
void
WebcamTracker::scr2cam (float *x, float *y)
{
*x -= 320;
}