-
Notifications
You must be signed in to change notification settings - Fork 2
/
curved_lane_detection.py
248 lines (222 loc) · 10.7 KB
/
curved_lane_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
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
mport numpy as np
import pandas as pd
import cv2
import os
import glob
import matplotlib.pyplot as plt
import pickle
# Curved lane detection
def pipeline(img, s_thresh=(100, 255), sx_thresh=(15, 255)):
#img = undistort(img)
img = np.copy(img)
# Convert to HLS color space and separate the V channel
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float)
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
h_channel = hls[:,:,0]
# Sobel x
sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 1) # Take the derivative in x
abs_sobelx = np.absolute(sobelx) # Absolute x derivative to accentuate lines away from horizontal
scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx))
# Threshold x gradient
sxbinary = np.zeros_like(scaled_sobel)
sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1
# Threshold color channel
s_binary = np.zeros_like(s_channel)
s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
color_binary = np.dstack((np.zeros_like(sxbinary), sxbinary, s_binary)) * 255
combined_binary = np.zeros_like(sxbinary)
combined_binary[(s_binary == 1) | (sxbinary == 1)] = 1
return combined_binary
def perspective_warp(img,
dst_size=(1280,720),
src=np.float32([(0.43,0.65),(0.58,0.65),(0.1,1),(1,1)]),
dst=np.float32([(0,0), (1, 0), (0,1), (1,1)])):
img_size = np.float32([(img.shape[1],img.shape[0])])
src = src* img_size
dst = dst * np.float32(dst_size)
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(img, M, dst_size)
return warped
def inv_perspective_warp(img,
dst_size=(1280,720),
src=np.float32([(0,0), (1, 0), (0,1), (1,1)]),
dst=np.float32([(0.43,0.65),(0.58,0.65),(0.1,1),(1,1)])):
img_size = np.float32([(img.shape[1],img.shape[0])])
src = src* img_size
dst = dst * np.float32(dst_size)
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(img, M, dst_size)
return warped
def get_hist(img):
hist = np.sum(img[img.shape[0]//2:,:], axis=0)
return hist
img = cv2.imread('Lane-detection/curved_lane_detection/test_images/test2.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst = pipeline(img)
dst_ = perspective_warp(dst, dst_size=(1280,720))
left_a, left_b, left_c = [],[],[]
right_a, right_b, right_c = [],[],[]
def sliding_window(img, nwindows=9, margin=150, minpix = 1, draw_windows=True):
global left_a, left_b, left_c,right_a, right_b, right_c
left_fit_= np.empty(3)
right_fit_ = np.empty(3)
out_img = np.dstack((img, img, img))*255
histogram = get_hist(img)
# find peaks of left and right halves
midpoint = int(histogram.shape[0]/2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
# Set height of windows
window_height = np.int(img.shape[0]/nwindows)
# Identify the x and y positions of all nonzero pixels in the image
nonzero = img.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
# Current positions to be updated for each window
leftx_current = leftx_base
rightx_current = rightx_base
# Create empty lists to receive left and right lane pixel indices
left_lane_inds = []
right_lane_inds = []
for window in range(nwindows):
# Identify window boundaries in x and y (and right and left)
win_y_low = img.shape[0] - (window+1)*window_height
win_y_high = img.shape[0] - window*window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
# Draw the windows on the visualization image
if draw_windows == True:
cv2.rectangle(out_img,(win_xleft_low,win_y_low),(win_xleft_high,win_y_high),
(100,255,255), 3)
cv2.rectangle(out_img,(win_xright_low,win_y_low),(win_xright_high,win_y_high),
(100,255,255), 3)
# Identify the nonzero pixels in x and y within the window
good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]
# Append these indices to the lists
left_lane_inds.append(good_left_inds)
right_lane_inds.append(good_right_inds)
# If you found > minpix pixels, recenter next window on their mean position
if len(good_left_inds) > minpix:
leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
if len(good_right_inds) > minpix:
rightx_current = np.int(np.mean(nonzerox[good_right_inds]))
# if len(good_right_inds) > minpix:
# rightx_current = np.int(np.mean([leftx_current +900, np.mean(nonzerox[good_right_inds])]))
# elif len(good_left_inds) > minpix:
# rightx_current = np.int(np.mean([np.mean(nonzerox[good_left_inds]) +900, rightx_current]))
# if len(good_left_inds) > minpix:
# leftx_current = np.int(np.mean([rightx_current -900, np.mean(nonzerox[good_left_inds])]))
# elif len(good_right_inds) > minpix:
# leftx_current = np.int(np.mean([np.mean(nonzerox[good_right_inds]) -900, leftx_current]))
# Concatenate the arrays of indices
left_lane_inds = np.concatenate(left_lane_inds)
right_lane_inds = np.concatenate(right_lane_inds)
# Extract left and right line pixel positions
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
# Fit a second order polynomial to each
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
left_a.append(left_fit[0])
left_b.append(left_fit[1])
left_c.append(left_fit[2])
right_a.append(right_fit[0])
right_b.append(right_fit[1])
right_c.append(right_fit[2])
left_fit_[0] = np.mean(left_a[-10:])
left_fit_[1] = np.mean(left_b[-10:])
left_fit_[2] = np.mean(left_c[-10:])
right_fit_[0] = np.mean(right_a[-10:])
right_fit_[1] = np.mean(right_b[-10:])
right_fit_[2] = np.mean(right_c[-10:])
# Generate x and y values for plotting
ploty = np.linspace(0, img.shape[0]-1, img.shape[0] )
left_fitx = left_fit_[0]*ploty**2 + left_fit_[1]*ploty + left_fit_[2]
right_fitx = right_fit_[0]*ploty**2 + right_fit_[1]*ploty + right_fit_[2]
out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 100]
out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 100, 255]
return out_img, (left_fitx, right_fitx), (left_fit_, right_fit_), ploty
def get_curve(img, leftx, rightx):
ploty = np.linspace(0, img.shape[0]-1, img.shape[0])
y_eval = np.max(ploty)
ym_per_pix = 30.5/720 # meters per pixel in y dimension
xm_per_pix = 3.7/720 # meters per pixel in x dimension
# Fit new polynomials to x,y in world space
left_fit_cr = np.polyfit(ploty*ym_per_pix, leftx*xm_per_pix, 2)
right_fit_cr = np.polyfit(ploty*ym_per_pix, rightx*xm_per_pix, 2)
# Calculate the new radii of curvature
left_curverad = ((1 + (2*left_fit_cr[0]*y_eval*ym_per_pix + left_fit_cr[1])**2)**1.5) / np.absolute(2*left_fit_cr[0])
right_curverad = ((1 + (2*right_fit_cr[0]*y_eval*ym_per_pix + right_fit_cr[1])**2)**1.5) / np.absolute(2*right_fit_cr[0])
car_pos = img.shape[1]/2
l_fit_x_int = left_fit_cr[0]*img.shape[0]**2 + left_fit_cr[1]*img.shape[0] + left_fit_cr[2]
r_fit_x_int = right_fit_cr[0]*img.shape[0]**2 + right_fit_cr[1]*img.shape[0] + right_fit_cr[2]
lane_center_position = (r_fit_x_int + l_fit_x_int) /2
center = (car_pos - lane_center_position) * xm_per_pix / 10
# Now our radius of curvature is in meters
return (left_curverad, right_curverad, center)
def draw_lanes(img, left_fit, right_fit):
ploty = np.linspace(0, img.shape[0]-1, img.shape[0])
color_img = np.zeros_like(img)
left = np.array([np.transpose(np.vstack([left_fit, ploty]))])
right = np.array([np.flipud(np.transpose(np.vstack([right_fit, ploty])))])
points = np.hstack((left, right))
cv2.fillPoly(color_img, np.int_(points), (0,200,255))
inv_perspective = inv_perspective_warp(color_img)
inv_perspective = cv2.addWeighted(img, 1, inv_perspective, 0.7, 0)
return inv_perspective
#%matplotlib gtk
out_img, curves, lanes, ploty = sliding_window(dst)
#plt.imshow(out_img)
#plt.plot(curves[0], ploty, color='yellow', linewidth=1)
#plt.plot(curves[1], ploty, color='yellow', linewidth=1)
print(np.asarray(curves).shape)
curverad=get_curve(img, curves[0],curves[1])
print(curverad)
img_ = draw_lanes(img, curves[0], curves[1])
#plt.imshow(img_, cmap='hsv')
f, (ax5) = plt.subplots(1, 1, figsize=(50, 10))
#ax1.imshow(img)
#ax1.set_title('(i)', fontsize=10)
#ax2.imshow(dst)
#ax2.set_title('(ii)', fontsize=10)
#ax3.imshow(dst_)
#ax3.set_title('(iii)', fontsize=10)
#ax4.imshow(out_img)
#ax4.plot(curves[0], ploty, color='yellow', linewidth=30)
#ax4.plot(curves[1], ploty, color='yellow', linewidth=30)
#ax4.set_title('Sliding window+Curve Fit', fontsize=20)
ax5.imshow(img_)
ax5.set_title('Overlay Lanes', fontsize=20)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
plt.show()
'''
#def vid_pipeline(img):
global running_avg
global index
img_ = pipeline(img)
img_ = perspective_warp(img_)
out_img, curves, lanes, ploty = sliding_window(img_, draw_windows=False)
curverad =get_curve(img, curves[0], curves[1])
lane_curve = np.mean([curverad[0], curverad[1]])
img = draw_lanes(img, curves[0], curves[1])
font = cv2.FONT_HERSHEY_SIMPLEX
fontColor = (0, 0, 0)
fontSize=0.5
cv2.putText(img, 'Lane Curvature: {:.0f} m'.format(lane_curve), (570, 620), font, fontSize, fontColor, 2)
cv2.putText(img, 'Vehicle offset: {:.4f} m'.format(curverad[2]), (570, 650), font, fontSize, fontColor, 2)
return img
right_curves, left_curves = [],[]
#from moviepy.editor import VideoFileClip
#myclip = VideoFileClip('Lane-detection/curved_lane_detection/project_video.mp4')#.subclip(40,43)
#output_vid = 'output.mp4'
#clip = myclip.fl_image(vid_pipeline)
#clip.write_videofile(output_vid, audio=False)
'''