-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
242 lines (193 loc) · 9.51 KB
/
functions.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
# print("Functions")
import math
import numpy as np
import cv2
from utils import globalvars
def perp(a) :
b = np.empty_like(a)
b[0] = -a[1]
b[1] = a[0]
return b
# line segment a given by endpoints a1, a2
# line segment b given by endpoints b1, b2
# return
def seg_intersect(a1,a2, b1,b2):
da = a2-a1
db = b2-b1
dp = a1-b1
dap = perp(da)
denom = np.dot( dap, db)
num = np.dot( dap, dp )
return (num / denom.astype(float))*db + b1
def movingAverage(avg, new_sample, N=20):
'''
Given a series of numbers and a fixed subset size, the first
element of the moving average is obtained by taking the average
of the initial fixed subset of the number series.
Then the subset is modified by "shifting forward";
that is, excluding the first number of the series and including the next value in the subset.
'''
if (avg == 0):
return new_sample
avg -= avg / N;
avg += new_sample / N;
return avg;
def grayscale(img):
"""Applies the Grayscale transform
This will return an image with only one color channel
but NOTE: to see the returned image as grayscale
you should call plt.imshow(gray, cmap='gray')"""
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def bgr_to_hsv(img):
"""Applies HSV transform"""
return cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
def bgr_to_hls(img):
"""Applies HLS transform"""
return cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
def bgr_to_lab(img):
"""Applies the CIE-LAB colorspace
It yields best results for low brightness im images"""
return cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
def bgr_to_y(img):
"""Applies the YCrCb transform"""
return cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
def canny(img, low_threshold, high_threshold):
"""Applies the Canny transform"""
return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(img, kernel_size):
"""Applies a Gaussian Noise kernel"""
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(img, vertices):
"""
Applies an image mask.
Only keeps the region of the image defined by the polygon
formed from `vertices`. The rest of the image is set to black.
"""
#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, vertices, ignore_mask_color)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines, color=[255, 0, 0], thickness=2):
print(globalvars.avgLeft, globalvars.avgRight)
"""
NOTE: this is the function you might want to use as a starting point once you want to
average/extrapolate the line segments you detect to map out the full
extent of the lane (going from the result shown in raw-lines-example.mp4
to that shown in P1_example.mp4).
Think about things like separating line segments by their
slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left
line vs. the right line. Then, you can average the position of each of
the lines and extrapolate to the top and bottom of the lane.
This function draws `lines` with `color` and `thickness`.
Lines are drawn on the image inplace (mutates the image).
If you want to make the lines semi-transparent, think about combining
this function with the weighted_img() function below
"""
# state variables to keep track of most dominant segment
largestLeftLineSize = 0
largestRightLineSize = 0
largestLeftLine = (0,0,0,0)
largestRightLine = (0,0,0,0)
if lines is None:
avgx1, avgy1, avgx2, avgy2 = globalvars.avgLeft
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw left line
avgx1, avgy1, avgx2, avgy2 = globalvars.avgRight
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw right line
return
'''
Find largest left and largest right line.
'''
for line in lines:
for x1,y1,x2,y2 in line:
size = math.hypot(x2 - x1, y2 - y1)
slope = ((y2-y1)/(x2-x1))
# Filter slope based on incline and
# find the most dominent segment based on length
if (slope > 0.5): #right
if (size > largestRightLineSize):
largestRightLine = (x1, y1, x2, y2)
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
elif (slope < -0.5): #left
if (size > largestLeftLineSize):
largestLeftLine = (x1, y1, x2, y2)
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
# Define an imaginary horizontal line in the center of the screen
# and at the bottom of the image, to extrapolate determined segment
'''
Two horizontal lines - bottom and 1/3rd distance from bottom.
'''
imgHeight, imgWidth = (img.shape[0], img.shape[1])
upLinePoint1 = np.array( [0, int(imgHeight - (imgHeight/3))] )
upLinePoint2 = np.array( [int(imgWidth), int(imgHeight - (imgHeight/3))] )
downLinePoint1 = np.array( [0, int(imgHeight)] )
downLinePoint2 = np.array( [int(imgWidth), int(imgHeight)] )
# Find the intersection of dominant lane with an imaginary horizontal line
# in the middle of the image and at the bottom of the image.
'''
Intersection of largest left line with horizontal lines.
'''
p3 = np.array( [largestLeftLine[0], largestLeftLine[1]] )
p4 = np.array( [largestLeftLine[2], largestLeftLine[3]] )
upLeftPoint = seg_intersect(upLinePoint1,upLinePoint2, p3,p4)
downLeftPoint = seg_intersect(downLinePoint1,downLinePoint2, p3,p4)
if (math.isnan(upLeftPoint[0]) or math.isnan(downLeftPoint[0])):
avgx1, avgy1, avgx2, avgy2 = globalvars.avgLeft
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw left line
avgx1, avgy1, avgx2, avgy2 = globalvars.avgRight
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw right line
return
# cv2.line(img, (int(upLeftPoint[0]), int(upLeftPoint[1])), (int(downLeftPoint[0]), int(downLeftPoint[1])), [0, 0, 255], 8) #draw left line
# Calculate the average position of detected left lane over multiple video frames and draw
avgx1, avgy1, avgx2, avgy2 = globalvars.avgLeft
globalvars.avgLeft = (movingAverage(avgx1, upLeftPoint[0]), movingAverage(avgy1, upLeftPoint[1]), movingAverage(avgx2, downLeftPoint[0]), movingAverage(avgy2, downLeftPoint[1]))
avgx1, avgy1, avgx2, avgy2 = globalvars.avgLeft
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw left line
# Find the intersection of dominant lane with an imaginary horizontal line
# in the middle of the image and at the bottom of the image.
'''
Intersection of largest right line with horizontal lines.
'''
p5 = np.array( [largestRightLine[0], largestRightLine[1]] )
p6 = np.array( [largestRightLine[2], largestRightLine[3]] )
upRightPoint = seg_intersect(upLinePoint1,upLinePoint2, p5,p6)
downRightPoint = seg_intersect(downLinePoint1,downLinePoint2, p5,p6)
if (math.isnan(upRightPoint[0]) or math.isnan(downRightPoint[0])):
avgx1, avgy1, avgx2, avgy2 = globalvars.avgLeft
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw left line
avgx1, avgy1, avgx2, avgy2 = globalvars.avgRight
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw right line
return
# cv2.line(img, (int(upRightPoint[0]), int(upRightPoint[1])), (int(downRightPoint[0]), int(downRightPoint[1])), [0, 0, 255], 8) #draw left line
# Calculate the average position of detected right lane over multiple video frames and draw
avgx1, avgy1, avgx2, avgy2 = globalvars.avgRight
globalvars.avgRight = (movingAverage(avgx1, upRightPoint[0]), movingAverage(avgy1, upRightPoint[1]), movingAverage(avgx2, downRightPoint[0]), movingAverage(avgy2, downRightPoint[1]))
avgx1, avgy1, avgx2, avgy2 = globalvars.avgRight
cv2.line(img, (int(avgx1), int(avgy1)), (int(avgx2), int(avgy2)), [255,255,255], 12) #draw left line
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
"""
`img` should be the output of a Canny transform.
Returns an image with hough lines drawn.
"""
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
line_img = np.zeros((*img.shape, 3), dtype=np.uint8)
draw_lines(line_img, lines)
return line_img
def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
"""
`img` is the output of the hough_lines(), An image with lines drawn on it.
Should be a blank image (all black) with lines drawn on it.
`initial_img` should be the image before any processing.
The result image is computed as follows:
initial_img * α + img * β + λ
NOTE: initial_img and img must be the same shape!
"""
return cv2.addWeighted(initial_img, α, img, β, λ)