forked from tj27-vkr/RCNN-Vehicle-Tracking-Lane-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_image.py
256 lines (205 loc) · 10.4 KB
/
process_image.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
249
250
251
252
253
254
255
256
import cv2
import numpy as np
from moviepy.editor import VideoFileClip
prev_frames = []
crop_points = np.float32([[0 , 720],
[1280 , 720],
[750 , 470],
[530 , 470]])
trans_points = np.float32([[320 , 720],
[960 , 720],
[960 , 0],
[320 , 0]])
def applyBackTrans(img, left_fit, right_fit):
ploty = np.linspace(0, 719, num=720)
# Calculate left and right x positions
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]
# Defining a blank mask to start with
polygon = np.zeros_like(img)
# Create an array of points for the polygon
plot_y = np.linspace(0, img.shape[0]-1, img.shape[0])
pts_left = np.array([np.transpose(np.vstack([left_fitx, plot_y]))])
pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, plot_y])))])
pts = np.hstack((pts_left, pts_right))
# Draw the polygon in blue
cv2.fillPoly(polygon, np.int_([pts]), (0, 0, 255))
# Calculate top and bottom distance between the lanes
top_dist = right_fitx[0] - left_fitx[0]
bottom_dist = right_fitx[-1] - left_fitx[-1]
# Add the polygon to the list of last frames if it makes sense
if len(prev_frames) > 0:
if top_dist < 300 or bottom_dist < 300 or top_dist > 500 or bottom_dist > 500:
polygon = prev_frames[-1]
else:
prev_frames.append(polygon)
else:
prev_frames.append(polygon)
# Check that the new detected lane is similar to the one detected in the previous frame
polygon_gray = cv2.cvtColor(polygon, cv2.COLOR_RGB2GRAY)
prev_gray = cv2.cvtColor(prev_frames[-1], cv2.COLOR_RGB2GRAY)
non_similarity = cv2.matchShapes(polygon_gray,prev_gray, 1, 0.0)
if non_similarity > 0.002:
polygon = prev_frames[-1]
# Calculate the inverse transformation matrix
M_inv = cv2.getPerspectiveTransform(trans_points, crop_points)
# Warp the blank back to original image space using inverse perspective matrix (Minv)
image_backtrans = cv2.warpPerspective(polygon, M_inv, (img.shape[1], img.shape[0]))
# Return the 8-bit mask
return np.uint8(image_backtrans)
def window_mask(width, height, img_ref, center,level):
output = np.zeros_like(img_ref)
output[int(img_ref.shape[0]-(level+1)*height):int(img_ref.shape[0]-level*height),max(0,int(center-width/2)):min(int(center+width/2),img_ref.shape[1])] = 1
return output
def slidingWindow(img):
# Window settings
window_width = 50
window_height = 100
# How much to slide left and right for searching
margin = 30
# Store the (left,right) window centroid positions per level
window_centroids = []
# Create our window template that we will use for convolutions
window = np.ones(window_width)
# Find the starting point for the lines
l_sum = np.sum(img[int(3*img.shape[0]/5):,:int(img.shape[1]/2)], axis=0)
l_center = np.argmax(np.convolve(window,l_sum))-window_width/2
r_sum = np.sum(img[int(3*img.shape[0]/5):,int(img.shape[1]/2):], axis=0)
r_center = np.argmax(np.convolve(window,r_sum))-window_width/2+int(img.shape[1]/2)
# Add what we found for the first layer
window_centroids.append((l_center,r_center))
# Go through each layer looking for max pixel locations
for level in range(1, (int)(img.shape[0] / window_height)):
# convolve the window into the vertical slice of the image
image_layer = np.sum(img[int(img.shape[0]-(level+1)*window_height):int(img.shape[0]-level*window_height),:], axis=0)
conv_signal = np.convolve(window, image_layer)
# Use window_width/2 as offset because convolution signal reference is at right side of window, not center of window
offset = window_width / 2
# Find the best left centroid by using past left center as a reference
l_min_index = int(max(l_center+offset-margin,0))
l_max_index = int(min(l_center+offset+margin,img.shape[1]))
l_center = np.argmax(conv_signal[l_min_index:l_max_index])+l_min_index-offset
# Find the best right centroid by using past right center as a reference
r_min_index = int(max(r_center+offset-margin,0))
r_max_index = int(min(r_center+offset+margin,img.shape[1]))
r_center = np.argmax(conv_signal[r_min_index:r_max_index])+r_min_index-offset
# Add what we found for that layer
window_centroids.append((l_center,r_center))
# If we have found any window centers, print error and return
if len(window_centroids) == 0:
print("No windows found in this frame!")
return
# Points used to draw all the left and right windows
l_points = np.zeros_like(img)
r_points = np.zeros_like(img)
# Go through each level and draw the windows
for level in range(0,len(window_centroids)):
# Window_mask is a function to draw window areas
l_mask = window_mask(window_width,window_height,img,window_centroids[level][0],level)
r_mask = window_mask(window_width,window_height,img,window_centroids[level][1],level)
# Add graphic points from window mask here to total pixels found
l_points[(l_points == 255) | ((l_mask == 1) ) ] = 255
r_points[(r_points == 255) | ((r_mask == 1) ) ] = 255
# Draw the results
template = np.array(r_points+l_points,np.uint8) # add both left and right window pixels together
zero_channel = np.zeros_like(template) # create a zero color channle
template = np.array(cv2.merge((template, template, template)),np.uint8) # make window pixels green
warpage = np.array(cv2.merge((img, img, img)),np.uint8) # making the original road pixels 3 color channels
output = cv2.addWeighted(warpage, 1, template, 0.5, 0.0) # overlay the orignal road image with window results
# Extract left and right line pixel positions
leftx = np.nonzero(l_points)[1]
lefty = np.nonzero(l_points)[0]
rightx = np.nonzero(r_points)[1]
righty = np.nonzero(r_points)[0]
# Fit a second order polynomial to each
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
# Return left and right lines as well as the image
return left_fit, right_fit, output
def area_of_interest(img, points):
#defining a blank mask to start with
mask = np.zeros_like(img)
#defining a 3 channel or 1 channel color to fill the mask with depending on the input image
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
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, points, ignore_mask_color)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def applyTransformation(img):
M = cv2.getPerspectiveTransform(crop_points, trans_points)
transformed = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0]))
return transformed
def abs_sobel_thresh(img, orient='x', thresh_min=0, thresh_max=255):
# Apply x or y gradient with the OpenCV Sobel() function
# and take the absolute value
if orient == 'x':
abs_sobel = np.absolute(cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3))
if orient == 'y':
abs_sobel = np.absolute(cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3))
# Rescale back to 8 bit integer
scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
# Create a copy and apply the threshold
binary_output = np.zeros_like(scaled_sobel)
# Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
binary_output[(scaled_sobel >= thresh_min) & (scaled_sobel <= thresh_max)] = 1
# Return the result
return binary_output
def mag_thresh(img, thresh_min=0, thresh_max=255):
# Take both Sobel x and y gradients
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=9)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=9)
# Calculate the gradient magnitude
gradmag = np.sqrt(sobelx**2 + sobely**2)
# Rescale to 8 bit
scale_factor = np.max(gradmag)/255
gradmag = (gradmag/scale_factor).astype(np.uint8)
# Create a binary image of ones where threshold is met, zeros otherwise
binary_output = np.zeros_like(gradmag)
binary_output[(gradmag >= thresh_min) & (gradmag <= thresh_max)] = 1
# Return the binary image
return binary_output
def applyMasks(img):
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
# Apply sobel in x direction on L and S channel
l_channel_sobel_x = abs_sobel_thresh(l_channel,'x', 20, 200)
s_channel_sobel_x = abs_sobel_thresh(s_channel,'x', 60, 200)
sobel_combined_x = cv2.bitwise_or(s_channel_sobel_x, l_channel_sobel_x)
# Apply magnitude sobel
l_channel_mag = mag_thresh(l_channel, 80, 200)
s_channel_mag = mag_thresh(s_channel, 80, 200)
mag_combined = cv2.bitwise_or(l_channel_mag, s_channel_mag)
# Combine all the sobel filters
sobel_mask = cv2.bitwise_or(mag_combined, sobel_combined_x)
# Mask out the desired image and filter image again
sobel_mask = area_of_interest(sobel_mask, np.array([[(330, 0),(950, 0), (950, 680), (330, 680)]]))
# Convert to HLS and extract S and V channel
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# Define color thresholds in HSV
white_low = np.array([[[0, 0, 210]]])
white_high = np.array([[[255, 30, 255]]])
yellow_low = np.array([[[18, 80, 80]]])
yellow_high = np.array([[[30, 255, 255]]])
# Apply the thresholds to get only white and yellow
white_mask = cv2.inRange(img_hsv, white_low, white_high)
yellow_mask = cv2.inRange(img_hsv, yellow_low, yellow_high)
# Bitwise or the yellow and white mask
color_mask = cv2.bitwise_or(yellow_mask, white_mask)
mask_combined = np.zeros_like(sobel_mask)
mask_combined[(color_mask>=.5)|(sobel_mask>=.5)] = 1
return mask_combined
if __name__ == "__main__":
img_arr = cv2.imread('frame002.jpg')
cropped_img = area_of_interest(img_arr, [crop_points.astype(np.int32)])
trans_img = applyTransformation(cropped_img)
masked_image = applyMasks(trans_img)
left_fit, right_fit, _ = slidingWindow(masked_image)
lane_mask = applyBackTrans(img_arr, left_fit, right_fit)
img_result = cv2.addWeighted(img_arr, 1, lane_mask, 1, 0)
cv2.imwrite('output.png', img_result)