-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad line detection.py
109 lines (82 loc) · 3.15 KB
/
road line detection.py
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
#load liberaries
import numpy as np
import cv2
from matplotlib import pyplot as plt
import math
#Load image
img=cv2.imread('solidYellowCurve.jpg')
#dinoissing image
blurred= cv2.blur(img, (3,3))
#edge detection with canny
gray= cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
edges=cv2.Canny(gray,75,150)
#mask the image (get region of interset[ROI])
plt.imshow(img)
#observation from plot
points=np.array([[200,500],
[800,500],
[460,300]])
mask=np.zeros_like(edges)
color=[255]
cv2.fillPoly(mask, [points], color)
masked_img=cv2.bitwise_and(edges, edges,mask=mask)
#line detection using Hough transform technique
lines=cv2.HoughLinesP(masked_img, rho=1, theta=np.pi/180,threshold=3,minLineLength=30, maxLineGap=120)
#formulate lines
#LINE PARAMETERS
SLOPE_THRESHOLD = 0.3
Y_MIN_ADJUST = 15
positive_slopes = []
negative_slopes = []
positive_intercepts = []
negative_intercepts = []
#named as y_max despte being at the bottom corner of the image due to y axis in reverse direction
y_max = img.shape[0]
y_min = img.shape[0]
for line in lines:
for x1,y1,x2,y2 in line:
#calculate slope for the line
slope = (y2-y1)/(x2-x1)
intercept = y2 - (slope*x2)
#for negative slope
if slope < 0.0 and slope > -math.inf and abs(slope) > SLOPE_THRESHOLD:
negative_slopes.append(slope)
negative_intercepts.append(intercept)
#for positive slope
elif slope > 0.0 and slope < math.inf and abs(slope) > SLOPE_THRESHOLD:
positive_slopes.append(slope)
positive_intercepts.append(intercept)
y_min = min(y_min, y1, y2)
y_min+=Y_MIN_ADJUST
#get averages for positive and negative slopes
positive_slope_mean = np.mean(positive_slopes)
negative_slope_mean = np.mean(negative_slopes)
#get averages for potitive and negative intercepts
positive_intercept_mean = np.mean(positive_intercepts)
negative_intercept_mean = np.mean(negative_intercepts)
lst = [
[[0, 0, 0, 0]],
[[0, 0, 0, 0]]
]
#calculation of coordinates for lane for positive slopes
if len(positive_slopes) > 0:
x_max = int((y_max - positive_intercept_mean)/positive_slope_mean)
x_min = int((y_min - positive_intercept_mean)/positive_slope_mean)
lst[0][0] = [x_min, y_min, x_max, y_max]
#calculation of coordinates for lane for negative slopes
if len(negative_slopes) > 0:
x_max = int((y_max - negative_intercept_mean)/negative_slope_mean)
x_min = int((y_min - negative_intercept_mean)/negative_slope_mean)
lst[1][0] = [x_min, y_min, x_max, y_max]
#draw lines on image
color = [243, 105, 14]
thickness = 12
lines_image = np.zeros((masked_img.shape[0], masked_img.shape[1], 3), dtype=np.uint8)
for line in lst:
for x1,y1,x2,y2 in line:
cv2.line(lines_image, (x1, y1), (x2, y2), color, thickness)
final_image = cv2.addWeighted(img, 0.8, lines_image, 1, 0)
cv2.imshow("final_image",final_image)
cv2.imwrite('result.jpg',final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()