-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaneDetection.cpp
More file actions
204 lines (159 loc) · 5.9 KB
/
Copy pathLaneDetection.cpp
File metadata and controls
204 lines (159 loc) · 5.9 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
//
// LaneDetection.cpp
// opencv
//
// Created by 심지훈 on 22/05/2019.
// Copyright © 2019 Shim. All rights reserved.
//
#include "LaneDetection.hpp"
bool right_flag = false;
bool left_flag = false;
double img_center;
void onMouse(int event, int x, int y, int flags, void* param)
{
switch(event)
{
case EVENT_LBUTTONDOWN:
cout<<"(x, y ) = ("<<x<<","<<y<<")"<<endl;
break;
}
}
void draw_trapezoid(Mat& dst, Point* pt, const Scalar& color )
{
if(dst.channels() == 1) {
cvtColor(dst, dst, COLOR_GRAY2BGR);
}
line(dst, pt[0], pt[1], color);
line(dst, pt[1], pt[2], color);
line(dst, pt[2], pt[3], color);
line(dst, pt[3], pt[0], color);
}
Mat region_of_interest(Mat img, const Point* pt, int nPoints, const Scalar& color)
{
Mat output;
Mat mask(img.rows, img.cols, CV_8U,Scalar(0));
fillConvexPoly(mask, pt, 4, color);
bitwise_and(img, mask, output);
return output;
}
vector<std::vector<cv::Vec4i> > lineSeperator(std::vector<cv::Vec4i> lines, cv::Mat img_edges)
{
vector<std::vector<cv::Vec4i> > output(2);
size_t j = 0;
Point ini;
Point fini;
double slope_thresh = 0.3;
vector<double> slopes;
vector<cv::Vec4i> selected_lines;
vector<cv::Vec4i> right_lines, left_lines;
// Calculate the slope of all the detected lines
for (auto i : lines) {
ini = Point(i[0], i[1]);
fini = Point(i[2], i[3]);
// Basic algebra: slope = (y1 - y0)/(x1 - x0)
double slope = (static_cast<double>(fini.y) - static_cast<double>(ini.y))/(static_cast<double>(fini.x) - static_cast<double>(ini.x) + 0.00001);
// If the slope is too horizontal, discard the line
// If not, save them and their respective slope
if (std::abs(slope) > slope_thresh) {
slopes.push_back(slope);
selected_lines.push_back(i);
}
}
// Split the lines into right and left lines
img_center = static_cast<double>((img_edges.cols / 2));
while (j < selected_lines.size()) {
ini = Point(selected_lines[j][0], selected_lines[j][1]);
fini = Point(selected_lines[j][2], selected_lines[j][3]);
// Condition to classify line as left side or right side
if (slopes[j] > 0 && fini.x > img_center && ini.x > img_center) {
right_lines.push_back(selected_lines[j]);
right_flag = true;
} else if (slopes[j] < 0 && fini.x < img_center && ini.x < img_center) {
left_lines.push_back(selected_lines[j]);
left_flag = true;
}
j++;
}
output[0] = right_lines;
output[1] = left_lines;
return output;
}
std::vector<cv::Point> regression(std::vector<std::vector<cv::Vec4i> > left_right_lines, cv::Mat inputImage) {
vector<cv::Point> output(4);
Point ini;
Point fini;
Point ini2;
Point fini2;
Vec4d right_line;
Vec4d left_line;
vector<cv::Point> right_pts;
vector<cv::Point> left_pts;
double right_m=0, left_m=0;
Point right_b, left_b;
// If right lines are being detected, fit a line using all the init and final points of the lines
if (right_flag == true) {
for (auto i : left_right_lines[0]) {
ini = cv::Point(i[0], i[1]);
fini = cv::Point(i[2], i[3]);
right_pts.push_back(ini);
right_pts.push_back(fini);
}
if (right_pts.size() > 0) {
// The right line is formed here
cv::fitLine(right_pts, right_line, DIST_L2, 0, 0.01, 0.01);
right_m = right_line[1] / right_line[0];
right_b = cv::Point(right_line[2], right_line[3]);
}
} else {
cout<<"No right lane"<<endl;
}
// If left lines are being detected, fit a line using all the init and final points of the lines
if (left_flag == true) {
for (auto j : left_right_lines[1]) {
ini2 = cv::Point(j[0], j[1]);
fini2 = cv::Point(j[2], j[3]);
left_pts.push_back(ini2);
left_pts.push_back(fini2);
}
if (left_pts.size() > 0) {
// The left line is formed here
cv::fitLine(left_pts, left_line, DIST_L2, 0, 0.01, 0.01);
left_m = left_line[1] / left_line[0];
left_b = cv::Point(left_line[2], left_line[3]);
}
} else {
cout<<"NO left lane"<<endl;
}
// One the slope and offset points have been obtained, apply the line equation to obtain the line points
int ini_y = inputImage.rows ;
int fin_y = 360;
double right_ini_x = ((ini_y - right_b.y) / right_m) + right_b.x;
double right_fin_x = ((fin_y - right_b.y) / right_m) + right_b.x;
double left_ini_x = ((ini_y - left_b.y) / left_m) + left_b.x;
double left_fin_x = ((fin_y - left_b.y) / left_m) + left_b.x;
cout<<endl;
cout<<"right_ini_x = "<<right_ini_x<<endl;
cout<<"right_fin_x = "<<right_fin_x<<endl;
cout<<"left_ini_x = "<<left_ini_x<<endl;
cout<<"left_fin_x = "<<left_fin_x<<endl;
output[0] = cv::Point(right_ini_x, ini_y);
output[1] = cv::Point(right_fin_x, fin_y);
output[2] = cv::Point(left_ini_x, ini_y);
output[3] = cv::Point(left_fin_x, fin_y);
return output;
}
void plotLane(Mat inputImage, vector<Point> lane)
{
vector<Point> poly_points;
Mat output;
inputImage.copyTo(output);
poly_points.push_back(lane[2]);
poly_points.push_back(lane[0]);
poly_points.push_back(lane[1]);
poly_points.push_back(lane[3]);
fillConvexPoly(output, poly_points, Scalar(0,255,0));
addWeighted(output, 0.3, inputImage, 1.0, -0.3, inputImage);
line(inputImage, lane[0], lane[1], Scalar(0,0,255),2, LINE_AA);
line(inputImage, lane[2], lane[3], Scalar(0,0,255),2, LINE_AA);
imshow("image", inputImage);
}