Skip to content

Instantly share code, notes, and snippets.

@Goddard
Last active February 10, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goddard/309e647071a30ff35752f9ca6116c502 to your computer and use it in GitHub Desktop.
Save Goddard/309e647071a30ff35752f9ca6116c502 to your computer and use it in GitHub Desktop.
def draw_lines(img, lines, color=[255, 0, 0], thickness=10):
"""
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
"""
left_m = 0
left_b = 0
left_count = 0
right_m = 0
right_b = 0
right_count = 0
left_stop = -0.43
right_stop = 0.43
y_stop = 315
for line in lines:
for x1,y1,x2,y2 in line:
slope = ((y2-y1)/(x2-x1))
intercept = y2 - (slope * x2)
if slope > left_stop:
left_m += slope
left_b += intercept
left_count += 1
elif slope < right_stop:
right_m += slope
right_b += intercept
right_count += 1
print("Slope : ", slope)
print("Intercept : ", intercept)
print("Left m : ", left_m)
print("Left b : ", left_b)
print("Left count : ", left_count)
print("Right m : ", right_m)
print("Right b : ", right_b)
print("Right count : ", right_count)
if left_count > 0:
mL = left_m / left_count
bL = left_b / left_count
if right_count > 0:
mR = right_m / right_count
bR = right_b / right_count
if left_count > 0:
x1 = (img.shape[0] - bL) / mL
x2 = (y_stop - bL) / mL
cv2.line(img, (int(x1), int(img.shape[0])), (int(x2), int(y_stop)), color, thickness)
if right_count > 0:
x1 = (img.shape[0] - bR) / mR
x2 = (y_stop - bR) / mR
cv2.line(img, (int(x1), int(img.shape[0])), (int(x2), int(y_stop)), color, thickness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment