-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlds_au.cpp
1360 lines (1136 loc) · 48.5 KB
/
tlds_au.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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <opencv2/opencv.hpp>
#include <opencv2/plot.hpp>
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>
#include <string>
#include "opencv2/videoio.hpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/opencv_modules.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/videoio.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <ctime>
#include <time.h>
#include <cstdio>
#include <sys/stat.h>
#include <sys/time.h>
#include <math.h>
#include <iomanip>
#include <chrono>
#include <fstream>
#include <cmath>
#include <cstdlib>
extern "C" {
#include <stdio.h>
}
using namespace std;
using namespace cv;
#define VIDEO_FILE_NAME "/home/rohit/adas/car_lane_sign_detection/au_tlds/au_tlds32.mp4"
// #define VIDEO_FILE_NAME "/media/richard/BLACKVUE/20191121_153145_EF.mp4"
// #define VIDEO_FILE_NAME "/home/richard/Documents/TLDS_videos/final_test/tlds52.mp4"
// #define VIDEO_FILE_NAME "/home/richard/Documents/TLDS_videos/confusing_videos/conf9.mp4"
#define TEMPLATE_PATH "/home/rohit/adas/TrafficLight-Detection/arrow/arrow_2.jpeg"
// #define CASCADE_FILE_NAME "/home/rohit/adas/TrafficLight-Detection/cascade_88_12_24.xml"
#define CASCADE_FILE_NAME "/home/rohit/adas/TrafficLight-Detection/cascade_90_12_24_extended.xml"
#define SKIPFRAME 5
// #define DISPLAYALL 0
#define SHOWIMAGE 0
#define NUMOFTHRESHOLDFRAMES 5
#define DEBUG_FLAG 1
#define SAVE_DEBUG_TO_TEXT 0
#define DEBUG_GLOBAL_VAR 0
#define TLDS 1
#define SAVE_VIDEO 0
#define GREEN 0
#define RED 2
#define GREEN_LEFT_RED_ARROW 0
#define GREEN_RIGHT_RED_ARROW 0
#define RED_RIGHT_GREEN_ARROW 1
#define RED_LEFT_GREEN_ARROW 1
#define RED_AND_GREEN 1
#define SEG_FAIL -1
//=======================================================variable declaration=================================================
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int value1, value2, value3, value4;
int value5, value6, value7, value8;
int yel_val1 = 0, yel_val2 = 0, val3_sat = -20, val4_sat = -20;
int numberofthresholdframes;
int leftthresholdframes;
CascadeClassifier traffic_light;
Mat leftSideROI, rightSideROI, middleROI, middleROI1, middleROI2 ;
vector<Vec3f> red_circles, green_circles;
Rect detected_light;
Mat templ;
int framecount = 0;
int beepcount = 0;
int thresholdcount = 0;
int left_thresholdcount = 0;
int detected_frames_count = 0;
int color_seg_fail_count = 0;
int segmentationType = -1;
int red_fail_count = 0;
int flag_alarm = 0;
bool isUsingDetector = true;
bool isStart = false;
bool isRedColor = false;
bool isGreenColor = false;
bool ifRedHappened = false;
bool ifYelHappened = false;
bool isFourPattern = false;
bool isThreePattern = false;
bool isLightDetected = false;
bool arrowPresent = false;
bool startRecording = false;
bool isLeft = false;
bool isRight = false;
bool onSides = false;
// =======middle width is 0.5
// float top_edge = 0.06;
// float ROI_height = 0.33;
// float mid_width = 0.5;
// float left_mid = 0.25;
// float side_width = 0.2;
// float left_edge = 0.05;
// float right_edge = 0.75;
// =======middle width is 0.4
// float top_edge = 0.05;
// float ROI_height = 0.33;
// float mid_width = 0.4;
// float left_mid = 0.3;
// float side_width = 0.25;
// float left_edge = 0.05;
// float right_edge = 0.7;
// =======middle width is 0.45 and middle and sides are occluded
float top_edge = 0.15;
float ROI_height = 0.33;
float mid_width = 0.45;
float left_mid = 0.275;
float side_width = 0.25;
float left_edge = 0.075;
float right_edge = 0.675;
// =======night ROI
float night_top = 0.05;
float night_top_height = 0.13;
float night_mid_top_height = 0.12;
float night_mid_height = 0.10;
float night_mid_bot_height = 0.06;
float night_bot_height = 0.04;
float night_width = 0.60;
float night_left = 0.20;
// float night_ROI_height = night_top_height + night_mid_top_height + night_mid_height + night_mid_bot_height + night_bot_height;
const int frame_width = 704;
const int frame_height = 480;
char* pYuvBuf;
char* pMemContent;
char timebuf[512];
char genbuf[512];
struct timeval tv;
struct tm stTime;
time_t lastTime = 0;
int len;
int u0_dist = 540, v0_dist = 960;
int load_dataset=0;
Mat cam_mat, distCoeffs;
VideoCapture cap;
#if SAVE_VIDEO
VideoWriter video("outcpp.mp4", VideoWriter::fourcc('F','M','P','4'), 60, Size(1920, 1080));
#endif
Mat res_image(1080, 1920, CV_8UC3, (Scalar(0, 0, 0)));
Mat alarm_stop(181, 300, CV_8UC3, (Scalar(0, 0, 0)));
void getVideoData()
{
cap.open(VIDEO_FILE_NAME);
}
void loadDetector()
{
traffic_light.load(CASCADE_FILE_NAME);
}
int getAverage(Mat& frame)
{
double res;
for (int i = 0; i < frame.rows; i++)
{
for (int j = 0; j < frame.cols; j++)
{
res += frame.at<uchar>(i, j);
}
}
int corow = frame.cols * frame.rows; // image's full pixel number
res /= corow;
return res;
}
bool compare_rect(const cv::Rect & a, const cv::Rect &b)
{
return a.width > b.width;
}
vector<Mat> framePreprocessing(Mat& input_frame)
{
vector<Mat> res;
Mat frame;
resize(input_frame, frame, Size(704, 480));
Mat imageROI_sides(frame.rows * ROI_height, frame.cols * side_width * 2, CV_8UC3, (Scalar(0, 0, 0))), imageROI_mid;
imageROI_mid = frame(Rect(frame.cols * left_mid, frame.rows * top_edge, frame.cols * mid_width, frame.rows * ROI_height));
Mat imageROI_left = frame(Rect(frame.cols * left_edge, frame.rows * top_edge, frame.cols * side_width, frame.rows * ROI_height));
Mat imageROI_right = frame(Rect(frame.cols * right_edge, frame.rows * top_edge, frame.cols * side_width, frame.rows * ROI_height));
imageROI_left.copyTo(imageROI_sides(Rect(0, 0, imageROI_left.cols, imageROI_left.rows)));
imageROI_right.copyTo(imageROI_sides(Rect(imageROI_left.cols, 0, imageROI_right.cols, imageROI_right.rows)));
#if DEBUG_FLAG
printf("isStart______: %d\n", isStart);
printf("1. Size of the Image ROI on sides: %dX%d\n\n",imageROI_sides.size().width, imageROI_sides.size().height);
printf("2. Size of the Image ROI in mid: %dX%d\n\n",imageROI_mid.size().width, imageROI_sides.size().height);
#endif
GaussianBlur(imageROI_sides, imageROI_sides, Size(3, 3), 0 );
GaussianBlur(imageROI_mid, imageROI_mid, Size(3, 3), 0 );
res.push_back(imageROI_sides);
res.push_back(imageROI_mid);
return res;
}
Mat detect_TLDS_light(Mat& imageROI)
{
Mat lightROI;
vector<Rect> light;
#if DEBUG_FLAG
printf("2. Detecting the traffic light.\n\n");
#endif
traffic_light.detectMultiScale(imageROI, light, 1.02, 3, 0|2);
#if DEBUG_FLAG
printf("3. Number of traffic light signs: %zu\n", light.size());
#endif
if(light.size() > 0)
{
sort(light.begin(), light.end(), compare_rect);
detected_light = light[0];
imageROI(Rect(detected_light.x, detected_light.y, detected_light.width, detected_light.height)).copyTo(lightROI);
#if DEBUG_FLAG
printf(" Size of the detected traffic light: %dX%d\n", detected_light.size().width, detected_light.size().height);
#endif
isLightDetected = true;
}
else
{
isLightDetected = false;
}
if (light.size() > 0){
#if DISPLAYALL
namedWindow("lightROI", WINDOW_NORMAL);
resizeWindow("lightROI", 120, 90);
imshow("lightROI", lightROI);
#endif
}
return lightROI;
}
bool isArrow(Mat& lightROI, double minMatchQuality)
{
Mat result;
Mat img = lightROI;
templ = imread(TEMPLATE_PATH);
resize(templ, templ, Size(7, 7));
rotate(templ, templ, ROTATE_90_CLOCKWISE);
rotate(templ, templ, ROTATE_90_CLOCKWISE);
/// Create windows
Mat img_display;
img.copyTo(img_display);
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
/// Do the Matching
matchTemplate(img, templ, result, 5);
/// Create Trackbar
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
matchLoc = maxLoc;
#if DEBUG_FLAG
printf("maxVal: %f\n", maxVal);
printf("minVal: %f\n", minVal);
#endif
if ((templ.cols > lightROI.cols) || (templ.rows > lightROI.rows))
return false;
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (maxVal > minMatchQuality){
Mat rect;
img_display(Rect(matchLoc.x, matchLoc.y, templ.cols, templ.rows)).copyTo(rect);
#if DISPLAYALL
namedWindow("detected", WINDOW_NORMAL);
resizeWindow("detected", Size(500, 500));
imshow("detected", rect);
namedWindow("image_window", WINDOW_AUTOSIZE);
imshow("image_window", img_display );
#endif
#if DEBUG_FLAG
printf("detected the arrow\n");
#endif
return true;
}
else
{
#if DISPLAYALL
namedWindow("image_window", WINDOW_AUTOSIZE);
imshow("image_window", img_display );
#endif
#if DEBUG_FLAG
printf("arrow detection failed!!!!!!\n");
#endif
}
return false;
}
int segmentColor(Mat& lightROI)
{
Mat red_image, green_image, lower_red_hue_range, upper_red_hue_range, saturated_red_image, saturated_green_image, yellow_hue_image;
Mat red_hue_image, green_hue_image, lower_green_hue_range, upper_green_hue_range;
// For Red light: Threshold the HSV image, keep only the red pixels
red_image = lightROI.clone();
green_image = lightROI.clone();
vector<Mat> channel;
cvtColor(green_image, saturated_green_image, COLOR_BGR2HSV);
split(saturated_green_image, channel);
channel[1] *= 1.2;
merge(channel, saturated_green_image);
#if DEBUG_FLAG
printf("6. Finding out which color is present.\n");
#endif
// Yellow color segmentation --------------------------------------------------------------------
Mat leftYel, rightYel;
cvtColor(red_image, red_image, COLOR_BGR2HSV);
inRange(red_image, Scalar(20, 150, 100), Scalar(40, 255, 255), yellow_hue_image);
GaussianBlur(yellow_hue_image, yellow_hue_image, Size(5, 5), 0, 0);
Rect leftYelRect(0, 0, yellow_hue_image.cols / 2, yellow_hue_image.rows / 2);
yellow_hue_image(Rect(leftYelRect.x, leftYelRect.y, leftYelRect.width, leftYelRect.height)).copyTo(leftYel);
Rect rightYelRect(yellow_hue_image.cols / 2, 0, yellow_hue_image.cols / 2, yellow_hue_image.rows / 2);
yellow_hue_image(Rect(rightYelRect.x, rightYelRect.y, rightYelRect.width, rightYelRect.height)).copyTo(rightYel);
yel_val1 = getAverage(leftYel);
yel_val2 = getAverage(rightYel);
if (yel_val1 != 0 || yel_val2 != 0)
{
ifYelHappened = true;
}
#if DEBUG_FLAG
printf("Left yellow vs right yellow: %d vs %d\n", yel_val1, yel_val2);
#endif
// Red color segmentation --------------------------------------------------------------------
Mat topLeftRed, topRightRed, botLeftRed, botRightRed;
inRange(red_image, Scalar(0, 100, 100), Scalar(10, 255, 255), lower_red_hue_range);
inRange(red_image, Scalar(170, 100, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect topLeftRedRect(0, 0, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(topLeftRedRect.x, topLeftRedRect.y, topLeftRedRect.width, topLeftRedRect.height)).copyTo(topLeftRed);
inRange(red_image, Scalar(0, 100, 100), Scalar(10, 255, 255), lower_red_hue_range);
inRange(red_image, Scalar(170, 100, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect topRightRedRect(red_hue_image.cols / 2, 0, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(topRightRedRect.x, topRightRedRect.y, topRightRedRect.width, topRightRedRect.height)).copyTo(topRightRed);
inRange(red_image, Scalar(0, 100, 100), Scalar(10, 255, 255), lower_red_hue_range);
inRange(red_image, Scalar(170, 100, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect botLeftRedRect(0, red_hue_image.rows / 2, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(botLeftRedRect.x, botLeftRedRect.y, botLeftRedRect.width, botLeftRedRect.height)).copyTo(botLeftRed);
GaussianBlur(botLeftRed, botLeftRed, Size(3, 3), 0 );
inRange(red_image, Scalar(0, 100, 100), Scalar(10, 255, 255), lower_red_hue_range);
inRange(red_image, Scalar(170, 100, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect botRightRedRect(red_hue_image.cols / 2, red_hue_image.rows / 2, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(botRightRedRect.x, botRightRedRect.y, botRightRedRect.width, botRightRedRect.height)).copyTo(botRightRed);
GaussianBlur(botRightRed, botRightRed, Size(3, 3), 0 );
value1 = getAverage(topLeftRed);
value2 = getAverage(topRightRed);
value3 = getAverage(botLeftRed);
value4 = getAverage(botRightRed);
#if DISPLAYALL
namedWindow("Red Combined threshold images", WINDOW_NORMAL);
resizeWindow("Red Combined threshold images", 120, 90);
imshow("Red Combined threshold images", red_hue_image);
#endif
if (value1 + value2 < 6 && red_fail_count < 3)
{
red_fail_count++;
}
/*else if ((value1 + value2 < 6 && red_fail_count == 3 && isGreenColor == false) || segmentationType == 1)
{
saturated_red_image = red_image.clone();
split(saturated_red_image, channel);
channel[1] *= 1.2;
merge(channel, saturated_red_image);
#if DEBUG_FLAG
printf("=X=X=X=X=X=X=X=X=X=X=X=X=X=X=X=X=X===UPDATED COLOR RANGE\n");
#endif
inRange(saturated_red_image, Scalar(0, 87, 100), Scalar(25, 255, 255), lower_red_hue_range);
inRange(saturated_red_image, Scalar(155, 87, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect topLeftRedRect(0, 0, red_hue_image.cols, red_hue_image.rows / 2);
red_hue_image(Rect(topLeftRedRect.x, topLeftRedRect.y, topLeftRedRect.width, topLeftRedRect.height)).copyTo(topLeftRed);
inRange(saturated_red_image, Scalar(0, 87, 100), Scalar(25, 255, 255), lower_red_hue_range);
inRange(saturated_red_image, Scalar(155, 87, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect topRightRedRect(0, 0, red_hue_image.cols, red_hue_image.rows / 2);
red_hue_image(Rect(topRightRedRect.x, topRightRedRect.y, topRightRedRect.width, topRightRedRect.height)).copyTo(topRightRed);
inRange(saturated_red_image, Scalar(0, 87, 100), Scalar(25, 255, 255), lower_red_hue_range);
inRange(saturated_red_image, Scalar(155, 87, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect botLeftRedRect(0, red_hue_image.rows / 2, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(botLeftRedRect.x, botLeftRedRect.y, botLeftRedRect.width, botLeftRedRect.height)).copyTo(botLeftRed);
inRange(saturated_red_image, Scalar(0, 87, 100), Scalar(25, 255, 255), lower_red_hue_range);
inRange(saturated_red_image, Scalar(155, 87, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
Rect botRightRedRect(red_hue_image.cols / 2, red_hue_image.rows / 2, red_hue_image.cols / 2, red_hue_image.rows / 2);
red_hue_image(Rect(botRightRedRect.x, botRightRedRect.y, botRightRedRect.width, botRightRedRect.height)).copyTo(botRightRed);
value1 = getAverage(topLeftRed);
value2 = getAverage(topRightRed);
val3_sat = getAverage(botLeftRed);
val4_sat = getAverage(botRightRed);
}*/
else if (red_fail_count > 0)
{
red_fail_count--;
}
#if DEBUG_FLAG
printf("Saturated Red image botleft vs botright: %d vs %d\n", val3_sat, val4_sat);
#endif
#if DEBUG_FLAG
printf("Red image top left vs top right vs botleft vs botright: %d vs %d vs %d vs %d\n", value1, value2, value3, value4);
#endif
// Red color segmentation --------------------------------------------------------------------
vector<Mat> arr_to_merge;
arr_to_merge.push_back(red_hue_image);
arr_to_merge.push_back(red_hue_image);
arr_to_merge.push_back(red_hue_image);
Mat color_red_hue_image, red_rect(350, 220, CV_8UC3, (Scalar(0, 0, 255)));
merge(arr_to_merge, color_red_hue_image);
resize(color_red_hue_image, color_red_hue_image, Size(170, 300));
red_rect.copyTo(res_image(Rect(995, 475, red_rect.cols, red_rect.rows)));
color_red_hue_image.copyTo(res_image(Rect(1020, 500, color_red_hue_image.cols, color_red_hue_image.rows)));
line(res_image, cvPoint(995, 475 + int(red_rect.rows / 2)), cvPoint(995 + red_rect.cols, 475 + int(red_rect.rows / 2)), Scalar(255, 255, 255), 2);
arr_to_merge.clear();
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Green color segmentation --------------------------------------------------------------------
Mat topRightGreen, topLeftGreen, botLeftGreen, botRightGreen;
cvtColor(green_image, green_image, COLOR_BGR2HSV);
inRange(green_image, Scalar(50, 120, 100), Scalar(80, 255, 255), green_hue_image);
GaussianBlur(green_hue_image, green_hue_image, Size(5, 5), 0, 0);
Rect topLeftGreenRect(0, 0, green_hue_image.cols / 2, red_hue_image.rows / 2);
green_hue_image(Rect(topLeftGreenRect.x, topLeftGreenRect.y, topLeftGreenRect.width, topLeftGreenRect.height)).copyTo(topLeftGreen);
GaussianBlur(topLeftGreen, topLeftGreen, Size(3, 3), 0, 0);
Rect topRightGreenRect(green_hue_image.cols / 2, 0, green_hue_image.cols / 2, red_hue_image.rows / 2);
green_hue_image(Rect(topRightGreenRect.x, topRightGreenRect.y, topRightGreenRect.width, topRightGreenRect.height)).copyTo(topRightGreen);
GaussianBlur(topRightGreen, topRightGreen, Size(3, 3), 0, 0);
// inRange(saturated_green_image, Scalar(45, 80, 90), Scalar(90, 255, 255), green_hue_image);
// GaussianBlur(green_hue_image, green_hue_image, Size(5, 5), 0, 0);
Rect botLeftGreenRect(0, green_hue_image.rows / 2, green_hue_image.cols / 2, red_hue_image.rows / 2);
green_hue_image(Rect(botLeftGreenRect.x, botLeftGreenRect.y, botLeftGreenRect.width, botLeftGreenRect.height)).copyTo(botLeftGreen);
Rect botRightGreenRect(green_hue_image.cols / 2, green_hue_image.rows / 2, green_hue_image.cols / 2, red_hue_image.rows / 2);
green_hue_image(Rect(botRightGreenRect.x, botRightGreenRect.y, botRightGreenRect.width, botRightGreenRect.height)).copyTo(botRightGreen);
value5 = getAverage(topLeftGreen);
value6 = getAverage(topRightGreen);
value7 = getAverage(botLeftGreen);
value8 = getAverage(botRightGreen);
#if DEBUG_FLAG
printf("Green image top left vs top right vs botleft vs botright: %d vs %d vs %d vs %d\n", value5, value6, value7, value8);
#endif
#if DISPLAYALL
namedWindow("Green Combined threshold images", WINDOW_NORMAL);
resizeWindow("Green Combined threshold images", 120, 90);
imshow("Green Combined threshold images", green_hue_image);
#endif
// Green color segmentation --------------------------------------------------------------------
arr_to_merge.push_back(green_hue_image);
arr_to_merge.push_back(green_hue_image);
arr_to_merge.push_back(green_hue_image);
Mat color_green_hue_image, green_rect(350, 220, CV_8UC3, (Scalar(0, 255, 0)));
merge(arr_to_merge, color_green_hue_image);
resize(color_green_hue_image, color_green_hue_image, Size(170, 300));
green_rect.copyTo(res_image(Rect(1295, 475, green_rect.cols, green_rect.rows)));
color_green_hue_image.copyTo(res_image(Rect(1320, 500, color_green_hue_image.cols, color_green_hue_image.rows)));
line(res_image, cvPoint(1295, 475 + int(green_rect.rows / 2)), cvPoint(1295 + green_rect.cols, 475 + int(green_rect.rows / 2)), Scalar(255, 255, 255), 2);
arr_to_merge.clear();
if ((value7 + value8 < 12) && ((value1 + value2 < val3_sat + val4_sat + 20 && value1 + value2 > val3_sat + val4_sat) || (val3_sat + val4_sat < value1 + value2 + 20 && val3_sat + val4_sat > value1 + value2)))
{
#if DEBUG_FLAG
printf("first SEG_FAIL\n");
#endif
return SEG_FAIL;
}
if (value7 + value8 < 12 && value1 + value2 > 12 && value3 + value4 < 12) // && value1 > 0 && value2 > 0) // red color
{
if ((isLeft == true || isRight == true) && (value1 == 0 || value2 == 0))
{
#if DEBUG_FLAG
printf("left vs right: %d vs %d\n", isLeft, isRight);
printf("isLeft || isRight RED\n");
#endif
return RED;
}
if (yel_val1 > yel_val2 || (value1 > value2 && yel_val1 == 0 && yel_val2 == 0 && ifYelHappened == false))
{
isLeft = true;
isRight = false;
}
else if (yel_val2 > yel_val1 || (value2 > value1 && yel_val1 == 0 && yel_val2 == 0 && ifYelHappened == false))
{
isRight = true;
isLeft = false;
}
#if DEBUG_FLAG
printf("yel_val1 || yel_val2 RED\n");
printf("left vs right: %d vs %d\n", isLeft, isRight);
#endif
return RED;
}
if (value1 + value2 > 12 && value7 + value8 > 12 && isLeft && value7 > value8) // green color and right red arrow
{
#if DEBUG_FLAG
printf("GREEN_RIGHT_RED_ARROW\n");
#endif
return GREEN_RIGHT_RED_ARROW;
}
else if (value7 + value8 > 12 && value1 + value2 > 12 && isRight && value7 < value8) // green color and left red arrow
{
#if DEBUG_FLAG
printf("GREEN_LEFT_RED_ARROW\n");
#endif
return GREEN_LEFT_RED_ARROW;
}
else if (value1 + value2 > 12 && value7 + value8 > 12 && isLeft && value7 <= value8) // red color and right green arrow
{
#if DEBUG_FLAG
printf("RED_RIGHT_GREEN_ARROW\n");
#endif
return RED_RIGHT_GREEN_ARROW;
}
else if (value1 + value2 > 12 && value7 + value8 > 12 && isRight && value7 >= value8) // red color and left green arrow
{
#if DEBUG_FLAG
printf("RED_LEFT_GREEN_ARROW\n");
#endif
return RED_LEFT_GREEN_ARROW;
}
else if (value1 + value2 > 12 && value7 + value8 > 12) // red color and left green arrow
{
#if DEBUG_FLAG
printf("RED_LEFT_GREEN_ARROW\n");
#endif
return RED_LEFT_GREEN_ARROW;
}
if (value1 + value2 < 12 && value7 + value8 > 12 && value5 + value6 < 12) // green color
{
#if DEBUG_FLAG
printf("GREEN\n");
#endif
return GREEN;
}
#if DEBUG_FLAG
printf("SEG_FAIL\n");
#endif
return SEG_FAIL;
}
int colorDecision(int segmentationType, bool fourPattern)
{
if (segmentationType == RED_RIGHT_GREEN_ARROW || segmentationType == RED_LEFT_GREEN_ARROW){
#if DEBUG_FLAG
printf("7. Segmented light area is on both sides: Turn left and RED LIGHT. \n\n");
#endif
isRedColor = true;
ifRedHappened = true;
}
if (segmentationType == RED)
{
#if DEBUG_FLAG
printf("7. Segmented light area is on top : RED LIGHT. \n\n");
#endif
isRedColor = true;
ifRedHappened = true;
return 0;
}
else if (segmentationType == GREEN || segmentationType == GREEN_LEFT_RED_ARROW || segmentationType == GREEN_RIGHT_RED_ARROW)
{
#if DEBUG_FLAG
printf("7. Segmented light area is bottom : GREEN LIGHT. \n\n");
#endif
isGreenColor = true;
isRedColor = false;
}
else
{
#if DEBUG_FLAG
printf("7. ThreePattern: Color segmentation failed !!!!!\n\n");
#endif
return -1;
}
if (isGreenColor == true && isRedColor == false && ifRedHappened == true)
{
thresholdcount++;
if (thresholdcount > numberofthresholdframes)
{
#if DEBUG_FLAG
printf("8. Light change occured and the alarm started.\n\n");
#endif
return 1;
}
}
return 0;
}
vector<int> segmentColor_night(Mat& imageROI, bool display) // , bool contour)
{
Mat red_image, green_image, lower_red_hue_range, upper_red_hue_range, saturated_red_image, saturated_green_image;
Mat red_hue_image, green_hue_image, lower_green_hue_range, upper_green_hue_range;
vector<Mat> arr_to_merge;
Rect bounding_rect;
// For Red light: Threshold the HSV image, keep only the red pixels
red_image = imageROI.clone();
green_image = imageROI.clone();
if (display)
{
#if DEBUG_FLAG
printf("6. Finding out which color is present.\n");
#endif
}
// vector<Mat> channel;
// saturated_red_image = red_image.clone();
// split(saturated_red_image, channel);
// channel[1] *= 1.2;
// merge(channel, saturated_red_image);
// Red color segmentation --------------------------------------------------------------------
cvtColor(red_image, red_image, COLOR_BGR2HSV);
inRange(red_image, Scalar(0, 90, 100), Scalar(10, 255, 255), lower_red_hue_range);
inRange(red_image, Scalar(150, 90, 100), Scalar(180, 255, 255), upper_red_hue_range);
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, Size(5, 5), 0, 0);
value1 = getAverage(red_hue_image);
if (display)
{
#if DEBUG_FLAG
printf("Red image value: %d\n", value1);
#endif
}
if (display)
{
arr_to_merge.push_back(red_hue_image);
arr_to_merge.push_back(red_hue_image);
arr_to_merge.push_back(red_hue_image);
Mat color_red_hue_image, red_rect(376, 450, CV_8UC3, (Scalar(0, 0, 255)));
merge(arr_to_merge, color_red_hue_image);
resize(color_red_hue_image, color_red_hue_image, Size(400, 326));
red_rect.copyTo(res_image(Rect(875, 275, red_rect.cols, red_rect.rows)));
color_red_hue_image.copyTo(res_image(Rect(900, 300, color_red_hue_image.cols, color_red_hue_image.rows)));
arr_to_merge.clear();
line(res_image, cvPoint(900 + int(color_red_hue_image.cols * 0), 300 + color_red_hue_image.rows * 0.225), cvPoint(900 + int(color_red_hue_image.cols), 300 + color_red_hue_image.rows * 0.225), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(900 + int(color_red_hue_image.cols * 0), 300 + color_red_hue_image.rows * 0.45), cvPoint(900 + int(color_red_hue_image.cols), 300 + color_red_hue_image.rows * 0.45), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(900 + int(color_red_hue_image.cols * 0), 300 + color_red_hue_image.rows * 0.65), cvPoint(900 + int(color_red_hue_image.cols), 300 + color_red_hue_image.rows * 0.65), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(900 + int(color_red_hue_image.cols * 0), 300 + color_red_hue_image.rows * 0.85), cvPoint(900 + int(color_red_hue_image.cols), 300 + color_red_hue_image.rows * 0.85), Scalar(255, 255, 255), 2);
}
// Red color segmentation --------------------------------------------------------------------
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Green color segmentation --------------------------------------------------------------------
cvtColor(green_image, green_image, COLOR_BGR2HSV);
inRange(green_image, Scalar(40, 70, 90), Scalar(90, 255, 255), green_hue_image);
GaussianBlur(green_hue_image, green_hue_image, Size(5, 5), 0, 0);
value2 = getAverage(green_hue_image);
if (display)
{
#if DEBUG_FLAG
printf("Green image value: %d \n", value2);
#endif
}
if (display)
{
arr_to_merge.push_back(green_hue_image);
arr_to_merge.push_back(green_hue_image);
arr_to_merge.push_back(green_hue_image);
Mat color_green_hue_image, green_rect(376, 450, CV_8UC3, (Scalar(0, 255, 0)));
merge(arr_to_merge, color_green_hue_image);
resize(color_green_hue_image, color_green_hue_image, Size(400, 326));
green_rect.copyTo(res_image(Rect(1325, 275, green_rect.cols, green_rect.rows)));
color_green_hue_image.copyTo(res_image(Rect(1350, 300, color_green_hue_image.cols, color_green_hue_image.rows)));
arr_to_merge.clear();
line(res_image, cvPoint(1350 + int(color_green_hue_image.cols * 0), 300 + color_green_hue_image.rows * 0.225), cvPoint(1350 + int(color_green_hue_image.cols), 300 + color_green_hue_image.rows * 0.225), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(1350 + int(color_green_hue_image.cols * 0), 300 + color_green_hue_image.rows * 0.45), cvPoint(1350 + int(color_green_hue_image.cols), 300 + color_green_hue_image.rows * 0.45), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(1350 + int(color_green_hue_image.cols * 0), 300 + color_green_hue_image.rows * 0.65), cvPoint(1350 + int(color_green_hue_image.cols), 300 + color_green_hue_image.rows * 0.65), Scalar(255, 255, 255), 2);
line(res_image, cvPoint(1350 + int(color_green_hue_image.cols * 0), 300 + color_green_hue_image.rows * 0.85), cvPoint(1350 + int(color_green_hue_image.cols), 300 + color_green_hue_image.rows * 0.85), Scalar(255, 255, 255), 2);
}
vector<int> res;
res.push_back(value1);
res.push_back(value2);
return res;
}
int segmentDecision_night(vector<int> &top, vector<int> &mid_top, vector<int> &mid, vector<int> &mid_bot, vector<int> &bot)
{
int red_sum = 0, green_sum = 0;
int red_arr[4] = {}, green_arr[4] = {};
red_arr[0] = top[0];
green_arr[0] = top[1];
red_sum += top[0];
green_sum += top[1];
#if DEBUG_FLAG
printf("Red vs green on TOP: %dx%d\n", red_arr[0], green_arr[0]);
#endif
red_arr[1] = mid_top[0];
green_arr[1] = mid_top[1];
red_sum += mid_top[0];
green_sum += mid_top[1];
#if DEBUG_FLAG
printf("Red vs green on MID_TOP: %dx%d\n", red_arr[1], green_arr[1]);
#endif
red_arr[2] = mid[0];
green_arr[2] = mid[1];
red_sum += mid[0];
green_sum += mid[1];
#if DEBUG_FLAG
printf("Red vs green on MID: %dx%d\n", red_arr[2], green_arr[2]);
#endif
red_arr[3] = mid_bot[0];
green_arr[3] = mid_bot[1];
red_sum += mid_bot[0];
green_sum += mid_bot[1];
#if DEBUG_FLAG
printf("Red vs green on MID_BOT: %dx%d\n", red_arr[3], green_arr[3]);
#endif
red_arr[4] = bot[0];
green_arr[4] = bot[1];
red_sum += bot[0];
green_sum += bot[1];
#if DEBUG_FLAG
printf("Red vs green on BOT: %dx%d\n\n", red_arr[4], green_arr[4]);
#endif
#if DEBUG_FLAG
printf("Red vs green total: %d x %d\n\n", red_sum, green_sum);
#endif
if ((red_sum >= 10) && (green_sum >= 10)){ // green and red circle
return RED_AND_GREEN;
}
if ((red_sum >= 10) && (green_sum <= 10)){ // red cirle
return RED;
}
if ((green_sum >= 10) && (red_sum <= 10)){ // green circle
return GREEN;
}
if ((red_sum == 0 && green_sum == 0)){ // none
return SEG_FAIL;
}
return SEG_FAIL;
}
int colorDecision_night(int segmentationType)
{
if (segmentationType == RED_AND_GREEN)
{
#if DEBUG_FLAG
printf("7. Segmented light area is on both sides: Turn left and RED LIGHT. \n\n");
#endif
isRedColor = true;
ifRedHappened = true;
}
else if (segmentationType == RED)
{
#if DEBUG_FLAG
printf("7. Segmented light area is on leftside : RED LIGHT. \n\n");
#endif
isRedColor = true;
ifRedHappened = true;
return 0;
}
else if (segmentationType == GREEN)
{
#if DEBUG_FLAG
printf("7. Segmented light area is rightside : GREEN LIGHT. \n\n");
#endif
isGreenColor = true;
isRedColor = false;
}
else
{
#if DEBUG_FLAG
printf("7. Color segmentation failed !!!!!\n\n");
#endif
return -1;
}
if (isGreenColor == true && isRedColor == false && ifRedHappened == true)
{
thresholdcount++;
if (thresholdcount > numberofthresholdframes)
{
#if DEBUG_FLAG
printf("8. Light change occured and the alarm started.\n\n");
#endif
return 1;
}
}
return 0;
}
void StartTLDS()
{
isStart = true;
isUsingDetector = true;
thresholdcount = 0;
left_thresholdcount = 0;
detected_frames_count = 0;
color_seg_fail_count = 0;
onSides = false;
}
void EndTLDS()
{
isStart = false;
isRedColor = false;
isGreenColor = false;
ifRedHappened = false;
ifYelHappened = false;
detected_frames_count = 0;
color_seg_fail_count = 0;
segmentationType = -1;
flag_alarm = 0;
onSides = false;
isLeft = false;
isRight = false;
}
int main()
{
namedWindow("demo", WINDOW_AUTOSIZE);
Mat tr_light, tr_stand, alarm_image;
tr_stand = imread("/home/rohit/adas/car_lane_sign_detection/au_tlds/tr_stand.png");
tr_stand.copyTo(res_image(Rect(1662, 500, tr_stand.cols, tr_stand.rows)));
tr_light = imread("/home/rohit/adas/car_lane_sign_detection/au_tlds/zdemo_traffic_light.png");
rotate(tr_light, tr_light, ROTATE_90_CLOCKWISE);
alarm_image = imread("/home/rohit/adas/car_lane_sign_detection/au_tlds/zalarm_image.png");
resize(tr_light, tr_light, Size(300, 300));
int tr_light_x = 1550, tr_light_y = 300;
tr_light.copyTo(res_image(Rect(tr_light_x, tr_light_y, tr_light.cols, tr_light.rows)));
circle(res_image, cvPoint(tr_light_x + 150, tr_light_y + 60), 39, Scalar(30, 30, 30), FILLED);
circle(res_image, cvPoint(tr_light_x + 150, tr_light_y + 150), 39, Scalar(30, 30, 30), FILLED);
circle(res_image, cvPoint(tr_light_x + 150, tr_light_y + 240), 39, Scalar(30, 30, 30), FILLED);
#if DEBUG_FLAG
printf("2. Inside the tlds function.\n\n");
#endif
#if SAVE_DEBUG_TO_TEXT
freopen("/app/sd/Blackvue/Record/debug_tlds.txt","a",stdout);
#endif
#if DEBUG_FLAG
printf("3. GPS SPEED and MAX SPEED satisfy the conditions.\n\n");
printf("TLDS starts.\n\n");
#endif
Mat imageROI_sides, imageROI_mid, lightROI_sides, lightROI_mid;
vector<Mat> preproc_ROI;
vector<int> whole_image, top, middle_top, middle, middle_bot, bot;
bool fourPattern;
loadDetector();
#if DEBUG_FLAG
printf("Finish loading dataset. \n");
#endif
int text_count = 1;
getVideoData();