This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheCarCV.cpp
387 lines (323 loc) · 11.1 KB
/
TheCarCV.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
//
// Created by rege on 08.03.17.
//
#include <plog/Log.h>
#include <opencv2/opencv.hpp>
#include "TheCarCV.hpp"
#include "Config.hpp"
#include "Detect.hpp"
#include "speedtest.h"
using namespace std;
using namespace experimental::filesystem;
using namespace cv;
TheCarCV::TheCarCV ()
{}
void TheCarCV::setUpWindows ()
{
/// creating windows
namedWindow("original", CV_WINDOW_AUTOSIZE);
if (showExtra)
{
namedWindow("color filter", CV_WINDOW_AUTOSIZE);
/// set up "control" window
// blue
createTrackbar("blue LowH", "color filter", &blueILowH, 179); //Hue (0 - 179)
createTrackbar("blue HighH", "color filter", &blueIHighH, 179);
createTrackbar("blue LowS", "color filter", &blueILowS, 255); //Saturation (0 - 255)
createTrackbar("blue HighS", "color filter", &blueIHighS, 255);
createTrackbar("blue LowV", "color filter", &blueILowV, 255); //Value (0 - 255)
createTrackbar("blue HighV", "color filter", &blueIHighV, 255);
int __fakeDp;
createTrackbar("dp (x0.1)", "color filter", &__fakeDp, 50, [] (int n, void * data)
{
double * dp = (double *) data;
*dp = ((double) n) / 10;
if (*dp == 0) *dp = 0.1;
}, (void *) &hDp);
createTrackbar("minDist", "color filter", &hMinDist, 200);
createTrackbar("param1", "color filter", &hParam1, 1000);
createTrackbar("param2", "color filter", &hParam2, 1000);
createTrackbar("minRadius", "color filter", &hMinRadius, 200);
createTrackbar("maxRadius", "color filter", &hMaxRadius, 200);
createTrackbar("threshold", "edges", &edgeThreshold, 100);
}
}
void TheCarCV::init ()
{
LOGD << "TheCarCV init…";
string mode = getString("mode");
if (mode == "quiet_detection")
{
this->mode = DETECTION;
this->show = false;
} else if (mode == "learning")
{
this->mode = LEARNING;
this->show = false;
} else if (mode == "detection")
{
this->mode = DETECTION;
this->show = true;
} else if (mode == "collect")
{
this->mode = COLLECT;
this->show = true;
} else if (mode == "quiet_collect")
{
this->mode = COLLECT;
this->show = false;
} else if (mode == "video")
{
this->mode = VIDEO;
this->show = true;
} else
{
LOGF << "You gave me mode \"" + mode + "\". Please check it in config.xml because\
I don't know what this mode means me to do. I'll just terminate.";
exit(20);
}
LOGI << "I'm working in mode \"" + mode + "\"";
showExtra = getBool("gui/show_extra_windows");
switch (this->mode)
{
case DETECTION:
Detect::getInstance().init();
Detect::getInstance().loadData();
break;
case COLLECT:
Detect::getInstance().init();
Detect::getInstance().createDirectories();
break;
case LEARNING:
Detect::getInstance().init();
break;
}
if (this->mode != LEARNING)
{
// All this initialization doesn't need if the application starts for learning
/// video input init
LOGD << "try to open a webcam";
int videoInputSourceNumber = getInt("video/video_input_source_number");
try
{
cap = new VideoCapture(videoInputSourceNumber); //capture the video from web cam
}
catch (exception e)
{
LOGF << "cannot open webcam " + to_string(videoInputSourceNumber);
throw (e);
}
LOGI << "video input source " + to_string(videoInputSourceNumber) + " has set";
if (!cap->isOpened()) // if not success, exit program with error
{
LOGE << "Cannot open the web cam";
__throw_system_error(2);
} else LOGI << "video input source " + to_string(videoInputSourceNumber) + " has opened seccesfully";
/// load some data from the config
blueILowH = getInt("hsv_filtering/blue/low_h");
blueIHighH = getInt("hsv_filtering/blue/high_h");
blueILowS = getInt("hsv_filtering/blue/low_s");
blueIHighS = getInt("hsv_filtering/blue/high_s");
blueILowV = getInt("hsv_filtering/blue/low_v");
blueIHighV = getInt("hsv_filtering/blue/high_v");
hDp = getDouble("hough_circle_transform_parametrs/dp");
hMinDist = getInt("hough_circle_transform_parametrs/min_dist");
hParam1 = getInt("hough_circle_transform_parametrs/p1");
hParam2 = getInt("hough_circle_transform_parametrs/p2");
hMinRadius = getInt("hough_circle_transform_parametrs/min_radius");
hMaxRadius = getInt("hough_circle_transform_parametrs/max_radius");
if (show) setUpWindows();
}
}
void reinforceBlueUsingRed (const Mat3b & src, Mat3b & dst)
{
typedef Point3_<unsigned char> Pixel;
Mat3b result(src.rows, src.cols);
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
Pixel p = src.at<Pixel>(y, x);
p.x = max(p.x, p.z);
result.at<Pixel>(y, x) = p;
}
}
dst = result;
}
void TheCarCV::start ()
{
if (mode == LEARNING)
{
SPEEDTEST_("learning", Detect::getInstance().train());
LOGI << "Learning ends. I have to terminate the program.";
return;
}
for (;;)
{
Mat frame;
static int cannotFetAFrame = 0;
if (!cap->read(frame))
{
cannotFetAFrame++;
// if cannotGetAFrame is too big then it's a serious error, let big is 10
if (cannotFetAFrame == 10)
{
LOGF << "Cannot get a frame from the web cam";
__throw_system_error(2);
}
}
cannotFetAFrame = 0;
if (waitKey(10) == 27)
{
break;
}
if (processFrame(frame))
{
break;
}
}
LOGI << "TheCarCV terminated";
}
void TheCarCV::hsvFilter (
Mat & imgHSV, Mat & dsc,
int lh, int hh,
int ls, int hs,
int lv, int hv
)
{
inRange(imgHSV, Scalar(lh, ls, lv), Scalar(hh, hs, hv), dsc); //Threshold the image
}
void TheCarCV::openCVHughCircleTransform (Mat & gray, vector<Vec3f> & circles)
{
assert(gray.channels() == 1);
Mat smoothed;
// smooth it, otherwise a lot of false circles may be detected
GaussianBlur(gray, smoothed, Size(9, 9), 2, 2);
HoughCircles(smoothed, circles, HOUGH_GRADIENT,
hDp, hMinDist, hParam1, hParam2, hMinRadius, hMaxRadius);
}
// if success then returns true else returns false
bool TheCarCV::cutSquareRegionByCircle (Mat & src, Mat & dsc, int x, int y, int radius)
{
assert(radius > 0);
int x0 = x - radius;
int y0 = y - radius;
// doesn't circle go out of image
if (x0 < 0 || y0 < 0)
return false;
if (x + radius > src.cols || y + radius > src.rows)
return false;
dsc = Mat(src, Rect_<int>(x0, y0, 2 * radius, 2 * radius));
return true;
}
double TheCarCV::getNonzerosToZerosRatio (const cv::Mat & monochromeImg)
{
assert(monochromeImg.type() == CV_8UC1);
int countOfZero = 0, countOfNonZero = 0;
for (uint8_t pixel : Mat_<uint8_t>(monochromeImg))
{
if (pixel) countOfNonZero++;
else countOfZero++;
}
return (double) countOfNonZero / (double) countOfZero;
}
void TheCarCV::saveAnImage (const Mat1b & preparedImage)
{
assert(preparedImage.rows != 0 || preparedImage.cols != 0);
Mat1b toSave;
preparedImage.copyTo(toSave);
for (uchar & p : Mat1b(toSave))
{
p = p ? (uchar) 255 : (uchar) 0;
}
// the baseDirectory with will be used to save test images
static string outputDirectory = getString("files/output_directory");
// prepare OS
// experimental::filesystem is used here
static bool checked = false;
if (!checked)
{
if (!exists(path(outputDirectory)))
{
create_directory(path(outputDirectory));
}
checked = true;
}
static int a = 0;
string filepath = outputDirectory + "/" + to_string(++a) + ".png";
LOGI << "saving " + filepath;
imwrite(filepath, toSave);
}
void TheCarCV::uniteRedAndBlue (const Mat1b & red, const Mat1b & blue, Mat3b & dst)
{
assert(red.rows != 0 && red.cols != 0 && blue.rows != 0 && blue.cols != 0);
assert(red.rows == blue.rows && red.cols == blue.cols);
Mat redAndBlueOnly(red.size(), CV_8UC3);
int red_from_to[] = {0, 2};
mixChannels(red, redAndBlueOnly, red_from_to, 1);
int blue_from_to[] = {0, 0, -1, 1};
mixChannels(blue, redAndBlueOnly, blue_from_to, 2);
dst = redAndBlueOnly;
}
bool TheCarCV::processFrame (const Mat3b & frame)
{
Mat3b reinforcedBlue;
reinforceBlueUsingRed(frame, reinforcedBlue);
Mat gray;
cvtColor(reinforcedBlue, gray, CV_BGR2GRAY);
Mat imgHSV;
cvtColor(reinforcedBlue, imgHSV, COLOR_BGR2HSV);
Mat blue;
hsvFilter(imgHSV, blue, blueILowH, blueIHighH, blueILowS, blueIHighS, blueILowV, blueIHighV);
if (show && showExtra) imshow("blue", blue);
vector<Vec3f> circles;
openCVHughCircleTransform(gray, circles);
for (const Vec3f & c : circles)
{
const int minRadius = getInt("hough_circle_transform_parametrs/min_radius");
if (c[2] < minRadius) continue; // it's double verify for minRadius limit
Mat cut;
if (!cutSquareRegionByCircle(blue, cut, c[0], c[1], c[2])) continue;
double rationofNonzerosToZeros = getNonzerosToZerosRatio(cut);
static double minRatio = getDouble("detection/ratio_nonzeros_to_zeros");
// if in both images too small non zero pixels then there is no good shot, is isn't red or blue sign
if (rationofNonzerosToZeros < minRatio) continue;
int sign = RoadSign::NONE;
if (mode != VIDEO)
{
Mat1b preparedImage;
Detect::getInstance().prepareAnImage(cut, preparedImage);
if (mode == COLLECT)
{
saveAnImage(preparedImage);
} else if (mode == DETECTION)
{
sign = Detect::getInstance().detect(preparedImage);
LOGI << "Detected: " + RoadSign::getName(sign);
}
}
// Mark the circle was found if it contain minimal necessary value
// of blue or red color inside so that to be passed to detection
circle(
reinforcedBlue,
Point(cvRound(c[0]), cvRound(c[1])),
cvRound(c[2]),
Scalar(50, 200, 30), // ~green color
3
);
if (mode == DETECTION)
{
putText(reinforcedBlue,
"Found: " + RoadSign::getName(sign),
Point(5, 20), // coordinates
FONT_HERSHEY_COMPLEX_SMALL, // font
1.0, // scale
Scalar(0, 0, 200),
1, // thickness
CV_AA
); // BGR color
}
}
if (show) imshow("original", reinforcedBlue);
return false;
}