-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
53 lines (52 loc) · 2.82 KB
/
test.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
rightSlope, leftSlope, rightIntercept, leftIntercept = [],[],[],[]
def draw_lines(img, lines, thickness=5):
global rightSlope, leftSlope, rightIntercept, leftIntercept
rightColor=[0,255,0] leftColor=[255,0,0]
#this is used to filter out the outlying lines that can affect the average
#We then use the slope we determined to find the y-intercept of the filtered lines by solving for b in y=mx+b
for line in lines:
for x1,y1,x2,y2 in line:
slope = (y1-y2)/(x1-x2)
if slope > 0.3:
if x1 > 500 :
yintercept = y2 - (slope*x2)
rightSlope.append(slope)
rightIntercept.append(yintercept)
else: None
elif slope < -0.3:
if x1 < 600:
yintercept = y2 - (slope*x2)
leftSlope.append(slope)
leftIntercept.append(yintercept)
#We use slicing operators and np.mean() to find the averages of the 30 previous frames
#This makes the lines more stable, and less likely to shift rapidly
leftavgSlope = np.mean(leftSlope[-30:])
leftavgIntercept = np.mean(leftIntercept[-30:])
rightavgSlope = np.mean(rightSlope[-30:])
rightavgIntercept = np.mean(rightIntercept[-30:])
#Here we plot the lines and the shape of the lane using the average slope and intercepts
try:
left_line_x1 = int((0.65*img.shape[0] - leftavgIntercept)/leftavgSlope)
left_line_x2 = int((img.shape[0] - leftavgIntercept)/leftavgSlope)
right_line_x1 = int((0.65*img.shape[0] - rightavgIntercept)/rightavgSlope)
right_line_x2 = int((img.shape[0] - rightavgIntercept)/rightavgSlope)
pts = np.array([[left_line_x1, int(0.65*img.shape[0])],[left_line_x2, int(img.shape[0])],[right_line_x2, int(img.shape[0])],[right_line_x1, int(0.65*img.shape[0])]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.fillPoly(img,[pts],(0,0,255))
cv2.line(img, (left_line_x1, int(0.65*img.shape[0])), (left_line_x2, int(img.shape[0])), leftColor, 10)
cv2.line(img, (right_line_x1, int(0.65*img.shape[0])), (right_line_x2, int(img.shape[0])), rightColor, 10)
except ValueError:
#I keep getting errors for some reason, so I put this here. Idk if the error still persists.
pass
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
"""
`img` should be the output of a Canny transform.
"""
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)
return line_img
def linedetect(img):
return hough_lines(img, 1, np.pi/180, 10, 20, 100)
hough_img = list(map(linedetect, canny_img))
display_images(hough_img)