forked from AvazAlimov/self-driving-car
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LaneDetector.cpp
178 lines (149 loc) · 4.92 KB
/
LaneDetector.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
#include "LaneDetector.hpp"
#include <iostream>
using namespace cv;
using namespace std;
LaneDetector::LaneDetector(Mat image)
{
//Resize and set a given image into sourceImage;
resize(image, sourceImage, Size(320, 240));
// Set default function components for left and right lines
// y = m * x + b
leftB = sourceImage.rows;
rightB = -sourceImage.rows;
leftM = -sourceImage.rows / (double)(sourceImage.cols / 2);
rightM = sourceImage.rows / (double)(sourceImage.cols / 2);
// Detect edges of an image
Mat edges = detectEdges(sourceImage);
// Cut ROI from edges image
Mat segment = cutInterestArea(edges);
// Get lines from ROI
vector<Vec4i> lines = getLines(segment);
// Set function components from lines
buildLines(lines);
}
double LaneDetector::average(vector<double> slopes)
{
double sum = 0;
for (int i = 0; i < slopes.size(); sum += slopes[i], i++);
return sum / slopes.size();
}
Mat LaneDetector::detectEdges(Mat image)
{
Mat output;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
cvtColor(image, output, COLOR_BGR2GRAY);
GaussianBlur(output, output, Size(5, 5), 0, 0);
Canny(output, output, 100, 255);
findContours(output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
output = Mat::zeros(output.size(), output.type());
for (int i = 0; i < contours.size(); i++)
drawContours(output, contours, i, Scalar::all(255), 2, 8, hierarchy, 0, Point());
return output;
}
Mat LaneDetector::cutInterestArea(Mat image)
{
Mat output;
Point points[4] = {
Point(0, sourceImage.rows),
Point(0, sourceImage.rows * 3 / 4),
Point(sourceImage.cols, sourceImage.rows * 3 / 4),
Point(sourceImage.cols, sourceImage.rows)};
Mat mask = Mat::zeros(image.size(), image.type());
fillConvexPoly(mask, points, 4, Scalar(255, 0, 0));
bitwise_and(image, mask, output);
return output;
}
vector<Vec4i> LaneDetector::getLines(Mat image)
{
vector<Vec4i> lines;
HoughLinesP(image, lines, 1, CV_PI / 180, 40, 30);
return lines;
}
void LaneDetector::buildLines(vector<Vec4i> lines)
{
vector<double> rightSlopes, leftSlopes;
vector<Point> rightPoints, leftPoints;
Vec4d right, left;
// Filtering lines
for (auto line : lines)
{
Point start = Point(line[0], line[1]);
Point end = Point(line[2], line[3]);
double slope = (end.y - start.y) / (double)(end.x - start.x);
if (abs(slope) > 0.2 && abs(slope) < 10)
{
if (slope > 0)
{
rightPoints.push_back(start);
rightPoints.push_back(end);
rightSlopes.push_back(slope);
}
else
{
leftPoints.push_back(start);
leftPoints.push_back(end);
leftSlopes.push_back(slope);
}
}
}
// Detecting a left line
if (leftPoints.size() > 0)
{
fitLine(leftPoints, left, DIST_L2, 0, 0.01, 0.01);
leftM = average(leftSlopes);
leftB = left[3] - leftM * left[2];
}
// Detecting a right line
if (rightPoints.size() > 0)
{
fitLine(rightPoints, right, DIST_L2, 0, 0.01, 0.01);
rightM = average(rightSlopes);
rightB = right[3] - rightM * right[2];
}
}
int LaneDetector::getDirection()
{
double x = (leftB - rightB) / (rightM - leftM);
double y = leftM * x + leftB;
double centerX = sourceImage.cols / 2;
double centerY = sourceImage.rows / 2;
double rangeX = sourceImage.cols / 6; //6
double rangeY = sourceImage.rows / 3;
if (y > sourceImage.rows / 4)
{
if (x < centerX - rangeX)
{
if (y > centerY + rangeY)
return Direction::TURN_LEFT;
return Direction::GO_LEFT;
}
else if (x > centerX + rangeX)
{
if (y > centerY + rangeY)
return Direction::TURN_RIGHT;
return Direction::GO_RIGHT;
}
}
return Direction::FORWARD;
}
int LaneDetector::drawLane()
{
int direction = getDirection();
double x = (leftB - rightB) / (rightM - leftM);
double y = leftM * x + leftB;
Point leftStart = Point(0, leftB);
Point leftEnd = Point(x, y);
Point rightStart = Point(sourceImage.cols, rightB + sourceImage.cols * rightM);
Point rightEnd = Point(x, y);
// Drawing two lines and intersection point
circle(sourceImage, Point(x, y), 10, Scalar(0, 0, 255), 10);
line(sourceImage, leftStart, leftEnd, Scalar(0, 255, 255), 10, LINE_AA);
line(sourceImage, rightStart, rightEnd, Scalar(0, 255, 255), 10, LINE_AA);
putText(sourceImage, to_string(direction), Point(100, 100), FONT_HERSHEY_TRIPLEX, 2, Scalar(0, 0, 255));
// Show output
namedWindow("Window", WINDOW_NORMAL);
resizeWindow("Window", 600, 600);
imshow("Window", sourceImage);
return direction;
}