-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlane_detect_img.py
153 lines (114 loc) · 4.83 KB
/
lane_detect_img.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
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
import math
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
import os
os.chdir('E:/Users/İndirilenler/yapay_zeka_proje_Lane_Detection')
def grayscale(img):
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
def canny(img, low_threshold, high_threshold):
return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(img, kernel_size):
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(img, vertices):
#empty mask
mask = np.zeros_like(img)
#color to fill the mask
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
# Fill pixels inside a polygon with a fill color
cv2.fillPoly(mask, vertices, ignore_mask_color)
# Rotate image only if mask pixels are non-zero
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines, color=[255, 0, 0], thickness=10):
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
def slope_lines(image,lines):
img = image.copy()
poly_vertices = []
order = [0,1,3,2]
left_lines = []
right_lines = []
for line in lines:
for x1,y1,x2,y2 in line:
if x1 == x2:
pass #Dikey
else:
m = (y2 - y1) / (x2 - x1)
c = y1 - m * x1
if m < 0:
left_lines.append((m,c))
elif m >= 0:
right_lines.append((m,c))
left_line = np.mean(left_lines, axis=0)
right_line = np.mean(right_lines, axis=0)
#print(left_line, right_line)
for slope, intercept in [left_line, right_line]:
# y1 height is obtained
rows, cols = image.shape[:2]
y1= int(rows) #image.shape[0]
# y2 value
y2= int(rows*0.6) #int(0.6*y1)
# line equation
x1=int((y1-intercept)/slope)
x2=int((y2-intercept)/slope)
poly_vertices.append((x1, y1))
poly_vertices.append((x2, y2))
draw_lines(img, np.array([[[x1,y1,x2,y2]]]))
poly_vertices = [poly_vertices[i] for i in order]
cv2.fillPoly(img, pts = np.array([poly_vertices],'int32'), color = (0,255,0))
return cv2.addWeighted(image,0.7,img,0.4,0.)
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
#draw_lines(line_img, lines)
line_img = slope_lines(line_img,lines)
return line_img
def weighted_img(img, initial_img, α=0.1, β=1., γ=0.):
lines_edges = cv2.addWeighted(initial_img, α, img, β, γ)
#lines_edges = cv2.polylines(lines_edges,get_vertices(img), True, (0,0,255), 10)
return lines_edges
def get_vertices(image):
rows, cols = image.shape[:2]
bottom_left = [cols*0.15, rows]
top_left = [cols*0.45, rows*0.6]
bottom_right = [cols*0.95, rows]
top_right = [cols*0.55, rows*0.6]
ver = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32)
return ver
# Lane finding
def lane_finding_pipeline(image):
#Grayscale
gray_img = grayscale(image)
#Gaussian Smoothing
smoothed_img = gaussian_blur(img = gray_img, kernel_size = 5)
#Canny Edge Detection
canny_img = canny(img = smoothed_img, low_threshold = 180, high_threshold = 240)
#Masked Image Within a Polygon
masked_img = region_of_interest(img = canny_img, vertices = get_vertices(image))
#Hough Transform Lines
houghed_lines = hough_lines(img = masked_img, rho = 1, theta = np.pi/180, threshold = 20, min_line_len = 20, max_line_gap = 180)
#Draw lines on edges
output = weighted_img(img = houghed_lines, initial_img = image, α=0.8, β=1., γ=0.)
return output
for image_path in list(os.listdir('./input_images')):
fig = plt.figure(figsize=(20, 10))
image = mpimg.imread(f'./input_images/{image_path}')
ax = fig.add_subplot(1, 2, 1,xticks=[], yticks=[])
plt.imshow(image)
ax.set_title("Input Image")
ax = fig.add_subplot(1, 2, 2,xticks=[], yticks=[])
plt.imshow(lane_finding_pipeline(image))
ax.set_title("Output Image [Lane Line Detected]")
plt.show()
img = lane_finding_pipeline(image)
img_name = os.path.basename(image_path)
img_name_final = os.path.splitext(img_name)[0] + '.jpg'
path = 'output/' + img_name_final
cv2.imwrite(path,img)