-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDetectColorSign.cpp
357 lines (306 loc) · 10.1 KB
/
DetectColorSign.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
#include "DetectColorSign.h"//
//createTrackbar("circleSize", "trackbar", &circleSize, 500, on_trackbar);
//namedWindow("trackbar", WINDOW_NORMAL);
//circle(distortedFrame, Point(imageSize.width * 0.5, imageSize.height * 0.3), circleSize, Scalar(0, 0, 255), -1, LINE_AA);
int DCS_HLP_threshold = 75;
int DCS_HLP_minLineLength = 15;
int DCS_HLP_maxLineGap = 5;
int DCS_HLP_threshold_2 = 30;
int DCS_HLP_minLineLength_2 = 15;
int DCS_HLP_maxLineGap_2 = 5;
int H_minDist = 20;
int H_param1 = 50;
int H_param2 = 35;
static void on_trackbar(int, void*)
{
}
DetectColorSign::DetectColorSign()
{
for (int i = 0; i < 10; i++)
{
pre_brightness[i] = 0;
}
ready = false;
print = true;
waiting = true;
startCount = 0;
lower_red1 = Scalar(0, 100, 150);
upper_red1 = Scalar(12, 255, 255);
lower_red2 = Scalar(168, 100, 150);
upper_red2 = Scalar(180, 255, 255);
lower_yellow = Scalar(18, 100, 150);
upper_yellow = Scalar(42, 255, 255);
lower_green = Scalar(43, 50, 130);
upper_green = Scalar(77, 255, 255);
}
DetectColorSign::DetectColorSign(bool onPrint)
{
*(this) = DetectColorSign();
print = onPrint;
}
bool DetectColorSign::detectTunnel(Mat& frame, double percent)
{
bool returnVal;
Mat grayFrame;
cvtColor(frame, grayFrame, COLOR_RGB2GRAY);
int pixelCnt(0);
int pixelValue(0);
for (int i = 0; i < grayFrame.cols; i += 10) // 10픽셀마다 하나씩 검사함 속도를 위해
{
for (int j = 0; j < grayFrame.rows; j += 10)
{
pixelValue += grayFrame.at<uchar>(j, i);
pixelCnt++;
}
}
int totalValue = pixelCnt * 255;
double brightRate = ((double)pixelValue / totalValue) * 100.0;
if (brightRate < (100 - percent))
{
putText(grayFrame, "detect tunnel!", Point(frame.cols / 4, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255), 3);
returnVal = true;
}
else returnVal = false;
if (print)
{
putText(grayFrame, "darkRate : " + to_string(100 - brightRate) + '%', Point(30, 30), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0), 2);
namedWindow("grayFrame", WINDOW_NORMAL);
imshow("grayFrame", grayFrame);
resizeWindow("grayFrame", 320, 240);
moveWindow("grayFrame", 0, 40);
}
return returnVal;
}
bool DetectColorSign::waitingCheck(Mat& frame, double difference)
{
if (!waiting)
return false;
double sum(0), average(0);
if (ready)
{
for (int i = 5; i < 10; i++)
{
sum += pre_brightness[0];
}
average = sum / 5;
}
else if (pre_brightness[9] > 0)
ready = true;
Mat grayFrame;
cvtColor(frame, grayFrame, COLOR_RGB2GRAY);
int pixelCnt(0);
int pixelValue(0);
for (int i = 0; i < grayFrame.cols; i += 10) // 10픽셀마다 하나씩 검사함 속도를 위해
{
for (int j = 0; j < grayFrame.rows; j += 10)
{
pixelValue += grayFrame.at<uchar>(j, i);
pixelCnt++;
}
}
int totalValue = pixelCnt * 255;
double brightRate = ((double)pixelValue / totalValue) * 100.0;
if (ready && (brightRate - average > difference || average - brightRate > difference))
{
startCount++;
if (startCount >= 4)
{
waiting = false;
return false;
}
}
else
{
pre_brightness[0] = brightRate;
for (int i = 0; i < 9; i++)
{
pre_brightness[i + 1] = pre_brightness[i];
}
}
putText(frame, "waiting~~", Point(frame.cols / 4, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 153, 0), 2);
return true;
}
bool DetectColorSign::priorityStop(Mat& frame, double percent)
{
bool returnVal;
if (isRedStop(frame, percent, false))
{
//클래스멤버로 저장되어있는 frame_red 활용.
Mat frame_red = store_red;
Mat frame_edge;
Canny(frame_red, frame_edge, 118, 242); //빨간색만 남은 frame의 윤곽을 1채널 Mat객체로 추출
vector<Vec4i> lines; //검출될 직선이 저장될 객체
HoughLinesP(frame_edge, lines, 1, CV_PI / 180, DCS_HLP_threshold, DCS_HLP_minLineLength, DCS_HLP_maxLineGap);
if (lines.size() >= 2)
{
putText(frame_red, "Line Count : " + to_string(lines.size()), Point(30, 60), FONT_HERSHEY_COMPLEX, 1, Scalar(255), 2);
putText(frame_red, "[Priority STOP!]", Point(frame.cols / 12, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255), 3);
putText(frame, "[Priority STOP!]", Point(frame.cols / 12, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 123, 0), 3);
returnVal = true;
}
else returnVal = false;
if (print)
{
putText(frame_red, "red Pixel : " + to_string(m_redRatio) + '%', Point(30, 30), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0), 2);
namedWindow("frame_red", WINDOW_NORMAL);
imshow("frame_red", frame_red);
resizeWindow("frame_red", 320, 240);
moveWindow("frame_red", 0, 40);
}
}
else returnVal = false;
return returnVal;
}
bool DetectColorSign::isRedStop(Mat& frame, double percent, bool pFlag)
{
//Mat frame_hsv;
//Mat frame_red1;
//Mat frame_red2;
bool returnVal;
Mat frame_red; //초기화
cvtColor(frame, frame_hsv, COLOR_BGR2HSV);
inRange(frame_hsv, lower_red1, upper_red1, frame_red1);
inRange(frame_hsv, lower_red2, upper_red2, frame_red2);
frame_red = frame_red1 | frame_red2;
store_red = frame_red.clone();
int redPixel(0);
int height_limit = frame_red.rows / 2; //화면 위쪽 50% 만 검사
for (int i = 0; i < frame_red.cols; i += 10) // 10픽셀마다 하나씩 검사함 속도를 위해
{
for (int j = 0; j < height_limit; j += 10)
{
if (frame_red.at<uchar>(j, i)) redPixel++; // 붉은색이 나오는 픽셀 개수 계산
}
}
//double redRatio = ((double)redPixel / ((frame.cols) * (frame.rows))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
double redRatio = ((double)redPixel / ((frame.cols / 10) * (frame.rows / 10))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
redRatio *= 100;
m_redRatio = redRatio;
if (redRatio > percent)
{
if (pFlag == true)
{
putText(frame_red, "RED signal!", Point(frame.cols / 5, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255), 3);
putText(frame, "RED signal!", Point(frame.cols / 5, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 255, 0), 3);
}
returnVal = true;
}
else
{
returnVal = false;
}
if (print)
{
putText(frame_red, "red Pixel : " + to_string(redRatio) + '%', Point(30, 30), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0), 2);
namedWindow("frame_red", WINDOW_NORMAL);
imshow("frame_red", frame_red);
resizeWindow("frame_red", 320, 240);
moveWindow("frame_red", 0, 40);
}
return returnVal;
}
bool DetectColorSign::isYellow(Mat& frame, double percent)
{
bool returnVal;
Mat frame_yellow;
cvtColor(frame, frame_hsv, COLOR_BGR2HSV);
inRange(frame_hsv, lower_yellow, upper_yellow, frame_yellow);
int yellowPixel(0);
int height_limit = frame_yellow.rows / 2; //화면 위쪽 50% 만 검사
for (int i = 0; i < frame_yellow.cols; i += 10) // 10픽셀마다 하나씩 검사함 속도를 위해
{
for (int j = 0; j < height_limit; j += 10)
{
if (frame_yellow.at<uchar>(j, i)) yellowPixel++; // 노란색이 나오는 픽셀 개수 계산
}
}
//640 x 480 이므로 64x24 = 1536 픽셀만 검사.
//전체픽셀 640 x 480 = 307,200
//연산픽셀 64 x 48 = 3072
//double yellowRatio = ((double)yellowPixel / ((frame.cols) * (frame.rows))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
double yellowRatio = ((double)yellowPixel / ((frame.cols / 10) * (frame.rows / 10))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
yellowRatio *= 100;
if (yellowRatio > percent)
{
putText(frame_yellow, "YELLOW signal!", Point(frame.cols / 8, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255), 3);
putText(frame, "YELLOW signal!", Point(frame.cols / 8, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 0, 123), 3);
returnVal = true;
}
else
{
returnVal = false;
}
if (print)
{
putText(frame_yellow, "yellow Pixel : " + to_string(yellowRatio) + '%', Point(30, 30), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0), 2);
namedWindow("frame_yellow", WINDOW_NORMAL);
imshow("frame_yellow", frame_yellow);
resizeWindow("frame_yellow", 320, 240);
moveWindow("frame_yellow", 0 + 320, 40);
}
return returnVal;
}
int DetectColorSign::isGreenTurnSignal(Mat& frame, double percent)
{
//Mat frame_hsv;
int returnVal;
Mat frame_green;
Mat frame_edge;
cvtColor(frame, frame_hsv, COLOR_BGR2HSV);
inRange(frame_hsv, lower_green, upper_green, frame_green);
Canny(frame_green, frame_edge, 118, 242); //초록색만 남은 frame의 윤곽을 1채널 Mat객체로 추출
int greenPixel(0);
int height_limit = frame_green.rows / 2; //화면 위쪽 50% 만 검사
for (int i = 0; i < frame_green.cols; i += 10) // 10픽셀마다 하나씩 검사함 속도를 위해
{
for (int j = 0; j < height_limit; j += 10)
{
if (frame_green.at<uchar>(j, i)) greenPixel++; // 초록색이 나오는 픽셀 개수 계산
}
}
//640 x 480 이므로 64x24 = 1536 픽셀만 검사.
//double greenRatio = ((double)greenPixel / ((frame.cols) * (frame.rows))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
double greenRatio = ((double)greenPixel / ((frame.cols / 10) * (frame.rows / 10))); //검출된 픽셀수를 전체 픽셀수로 나눈 비율
greenRatio *= 100;
vector<Vec4i> lines; //검출될 직선이 저장될 객체
HoughLinesP(frame_edge, lines, 1, CV_PI / 180, DCS_HLP_threshold_2, DCS_HLP_minLineLength_2, DCS_HLP_maxLineGap_2);
if (greenRatio > percent)
{
if (lines.size() >= 3)
{
if (print)
{
putText(frame_green, "Green LEFT(<-) signal", Point(frame.cols / 10, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 0, 255), 3);
putText(frame, "Green LEFT(<-) signal", Point(frame.cols / 10, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 0, 255), 3);
}
returnVal = 1;
}
else
{
if (print)
{
putText(frame_green, "Green signal", Point(frame.cols / 6, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 0, 255), 3);
putText(frame, "Green signal", Point(frame.cols / 6, frame.rows * 0.65), FONT_HERSHEY_COMPLEX, 2, Scalar(255, 0, 255), 3);
}
returnVal = 2;
}
}
else
{
returnVal = 0;
}
if (print)
{
putText(frame_green, "green Pixel : " + to_string(greenRatio) + '%', Point(30, 30), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0), 2);
putText(frame_green, "Line Count : " + to_string(lines.size()), Point(30, 60), FONT_HERSHEY_COMPLEX, 1, Scalar(255), 2);
for (Vec4i l : lines)
{
line(frame_green, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255), 2);
}
namedWindow("frame_green", WINDOW_NORMAL);
imshow("frame_green", frame_green);
resizeWindow("frame_green", 320, 240);
moveWindow("frame_green", 0 + 320 + 320, 40);
}
return returnVal;
}