Skip to content

Instantly share code, notes, and snippets.

@jmsktm
Created October 17, 2018 06:43
Show Gist options
  • Save jmsktm/b3e378dec61c2d850aead0f51312a325 to your computer and use it in GitHub Desktop.
Save jmsktm/b3e378dec61c2d850aead0f51312a325 to your computer and use it in GitHub Desktop.
"""
This function takes hough lines as input, and uses the information
to arrive at two straight lanes which correspond to the left and
the right lanes.
"""
def final_lines(lines, video_width, ymin, ymax):
# Find the middle of screen (widthwise). We will use this to determine
# whether a line/coordinate will be used to generate the left or
# the right lane
mid = video_width / 2
#Initializing numpy arrays to store coordinates from hough lines
left_x, left_y, right_x, right_y = np.array([]), np.array([]), np.array([]), np.array([])
# Initializing the end coordinates of the left lane:
# (left_x1, left_y1) and (left_x2, left_y2)
left_x1, left_y1, left_x2, left_y2 = 0., 0., 0., 0.
# Initializing the end coordinates of the right lane:
# (right_x1, right_y1) and (right_x2, right_y2)
right_x1, right_y1, right_x2, right_y2 = 0., 0., 0., 0.
for line in lines:
for x1, y1, x2, y2 in line:
# Calculating slope, length of line etc to determine whether or
# not to include the hough line to come to the final result (two lines)
slope = slope_of_line(x1, y1, x2, y2)
length = length_of_line(x1, y1, x2, y2)
# Considering only those lines which meets the criteria. Eg.
# if the line is to the left of the image, the slope should be
# negative and between certain degrees. Same for the hough lines on the right.
# If they meet the criteria, the end coordinates of the lines are
# stored in the respective numpy arrays.
if ((x1 < mid and x2 < mid and slope < 0 and include_line(slope) == True) or (x1 > mid and x2 > mid and slope > 0 and include_line(slope) == True)):
if x1 < mid:
left_x, left_y = np.append(left_x, [x1]), np.append(left_y, [y1])
else:
right_x, right_y = np.append(right_x, [x1]), np.append(right_y, [y1])
if x2 < mid:
left_x, left_y = np.append(left_x, [x2]), np.append(left_y, [y2])
else:
right_x, right_y = np.append(right_x, [x2]), np.append(right_y, [y2])
# If the arrays are empty, they cannot be regressed. Otherwise, we perform
# curve-fitting on the array using linear regression function.
# Once we have slopes(slope_left, slope_right) and y-intercepts(intercept_left, intercept_right)
# of both the lines (left and right), we find the x and y coordinates of the lines (lanes)
# between y=ymin and y=ymax using the formula y=mx+b (or x=(y-b)/m)
# of both the lines at y=ymin and y=ymax
if left_x.size>0 and left_y.size >0:
slope_left, intercept_left, r_value1, p_value1, std_err1 = stats.linregress(left_x, left_y)
left_x1, left_y1, left_x2, left_y2 = (ymax - intercept_left) / slope_left, ymax, (ymin - intercept_left) / slope_left, ymin
if right_x.size>0 and right_y.size >0:
slope_right, intercept_right, r_value2, p_value2, std_err2 = stats.linregress(right_x, right_y)
right_x1, right_y1, right_x2, right_y2 = (ymin - intercept_right) / slope_right, ymin, (ymax - intercept_right) / slope_right, ymax
# Returning the end-coordinates of the lanes as an array.
return np.array([left_x1, left_y1, left_x2, left_y2, right_x1, right_y1, right_x2, right_y2]).astype(int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment