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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* mariokart.cpp
*
* Copyright 2012 Florian Jung <florian.a.jung@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "os.h"
#include <vector>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include "util.h"
#include "steer_interface.h"
#include "steer_accumulator.h"
#include "naive_steerer.h"
#include "horizon_steerer.h"
#include "road_thresholder_iface.h"
#include "road_thresholder.h"
#include "xorg_grabber.h"
#include "joystick.h"
#include <iostream>
#include <list>
#include <assert.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#define HIST_SMOOTH 7
//#define NO_BRIGHTNESS // will man nicht, nur zu demonstrationszwecken
#define ERODE_RADIUS_2D 4
#define IMG_HISTORY 3
int crosshair_x=0, crosshair_y=0;
void mouse_callback(int event, int x, int y, int flags, void* userdata)
{
if (event==EVENT_LBUTTONDOWN)
{
crosshair_x=x;
crosshair_y=y;
}
}
sem_t thread1_go;
sem_t thread1_done;
Mat thread1_img;
void* thread1_func(void*)
{
Mat gray;
static Mat gray_prev;
static std::vector<cv::Point2f> points[2];
std::vector<uchar> status; // status of tracked features
std::vector<float> err; // error in tracking
cout << "thread 1 is alive :)" <<endl;
while(1)
{
sem_wait(&thread1_go);
sem_post(&thread1_done);
sem_wait(&thread1_go);
// now we have our private image at thread1_img.
/*
cvtColor(thread1_img, gray, CV_BGR2GRAY);
if (points[0].size() <= 2000) // we need more points
{
std::vector<cv::Point2f> features; // detected features
// detect the features
goodFeaturesToTrack(gray, // the image
features, // the output detected features
3000, // the maximum number of features
0.2, // quality level
10); // min distance between two features
// add the detected features to
// the currently tracked features
points[0].insert(points[0].end(), features.begin(),features.end());
}
// for first image of the sequence
if(gray_prev.empty())
gray.copyTo(gray_prev);
cv::calcOpticalFlowPyrLK(
gray_prev, gray, // 2 consecutive images
points[0], // input point positions in first image
points[1], // output point positions in the 2nd image
status, // tracking success
err); // tracking error
int k=0;
for(int i=0; i < points[1].size(); i++) {
// do we keep this point?
if (status[i]) {
// keep this point in vector
points[0][k] = points[0][i];
points[1][k++] = points[1][i];
}
}
// eliminate unsuccesful points
points[0].resize(k);
points[1].resize(k);
// for all tracked points
for(int i= 0; i < points[1].size(); i++ ) {
// draw line and circle
cv::line(thread1_img,
points[0][i], // initial position
points[1][i],// new position
cv::Scalar(255,255,255));
cv::circle(thread1_img, points[1][i], 2,
cv::Scalar(255,255,255),-1);
}
// 4. extrapolate movement
for (int i=0;i<points[0].size();i++)
{
points[0][i].x = points[1][i].x+(points[1][i].x-points[0][i].x);
points[0][i].y = points[1][i].y+(points[1][i].y-points[0][i].y);
}
cv::swap(gray_prev, gray);
*/
// now we must stop accessing thread1_img, main() may access it
sem_post(&thread1_done);
}
}
int main(int argc, char* argv[])
{
try {
if (sem_init(&thread1_go, 0, 0)) throw string("sem_init failed");
if (sem_init(&thread1_done, 0, 0)) throw string("sem_init failed");
pthread_t thread1;
if (pthread_create(&thread1, NULL, thread1_func, NULL)) throw string("pthread_create failed");
string tmp;
#ifdef LINUX
Joystick joystick;
cout << "joystick initalized, now starting mupen." << endl;
#endif
if (fork()==0) { system("mupen64plus --nogui --noask ~/MarioKart64.rom"); exit(0); }
#ifdef FREEBSD
sleep(2);
Joystick joystick;
#endif
sleep(1);
joystick.reset();
cout << "successfully reset joystick." << endl;
/* cout << "press enter to steer left" << endl;
getchar();
cout << "now steering left. press enter to continue." << endl;
joystick.steer(-1.0);
getchar();
cout << "centered. press enter to steer right." << endl;
joystick.steer(0.0);
getchar();
cout << "steering right" << endl;
joystick.steer(1.0);
getchar();
cout << "centered. press enter to press a." << endl;
joystick.steer(0.0);
getchar();
cout << "pressing A" << endl;
joystick.press_a(true);
getchar();
cout << "A released." << endl;
joystick.press_a(false);
getchar();*/
cout << "waiting for game to start, press enter when started." << endl;
joystick.press_a(false);
joystick.reset();
getchar();
joystick.reset();
#ifdef LINUX
XorgGrabber capture("glN64");
#endif
#ifdef FREEBSD
XorgGrabber capture("Mupen64Plus OpenGL Video");
#endif
Mat transform;
bool first=true;
int xlen, ylen;
Mat erode_2d_small=circle_mat(3);
Mat erode_2d_big=circle_mat(10);
#define trans_width 600
#define trans_height 400
#define road_width 100
namedWindow("edit");
setMouseCallback("edit", mouse_callback, NULL);
joystick.throttle(0.5);
VideoWriter img_writer, thres_writer, thres2_writer;
Mat img_;
capture.read(img_);
xlen=img_.cols;
ylen=img_.rows;
crosshair_x=xlen/2;
crosshair_y=0.58*ylen;
Point2f src_pts[4] = { Point2f(0, ylen), Point2f(xlen, ylen), Point2f(xlen* (.5 - 0.13), ylen/2), Point2f(xlen* (.5 + .13), ylen/2) };
//Point2f dest_pts[4] = { Point2f(0, ylen), Point2f(trans_width, ylen), Point2f(0, trans_height), Point2f(0, trans_height) };
Point2f dest_pts[4] = { Point2f(trans_width/2 - road_width/2, trans_height), Point2f(trans_width/2 + road_width/2, trans_height), Point2f(trans_width/2 - road_width/2, trans_height/2), Point2f(trans_width/2 + road_width/2, trans_height/2) };
transform=getPerspectiveTransform(src_pts, dest_pts);
//img_writer.open("img.mpg", CV_FOURCC('P','I','M','1'), 30, Size(xlen,ylen), false);
img_writer.open("img.mpg", CV_FOURCC('P','I','M','1'), 30, Size(xlen,ylen), false);
thres_writer.open("thres.mpg", CV_FOURCC('P','I','M','1'), 30, Size(xlen,ylen), false);
thres2_writer.open("thres2.mpg", CV_FOURCC('P','I','M','1'), 30, Size(xlen,ylen), false);
if (!img_writer.isOpened() || !thres_writer.isOpened() || !thres2_writer.isOpened())
{
cout << "ERROR: could not open video files!" << !img_writer.isOpened() << !thres_writer.isOpened() << !thres2_writer.isOpened() <<endl;
}
SteerAccumulator* steerer = new SteerAccumulator();
NaiveSteerer* naive_steerer = new NaiveSteerer(xlen/2, 0.58*ylen);
HorizonSteerer* hor_steerer = new HorizonSteerer(xlen,ylen);
steerer->add_steerer(naive_steerer, 1.0);
steerer->add_steerer(hor_steerer, 5.0);
RoadThresholderIface* road_thresholder = new RoadThresholder();
while(1)
{
capture.read(img_);
assert ((img_.cols==xlen) && (img_.rows==ylen));
Mat img;
img_.convertTo(img, CV_8UC3, 1); //FINDMICH
img.copyTo(thread1_img); sem_post(&thread1_go);
#ifdef NO_BRIGHTNESS
//assert(img.type()==CV_8UC3);
for (int row = 0; row<img.rows; row++)
{
uchar* data=img.ptr<uchar>(row);
for (int col=0; col<img.cols;col++)
{
int sum=data[0] + data[1] + data[2];
if (sum>0)
{
data[0]=(int)data[0] * 256 / sum;
data[1]=(int)data[1] * 256 / sum;
data[2]=(int)data[2] * 256 / sum;
}
else
{
data[0]=255/3;
data[1]=255/3;
data[2]=255/3;
}
data+=img.step[1];
}
}
#endif
road_thresholder->process_image(img);
Mat img_thres = road_thresholder->get_road();
Mat img_eroded(img_thres.rows, img_thres.cols, img_thres.type());
Mat img_tmp(img_thres.rows, img_thres.cols, img_thres.type());
Mat img_thres2(img_thres.rows, img_thres.cols, img_thres.type());
erode(img_thres, img_tmp, erode_2d_small);
dilate(img_tmp, img_thres, erode_2d_small);
dilate(img_thres, img_tmp, erode_2d_big);
erode(img_tmp, img_thres2, erode_2d_big);
assert(img.rows==img_eroded.rows);
assert(img.cols==img_eroded.cols);
/*
Mat img_perspective(trans_height, trans_width, img_thres.type());
warpPerspective(img_thres, img_perspective, transform, img_perspective.size());
threshold(img_perspective, img_perspective, 127, 255, THRESH_BINARY);
Mat img_perspective_temp(img_perspective.rows, img_perspective.cols, img_perspective.type());
Mat img_perspective_temp2(img_perspective.rows, img_perspective.cols, img_perspective.type());
erode(img_perspective, img_perspective_temp, circle_mat(7));
dilate(img_perspective_temp, img_perspective_temp2, circle_mat(7 + 15));
erode(img_perspective_temp2, img_perspective, circle_mat(15));
*/
steerer->process_image(img_thres2);
double steer_value = steerer->get_steer_data();
hor_steerer->process_image(img_thres2);
img.row(crosshair_y)=Scalar(255,0,0);
img.col(crosshair_x)=Scalar(255,0,0);
img_thres2.row(crosshair_y)=128;
img_thres2.col(crosshair_x)=128;
Mat steer=Mat::zeros(20,1920,CV_8U);
steer.col( steer.cols /2 )=128;
if (steerer->get_confidence()>0)
{
int x = (steer_value/2.0 + 0.5)*steer.cols;
if (x<1) x=1;
if (x>=steer.cols-1) x=steer.cols-2;
steer.col(x) = 255;
steer.col(x-1)=240;
steer.col(x+1)=240;
joystick.steer( steer_value,0.05);
}
else
joystick.steer(0.0);
sem_wait(&thread1_done); // wait for thread1 to finish
//imshow("orig", img);
imshow("edit", img);
//imshow("perspective", img_perspective);
//imshow("diff", img_diff);
imshow("thres", img_thres);
imshow("thres2", img_thres2);
imshow("tracked", thread1_img);
imshow("steer", steer);
//img_writer << img_diff;
thres_writer << img_thres;
thres2_writer << img_thres2;
waitKey(1000/50);
joystick.process();
}
} //try
catch(string meh)
{
cout << "error: "<<meh<< endl;
}
catch(...)
{
cout << "error!" << endl;
}
sem_destroy(&thread1_go);
sem_destroy(&thread1_done);
}
|