Skip to content
Snippets Groups Projects
Commit 444d3de0 authored by Leendert Hendricus Pruissen's avatar Leendert Hendricus Pruissen
Browse files

updated detect lanes function

parent 4d394dd2
No related branches found
No related tags found
No related merge requests found
......@@ -27,20 +27,62 @@ def detect_edge(pic):
return img_edge
def ROI(pic):
return 0
height = pic.shape[0]
triangle = np.array([[(200, height), (1100, height), (550, 250)]])
mask = np.zeros_like(pic)
cv2.fillPoly(mask, triangle, 255)
roi = cv2.bitwise_and(pic, mask)
return roi
def applyLanes(original_pic, edge_pic):
lines = cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=70, minLineLength=10)
def getLines(edge_pic):
return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=70, minLineLength=10)
def applyLines(original_pic, lines):
for line in lines:
x1, y1, x2, y2 = line[0]
print(x1)
cv2.line(original_pic, (x1, y1), (x2, y2), (0, 255, 0), 3)
x1, y1, x2, y2 = line.reshape(4)
cv2.line(original_pic, (x1, y1), (x2, y2), (255, 0, 0), 3)
return original_pic
def find_average_lane(pic, lines):
left_lines = []
right_lines = []
for line in lines:
x1, y1, x2, y2 = line[0]
parameters = np.polyfit((x1, x2), (y1, y2), 1)
print(parameters )
slope = parameters[0]
intercept = parameters[1]
if(slope < 0):
left_lines.append((slope, intercept))
else:
right_lines.append((slope, intercept))
video = cv2.VideoCapture("test_data.mp4")
right_average = np.average(left_lines, axis=0)
left_average = np.average(right_lines, axis=0)
left_line = make_coordinates(pic, left_average)
right_line = make_coordinates(pic, right_average)
print("Left fit")
print(left_line)
print("\nRight fit")
print(right_line)
return np.array([left_line, right_line])
def make_coordinates(image, line_parameters):
slope, intercept = line_parameters
y1 = image.shape[0]
y2 = int(y1*(1/2))
x1 = int((y1 - intercept)/slope)
x2 = int((y2 - intercept) / slope)
return np.array([x1, y1, x2, y2])
video = cv2.VideoCapture("test2.mp4")
while True:
ret, frame = video.read()
......@@ -55,9 +97,15 @@ while True:
wEdges = detect_edge(new_img)
lanes = applyLanes(frame, wEdges)
cropped = ROI(wEdges)
lines = getLines(cropped)
cv2.imshow("frame", wEdges)
average_lines = find_average_lane(new_img, lines)
average_lanes = applyLines(frame, average_lines)
cv2.imshow("frame", average_lanes)
key = cv2.waitKey(25)
if key == 27:
......@@ -70,18 +118,22 @@ cv2.destroyAllWindows()
# Load image
img = cv2.imread('lol_image.jpg')
cv2.imshow("original", img)
new_img = formatImg(img)
wEdges = detect_edge(new_img)
cropped = ROI(wEdges)
lines = getLines(cropped)
lanes = applyLanes(img, wEdges)
#lanes = applyLines(img, lines)
average_lines = find_average_lane(img, lines)
average_lanes = applyLines(img, average_lines)
cv2.imshow("edges", wEdges)
cv2.imshow("with lines", lanes)
#cv2.imshow("with lines", lanes)
cv2.imshow("Average", average_lanes)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment