-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
720 lines (560 loc) · 22.4 KB
/
Camera.cpp
File metadata and controls
720 lines (560 loc) · 22.4 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "Camera.hpp"
using namespace std;
using namespace cv;
using namespace cv::cuda;
unsigned int counter = 0;
unsigned int fps = 0;
struct InRangeParam {
int minH, maxH;
int minS, maxS;
int minV, maxV;
cv::cuda::GpuMat output;
};
GpuMat hsvGpu;
GpuMat hsvGpuGoal;
GpuMat outGpu, outGpu2;
GpuMat shsv[3];
GpuMat thresch[3];
GpuMat threscl[3];
GpuMat thresc[3];
GpuMat inGpu;
GpuMat ballErodeMat, goalErodeMat, greenErodeMat;
GpuMat ballDilateMat, goalDilateMat, greenDilateMat;
GpuMat ballGpuMat(WIDTH, HEIGHT, CV_8UC1);
GpuMat goalGpuMat(WIDTH, HEIGHT, CV_8UC1);
GpuMat greenGpuMat(WIDTH, HEIGHT, CV_8UC1);
Mat debugMerge(WIDTH, HEIGHT, CV_8UC3);
Mat goalMarker, ballMarker, goalMat, ballMat;
Mat mat;
Mat dst;
Mat thrMat;
Mat hsvMat;
Mat inputMat;
Mat resultMat;
Mat ballDilated;
Mat goalDilated;
Mat greenDilated;
// BGR
Scalar ballContourColor = Scalar(0, 0, 255);
Scalar goalContourColor = Scalar(0, 255, 255);
Scalar centerContourColor = Scalar(255, 33, 0);
const string BALL_FILE = "ball";
const string GREEN_FILE = "green";
vector<vector<Point>> ballContours, goalContours;
vector<Vec4i> ballHierarchy, goalHierarchy;
atomic<int> frame_rate;
atomic<int> live_stream;
atomic<bool> ball_visible;
atomic<int> ball_x;
atomic<int> ball_y;
atomic<int> ext_ball_zone;
atomic<bool> ball_close_kick;
atomic<int> goal_x;
atomic<int> goal_y;
atomic<int> goal_height;
atomic<int> goal_width;
atomic<bool> goal_visible;
atomic<bool> ext_i_see_goal_to_kick;
atomic<bool> ext_livestream;
atomic<bool> ext_attack_blue_goal;
bool attack_blue_goal;
InRangeParam ballInRangeParam, goalInRangeParam, greenInRangeParam;
int hMin, sMin, vMin;
int hMax, sMax, vMax;
int showImage, showFpsCount, imageWindowsOn;
//ofstream goal_values;
//myfile.open ("goal_values.txt");
//writing_counter = 0;
Mat element;
Ptr<cuda::Filter> erodeFilter;
Ptr<cuda::Filter> dilateFilter;
int elementShape = MORPH_ELLIPSE;
float filterArea;
int dilateMultiplier;
object goal, ball;
#define CALIBRATION true
#define MIN_GOAL_WIDTH 38
#define MIN_GOAL_HEIGHT 12
#define MIN_BALL_WIDTH 10
#define MIN_BALL_HEIGHT 10
#define GOAL_MAX_PIXEL 500
#define BALL_MAX_PIXEL 772
#define KICK_TOLERANCE 30
#define KICK_X_DIFFERENCE 70
#define BALL_Y_MIN_KICK 365
#define BALL_Y_MAX_KICK 500
struct timespec t0, t1;
xiAPIplusCameraOcv cam;
int ball_close(int x, int y) {
//first zone
float res = 0.0006484349*x*x-0.648445*x+499.613981 - FIRST_ZONE_TOLERANCE;
if (res < y) return FIRST_ZONE_NUMBER;
//second zone
res = 0.0005064493*x*x-0.5180190126*x+351.566136 - SECOND_ZONE_TOLERANCE;
if (res < y) return SECOND_ZONE_NUMBER;
//third zone
res = 0.0003762345*x*x-0.3793232269*x+268.615192 - THIRD_ZONE_TOLERANCE;
if (res < y) return THIRD_ZONE_NUMBER;
// fourth zone
res = 0.0003288547*x*x-0.3320353119*x+227.712064 - FOURTH_ZONE_TOLERANCE;
if (res < y) return FOURTH_ZONE_NUMBER;
// fifth zone
res = 0.0003011454*x*x-0.3028442648*x+201.358953 - FIFTH_ZONE_TOLERANCE;
if (res < y) return FIFTH_ZONE_NUMBER;
return BLYAT_ZONE_NUMBER;
}
byte *matToBytes(Mat image) {
int size = image.total() * image.elemSize();
byte *bytes = new byte[size];
std::memcpy(bytes,image.data,size * sizeof(byte));
return bytes;
}
Mat *uMat;
void overlayImage(const cv::Mat &background, const cv::Mat &foreground, cv::Mat &output, cv::Point2i location) {
background.copyTo(output);
// start at the row indicated by location, or at row 0 if location.y is negative.
for(int y = std::max(location.y , 0); y < background.rows; ++y)
{
int fY = y - location.y; // because of the translation
// we are done of we have processed all rows of the foreground image.
if(fY >= foreground.rows)
break;
// start at the column indicated by location,
// or at column 0 if location.x is negative.
for(int x = std::max(location.x, 0); x < background.cols; ++x)
{
int fX = x - location.x; // because of the translation.
// we are done with this row if the column is outside of the foreground image.
if(fX >= foreground.cols)
break;
// determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
double opacity =
((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3])
/ 255.;
// and now combine the background and foreground pixel, using the opacity,
// but only if opacity > 0.
for(int c = 0; opacity > 0 && c < output.channels(); ++c)
{
unsigned char foregroundPx =
foreground.data[fY * foreground.step + fX * foreground.channels() + c];
unsigned char backgroundPx =
background.data[y * background.step + x * background.channels() + c];
output.data[y*output.step + output.channels()*x + c] =
backgroundPx * (1.-opacity) + foregroundPx * opacity;
}
}
}
}
void measureFps() {
clock_gettime(CLOCK_REALTIME, &t1);
uint64_t deltaTime = (t1.tv_sec - t0.tv_sec) * 1000000 + (t1.tv_nsec - t0.tv_nsec) / 1000 / 1000;
counter++;
if (deltaTime > 1000) {
fps = counter;
frame_rate.store(fps);
printf("\033[1;32;40mFPS:%d\033[0m\n", fps);
cout << "DEGREE: " << compass_degree.load() << endl;
counter = 0;
t0 = t1;
}
}
void stop() {
cam.StopAcquisition();
cam.Close();
printf("Done\n");
exit(0);
}
int debugCounter = 0;
void debugImage(Mat *result) {
if (debugCounter > showFpsCount+1) {
char fpsStr[32];
sprintf(fpsStr, "%10d", fps);
rectangle(mat, Point2f(0, 0), Point2f(200, 40), Scalar(255, 255, 255, 255), CV_FILLED);
putText(mat, fpsStr, Point2f(0, 30), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,0,255));
imshow("input", resultMat);
if (result != NULL)
imshow("result", *result);
debugCounter = 0;
} else
debugCounter++;
if (cv::waitKey(1) == 27) {stop();}
measureFps();
}
void createImageWindows() {
if (imageWindowsOn == 0) {
namedWindow("input", WINDOW_NORMAL);
namedWindow("result", WINDOW_NORMAL);
resizeWindow("input", 1000, 400);
resizeWindow("result", 1000, 400);
moveWindow("input", 0, 0);
moveWindow("result", 0, 475);
imageWindowsOn = 1;
}
}
void destroyImageWindows() {
if (imageWindowsOn == 1) {
destroyWindow("input");
destroyWindow("result");
}
imageWindowsOn = 0;
}
void save_values() {
fstream camera_values;
string color = attack_blue_goal ? "blue" : "yellow";
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + color, ios::out | ios::trunc);
camera_values << goalInRangeParam.minH << endl;
camera_values << goalInRangeParam.maxH << endl;
camera_values << goalInRangeParam.minS << endl;
camera_values << goalInRangeParam.maxS << endl;
camera_values << goalInRangeParam.minV << endl;
camera_values << goalInRangeParam.maxV << endl;
camera_values.close();
cout << "DATA SAVED" << endl;
}
void save_ball_values() {
fstream camera_values;
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + BALL_FILE, ios::out | ios::trunc);
camera_values << ballInRangeParam.minH << endl;
camera_values << ballInRangeParam.maxH << endl;
camera_values << ballInRangeParam.minS << endl;
camera_values << ballInRangeParam.maxS << endl;
camera_values << ballInRangeParam.minV << endl;
camera_values << ballInRangeParam.maxV << endl;
camera_values.close();
cout << "BALL SAVED" << endl;
}
void save_green_values() {
fstream camera_values;
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + GREEN_FILE, ios::out | ios::trunc);
camera_values << greenInRangeParam.minH << endl;
camera_values << greenInRangeParam.maxH << endl;
camera_values << greenInRangeParam.minS << endl;
camera_values << greenInRangeParam.maxS << endl;
camera_values << greenInRangeParam.minV << endl;
camera_values << greenInRangeParam.maxV << endl;
camera_values.close();
cout << "GREEN SAVED" << endl;
}
void load_ball_values() {
string line;
fstream camera_values;
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + BALL_FILE, ios::in);
getline(camera_values, line); ballInRangeParam.minH = atoi(line.c_str());
getline(camera_values, line); ballInRangeParam.maxH = atoi(line.c_str());
getline(camera_values, line); ballInRangeParam.minS = atoi(line.c_str());
getline(camera_values, line); ballInRangeParam.maxS = atoi(line.c_str());
getline(camera_values, line); ballInRangeParam.minV = atoi(line.c_str());
getline(camera_values, line); ballInRangeParam.maxV = atoi(line.c_str());
cout << "BALL LOADED" << endl;
camera_values.close();
}
void load_green_values() {
string line;
fstream camera_values;
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + GREEN_FILE, ios::in);
getline(camera_values, line); greenInRangeParam.minH = atoi(line.c_str());
getline(camera_values, line); greenInRangeParam.maxH = atoi(line.c_str());
getline(camera_values, line); greenInRangeParam.minS = atoi(line.c_str());
getline(camera_values, line); greenInRangeParam.maxS = atoi(line.c_str());
getline(camera_values, line); greenInRangeParam.minV = atoi(line.c_str());
getline(camera_values, line); greenInRangeParam.maxV = atoi(line.c_str());
cout << "GREEN LOADED" << endl;
camera_values.close();
}
bool goal_load_setter = false;
void load_values() {
string line;
fstream camera_values;
string color = attack_blue_goal ? "blue" : "yellow";
camera_values.open("/root/robocup-2018/CAMERA_VALUES." + color, ios::in);
getline(camera_values, line); goalInRangeParam.minH = atoi(line.c_str());
getline(camera_values, line); goalInRangeParam.maxH = atoi(line.c_str());
getline(camera_values, line); goalInRangeParam.minS = atoi(line.c_str());
getline(camera_values, line); goalInRangeParam.maxS = atoi(line.c_str());
getline(camera_values, line); goalInRangeParam.minV = atoi(line.c_str());
getline(camera_values, line); goalInRangeParam.maxV = atoi(line.c_str());
if (goal_load_setter) {
setTrackbarPos("goal minH", "gui2", goalInRangeParam.minH);
setTrackbarPos("goal minS", "gui2", goalInRangeParam.minS);
setTrackbarPos("goal minV", "gui2", goalInRangeParam.minV);
setTrackbarPos("goal maxH", "gui2", goalInRangeParam.maxH);
setTrackbarPos("goal maxS", "gui2", goalInRangeParam.maxS);
setTrackbarPos("goal maxV", "gui2", goalInRangeParam.maxV);
}
goal_load_setter = true;
cout << "DATA LOAD" << endl;
camera_values.close();
}
void save_values_callback(int event, int x, int y, int flags, void* userdata) {
if (event == EVENT_LBUTTONDOWN) {
save_values();
save_ball_values();
save_green_values();
}
}
void load_values_callback(int event, int x, int y, int flags, void* userdata) {
if (event == EVENT_LBUTTONDOWN) {
load_values();
load_ball_values();
load_green_values();
}
}
void init_camera() {
createImageWindows();
namedWindow("gui", WINDOW_NORMAL);
namedWindow("gui1", WINDOW_NORMAL);
namedWindow("gui2", WINDOW_NORMAL);
namedWindow("gui3", WINDOW_NORMAL);
namedWindow("save", WINDOW_NORMAL);
namedWindow("load", WINDOW_NORMAL);
resizeWindow("gui", 200, 800);
resizeWindow("gui1", 200, 800);
resizeWindow("gui1", 200, 800);
resizeWindow("gui2", 200, 800);
resizeWindow("gui3", 200, 800);
resizeWindow("save", 100, 100);
resizeWindow("load", 100, 100);
moveWindow("gui", 850, 0);
moveWindow("gui1", 1050, 0);
moveWindow("gui2", 1250, 0);
moveWindow("gui3", 1450, 0);
moveWindow("save", 1100, 500);
moveWindow("load", 1100, 700);
setMouseCallback("save", save_values_callback);
setMouseCallback("load", load_values_callback);
printf("Opening first camera...\n");
cam.OpenFirst();
cam.SetExposureTime(EXPOSURE_TIME);
cam.SetDownsamplingType(XI_SKIPPING);
cam.SetDownsampling(cam.GetDownsampling_Maximum());
cam.SetImageDataFormat(XI_RAW8);
cam.SetNextImageTimeout_ms(CAMERA_TIMEOUT);
//cam.SetGain(12.0f);
printf("Starting acquisition...\n");
cam.StartAcquisition();
printf("First pixel value \n");
XI_IMG_FORMAT format = cam.GetImageDataFormat();
printf("XI_IMG_FORMAT=%d\n", format);
printf("Get XI_PRM_COLOR_FILTER_ARRAY\n");
XI_COLOR_FILTER_ARRAY colorFilterArray = cam.GetSensorColorFilterArray();
printf("XI_PRM_COLOR_FILTER_ARRAY=%d\n", colorFilterArray);
//hMin = 95; createTrackbar("Hmin", "gui", &hMin, 255, 0);
//sMin = 0; createTrackbar("Smin", "gui", &sMin, 255, 0);
//vMin = 0; createTrackbar("Vmin", "gui", &vMin, 255, 0);
//hMax = 110; createTrackbar("Hmax", "gui", &hMax, 255, 0);
//sMax = 255; createTrackbar("Smax", "gui", &sMax, 255, 0);
//vMax = 255; createTrackbar("Vmax", "gui", &vMax, 255, 0);
load_values();
load_ball_values();
load_green_values();
createTrackbar("ball minH", "gui1", &ballInRangeParam.minH, 255, 0);
createTrackbar("ball maxH", "gui1", &ballInRangeParam.maxH, 255, 0);
createTrackbar("ball minS", "gui1", &ballInRangeParam.minS, 255, 0);
createTrackbar("ball maxS", "gui1", &ballInRangeParam.maxS, 255, 0);
createTrackbar("ball minV", "gui1", &ballInRangeParam.minV, 255, 0);
createTrackbar("ball maxV", "gui1", &ballInRangeParam.maxV, 255, 0);
ballInRangeParam.output = ballGpuMat;
createTrackbar("goal minH", "gui2", &goalInRangeParam.minH, 255, 0);
createTrackbar("goal maxH", "gui2", &goalInRangeParam.maxH, 255, 0);
createTrackbar("goal minS", "gui2", &goalInRangeParam.minS, 255, 0);
createTrackbar("goal maxS", "gui2", &goalInRangeParam.maxS, 255, 0);
createTrackbar("goal minV", "gui2", &goalInRangeParam.minV, 255, 0);
createTrackbar("goal maxV", "gui2", &goalInRangeParam.maxV, 255, 0);
goalInRangeParam.output = goalGpuMat;
createTrackbar("green minH", "gui3", &greenInRangeParam.minH, 255, 0);
createTrackbar("green maxH", "gui3", &greenInRangeParam.maxH, 255, 0);
createTrackbar("green minS", "gui3", &greenInRangeParam.minS, 255, 0);
createTrackbar("green maxS", "gui3", &greenInRangeParam.maxS, 255, 0);
createTrackbar("green minV", "gui3", &greenInRangeParam.minV, 255, 0);
createTrackbar("green maxV", "gui3", &greenInRangeParam.maxV, 255, 0);
greenInRangeParam.output = greenGpuMat;
dilateMultiplier = 0; createTrackbar("dilate m", "gui", &dilateMultiplier, 900, update_filters);
showImage = 0; createTrackbar("show image", "gui", &showImage, 1, 0);
showFpsCount = 0; createTrackbar("show fps count", "gui", &showFpsCount, 100, 0);
goalMat = imread("/root/robocup-2018/jerry.png", -1);
ballMat = imread("/root/robocup-2018/supak.png", -1);
thread camera_thread(update_camera);
camera_thread.detach();
}
void update_filters(int, void*) {
filterArea = (dilateMultiplier+100) / 100.0f;
element = getStructuringElement(elementShape, Size(filterArea, filterArea), Point(filterArea / 2.0f, filterArea / 2.0f));
erodeFilter = cuda::createMorphologyFilter(MORPH_ERODE, CV_8U, element);
dilateFilter = cuda::createMorphologyFilter(MORPH_DILATE, CV_8U, element);
}
void write_to_livestream() {
//string pipeline = "appsrc ! videoconvert ! vp8enc ! webmmux ! shout2send ip=127.0.0.1 port=1234 password=hackme mount=/s";
string pipeline = "appsrc ! udpsink host=localhost port=9999";
VideoWriter writer(pipeline, 0, (double)25, cv::Size(1032, 772), false);
while(true) {
writer << ballDilated;
}
}
string pipeline = "appsrc ! videoconvert ! videoscale ! video/x-raw,width=500 ! clockoverlay shaded-background=true font-desc=\"Sans 38\" ! x264enc tune=\"zerolatency\" threads=1 ! tcpserversink host=0.0.0.0 port=4444";
VideoWriter writer(pipeline, 0, (double)25, cv::Size(1032, 772), false);
int calib_count = 666;
void distance_calibration(int count) {
fstream calibration_data;
int measuremets[772];
int measuredCount[772];
calibration_data.open("/root/robocup-2018/BALL_CALIB." + to_string(count), ios::out);
cout << "calibration STARTS in" << endl;
for (int i = 5; i > 0; i--) {
cout << i << endl;
usleep(1000*1000);
}
for(int i = 0; i < 15*100; i++) {
if (ball.x != -1) {
measuredCount[ball.y]++;
measuremets[ball.y] += ball.width;
}
cout << i << endl;
usleep(1000*10);
}
for (int i = 0; i < 772; i++) {
if (measuremets[i] != 0)
calibration_data << i << " " << measuremets[i] / measuredCount[i] << endl;
}
calib_count++;
calibration_data.close();
}
void start_distance_calibration_thread() {
thread calib(distance_calibration, calib_count);
calib.detach();
}
void update_camera() {
counter = 0;
clock_gettime(CLOCK_REALTIME, &t0);
GpuMat temp;
GpuMat thres;
GpuMat toInv;
update_filters(0, NULL);
//start_distance_calibration_thread();
for (;;) {
if (ext_attack_blue_goal.load() != attack_blue_goal) {
attack_blue_goal = ext_attack_blue_goal.load();
load_values();
}
mat = cam.GetNextImageOcvMat();
inGpu.upload(mat);
cv::cuda::demosaicing(inGpu, outGpu, FILTER);
cv::cuda::cvtColor(outGpu, hsvGpu, COLOR_RGB2HSV);
inRange_gpu(hsvGpu,
ballInRangeParam.minH, ballInRangeParam.minS, ballInRangeParam.minV,
ballInRangeParam.maxH, ballInRangeParam.maxS, ballInRangeParam.maxV,
ballGpuMat);
hsvGpuGoal = GpuMat(hsvGpu, Rect(0, 0, HEIGHT, GOAL_CROP_HEIGHT));
inRange_gpu(hsvGpuGoal,
goalInRangeParam.minH, goalInRangeParam.minS, goalInRangeParam.minV,
goalInRangeParam.maxH, goalInRangeParam.maxS, goalInRangeParam.maxV,
goalGpuMat);
inRange_gpu(hsvGpu,
greenInRangeParam.minH, greenInRangeParam.minS, greenInRangeParam.minV,
greenInRangeParam.maxH, greenInRangeParam.maxS, greenInRangeParam.maxV,
greenGpuMat);
erodeFilter->apply(ballGpuMat, ballErodeMat);
erodeFilter->apply(goalGpuMat, goalErodeMat);
erodeFilter->apply(greenGpuMat, greenErodeMat);
dilateFilter->apply(ballErodeMat, ballDilateMat);
dilateFilter->apply(goalErodeMat, goalDilateMat);
dilateFilter->apply(greenErodeMat, greenDilateMat);
if (ext_livestream) {
ballDilateMat.download(ballDilated);
writer << ballDilated;
}
goal = goal_detect(goalDilateMat, MIN_GOAL_WIDTH, MIN_GOAL_HEIGHT, GOAL_MAX_PIXEL);
ball //= goal_detect(ballDilateMat, MIN_BALL_WIDTH, MIN_BALL_HEIGHT, BALL_MAX_PIXEL);
= object_detect(ballDilateMat, MIN_BALL_WIDTH, MIN_BALL_HEIGHT, BALL_MAX_PIXEL);
if (showImage)
outGpu.download(resultMat); // DEBUG
if (goal.x == -1) goal_visible.store(false);
else goal_visible.store(true);
if (ball.x == -1) ball_visible.store(false);
else ball_visible.store(true);
if(goal.x != -1) {
if (ball.x > goal.x - goal.width / 2 + KICK_TOLERANCE && ball.x < goal.x + goal.width / 2 - KICK_TOLERANCE) {
ext_i_see_goal_to_kick = true;
} else {
ext_i_see_goal_to_kick = false;
}
} else {
ext_i_see_goal_to_kick = false;
}
ball_x.store(ball.x);
ball_y.store(ball.y);
ext_ball_zone.store(ball_close(ball.x, ball.y));
goal_x.store(goal.x);
goal_y.store(goal.y);
goal_height.store(goal.height);
goal_width.store(goal.width);
//cout << ball.width << endl;
if (showImage) {
if (ext_i_see_goal_to_kick) {
putText(resultMat, "KICK", Point2f(10, 25), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255,255));
} else {
putText(resultMat, "NO KICK", Point2f(10, 25), FONT_HERSHEY_PLAIN, 2, Scalar(0,255,0,255));
}
rectangle(resultMat, Point((HEIGHT/2)-KICK_X_DIFFERENCE, 365),
Point((HEIGHT/2)+KICK_X_DIFFERENCE, 500),
Scalar(0, 255, 255), 2, 8);
}
if ((ball.x > (HEIGHT/2)-KICK_X_DIFFERENCE && ball.x < (HEIGHT/2)+KICK_X_DIFFERENCE)
&& (ball.y > BALL_Y_MIN_KICK && ball.y < BALL_Y_MAX_KICK)) {
ball_close_kick = true;
} else {
ball_close_kick = false;
}
if (showImage) {
if (goal.x != -1 && goal.height > 0) {
rectangle(resultMat, Point(0, goal.y), Point(1032, goal.y), Scalar(0, 0, 255), 2, 8);
rectangle(resultMat, Point(goal.x, 0), Point(goal.x, 772), Scalar(0, 0, 255), 2, 8);
rectangle(resultMat, Point(goal.x - goal.width / 2 + KICK_TOLERANCE, 0),
Point(goal.x - goal.width / 2 + KICK_TOLERANCE, 772),
Scalar(0, 255, 0), 2, 8);
rectangle(resultMat, Point(goal.x + goal.width / 2 - KICK_TOLERANCE, 0),
Point(goal.x + goal.width / 2 - KICK_TOLERANCE, 772),
Scalar(0, 255, 0), 2, 8);
resize(goalMat,goalMarker,Size(goal.height*100/86,goal.height));
overlayImage(resultMat, goalMarker, resultMat, cv::Point(goal.x - goal.height*100/86/2, goal.y - goal.height/2));
//goalMarker.copyTo(resultMat(cv::Rect(goal.x - goalMarker.rows/2, goal.y - goalMarker.cols/2,
// goalMarker.cols, goalMarker.rows)));
}
if (ball.x != -1 && ball.width > 0 && ball.height > 0) {
//rectangle(resultMat, Point(ball.x-ball.width/2, ball.y-ball.height/2), Point(ball.x+ball.width/2, ball.y+ball.height/2), Scalar(0, 255, 0), 2, 8);
rectangle(resultMat, Point(0, ball.y), Point(1032, ball.y), Scalar(255, 0, 0), 2, 8);
rectangle(resultMat, Point(ball.x, 0), Point(ball.x, 772), Scalar(255, 0, 0), 2, 8);
resize(ballMat,ballMarker,Size(ball.width,ball.height));
overlayImage(resultMat, ballMarker, resultMat, cv::Point(ball.x - ball.width/2, ball.y - ball.height/2));
//ballMarker.copyTo(resultMat(cv::Rect(ball.x - ballMarker.rows/2, ball.y - ballMarker.cols/2,
// ballMarker.cols, ballMarker.rows)));
}
/*for(int j = 0; j < p_balls.size(); j++){
rectangle(resultMat, Point(p_balls[j].x, p_balls[j].y), Point(p_balls[j].x+p_balls[j].width, p_balls[j].y+p_balls[j].height), Scalar(0, 255, 0), 2, 8);
}*/
//cout << "Goal height: " << goal.height << " goal width: " << goal.width << "\n";
}
if(CALIBRATION && showImage) {
outGpu.download(mat);
goalDilateMat.download(goalDilated);
ballDilateMat.download(ballDilated);
greenDilateMat.download(greenDilated);
auto channels = std::vector<cv::Mat>{Mat(WIDTH, HEIGHT, CV_8UC1, Scalar(0)), goalDilated, ballDilated, greenDilated};
cv::merge(channels, debugMerge);
createImageWindows();
//debugImage(&greenDilated);
debugImage(&debugMerge);
continue;
}
// Utility
if (showImage) {
createImageWindows();
outGpu.download(mat);
debugImage(&resultMat);
} else {
measureFps();
destroyImageWindows();
if (cv::waitKey(1) == 27) {stop();}
}
}
stop();
}