In this project, you will use the tools you learned about in the lesson to identify lane lines on the road. You can develop your pipeline on a series of individual images, and later apply the result to a video stream (really just a series of images). Check out the video clip "raw-lines-example.mp4" (also contained in this repository) to see what the output should look like after using the helper functions below.
Once you have a result that looks roughly like "raw-lines-example.mp4", you'll need to get creative and try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. You can see an example of the result you're going for in the video "P1_example.mp4". Ultimately, you would like to draw just one line for the left side of the lane, and one for the right.
In addition to implementing code, there is a brief writeup to complete. The writeup should be completed in a separate file, which can be either a markdown file or a pdf document. There is a write up template that can be used to guide the writing process. Completing both the code in the Ipython notebook and the writeup template will cover all of the rubric points for this project.
Let's have a look at our first image called './images/solidWhiteRight.jpg'. Run the 2 cells below (hit Shift-Enter or the "play" button above) to display the image.
Note: If, at any point, you encounter frozen display windows or other confounding issues, you can always start again with a clean slate by going to the "Kernel" menu above and selecting "Restart & Clear Output".
The tools you have are color selection, region of interest selection, grayscaling, Gaussian smoothing, Canny Edge Detection and Hough Tranform line detection. You are also free to explore and try other techniques that were not presented in the lesson. Your goal is piece together a pipeline to detect the line segments in the image, then average/extrapolate them and draw them onto the image for display (as below). Once you have a working pipeline, try it out on the video stream below.
Run the cell below to import some packages. If you get an import error
for a package you've already installed, try changing your kernel (select the Kernel menu above --> Change Kernel). Still have problems? Try relaunching Jupyter Notebook from the terminal prompt. Also, see this forum post for more troubleshooting tips.
In [1]:
#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
%matplotlib inline
In [2]:
#reading in an image
image = mpimg.imread('images/solidWhiteRight.jpg')
#printing out some stats and plotting
print('This image is:', type(image), 'with dimesions:', image.shape)
plt.imshow(image) # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')
Out[2]:
Some OpenCV functions (beyond those introduced in the lesson) that might be useful for this project are:
cv2.inRange()
for color selection
cv2.fillPoly()
for regions selection
cv2.line()
to draw lines on an image given endpoints
cv2.addWeighted()
to coadd / overlay two images
cv2.cvtColor()
to grayscale or change color
cv2.imwrite()
to output images to file
cv2.bitwise_and()
to apply a mask to an image
Check out the OpenCV documentation to learn about these and discover even more awesome functionality!
Below are some helper functions to help get you started. They should look familiar from the lesson!
In [87]:
zeros = np.zeros(shape=(10, 10))
nums = np.arange(0, 10)
zeros[1:4, :] = nums
print(zeros)
In [157]:
import math
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
(assuming your grayscaled image is called 'gray')
you should call plt.imshow(gray, cmap='gray')"""
# Or use BGR2GRAY if you read an image with cv2.imread()
# return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
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 create_vertices(img):
"""
'img' is a canny transform edge image
Adjust our vertices here to be a trapezoid
The top of the trapezoid should be where we first detect edges from the center looking bottom-up
Sides of the trapezoid should extend to edges (plus buffer)
"""
ysize, xsize = img.shape[0], img.shape[1]
bottom_ignore = ysize//6
ybuffer = ysize//30
xbuffer_top = xsize//50
xbuffer_bot = xbuffer_top*2
side_search_buffer = ybuffer//2
# Let's find the last white pixel's index in the center column.
# This will give us an idea of where our region should be
# We ignore a certain portion of the bottom of the screen so we get a better region top
# - This is partly because car hoods can obsure the region
center_white = img[:ysize-bottom_ignore, xsize//2] == 255
indices = np.arange(0, center_white.shape[0])
indices[~center_white] = 0
last_white_ind = np.amax(indices)
# If our first white pixel is too close to the bottom of the screen, default back to the screen center
# region_top_y = (last_white_ind if last_white_ind < 4*ysize//5 else ysize//2) + ybuffer
region_top_y = min(last_white_ind + ybuffer, ysize-1)
# Now we need to find the x-indices for the top segment of our region
# To do this we will look left and right from our center point until we find white
y_slice_top = max(region_top_y - side_search_buffer, 0)
y_slice_bot = min(region_top_y + side_search_buffer, ysize-1)
region_top_white = np.copy(img[y_slice_top:y_slice_bot, :]) == 255
indices = np.zeros_like(region_top_white, dtype='int32')
indices[:, :] = np.arange(0, xsize)
indices[~region_top_white] = 0
# Separate into right and left sides we can grab our indices easier:
# Right side min and left side max
right_side = np.copy(indices)
right_side[right_side < xsize//2] = xsize*2 # Large number because we will take min
left_side = np.copy(indices)
left_side[left_side > xsize//2] = 0
region_top_x_left = max(np.amax(left_side) - xbuffer_top, 0)
region_top_x_right = min(np.amin(right_side) + xbuffer_top, xsize)
# Now we do the same thing for the bottom
# Look left and right from the center until we hit white
indices = np.arange(0, xsize)
region_bot_white = img[ysize-bottom_ignore, :] == 255
indices[~region_bot_white] = 0
# Separate into right and left sides we can grab our indices easier:
# Right side min and left side max
right_side = np.copy(indices)
right_side[right_side < xsize//2] = xsize*2 # Large number because we will take min
left_side = np.copy(indices)
left_side[left_side > xsize//2] = 0
region_bot_x_left = max(np.amax(left_side) - xbuffer_bot, 0)
region_bot_x_right = min(np.amin(right_side) + xbuffer_bot, xsize)
# Because of our bottom_ignore, we need to extrapolate these bottom x coords to bot of screen
left_slope = ((ysize-bottom_ignore) - region_top_y)/(region_bot_x_left - region_top_x_left)
right_slope = ((ysize-bottom_ignore) - region_top_y)/(region_bot_x_right - region_top_x_right)
# Let's check these slopes we don't divide by 0 or inf
if abs(left_slope < .001):
left_slope = .001 if left_slope > 0 else -.001
if abs(right_slope < .001):
right_slope = .001 if right_slope > 0 else -.001
if abs(left_slope) > 1000:
left_slope = 1000 if left_slope > 0 else -1000
if abs(right_slope) > 1000:
right_slope = 1000 if right_slope > 0 else -1000
# b=y-mx
left_b = region_top_y - left_slope*region_top_x_left
right_b = region_top_y - right_slope*region_top_x_right
# x=(y-b)/m
region_bot_x_left = max(int((ysize-1-left_b)/left_slope), 0)
region_bot_x_right = min(int((ysize-1-right_b)/right_slope), xsize-1)
verts = [
(region_bot_x_left, ysize),
(region_top_x_left, region_top_y),
(region_top_x_right, region_top_y),
(region_bot_x_right, ysize)
]
return np.array([verts], dtype=np.int32)
def region_of_interest(img):
"""
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
verts = create_vertices(img)
cv2.fillPoly(mask, verts, ignore_mask_color)
#Let's return an image of the regioned area in lines
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
cv2.polylines(line_img, verts, isClosed=True, color=[0, 255, 0], thickness=5)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
return masked_image, line_img
def draw_lines(img, lines, color=[255, 0, 0], thickness=8):
"""
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
"""
if lines is None: return lines
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
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)
avg_lines = average_lines(lines, img)
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
# draw_lines(line_img, lines)
draw_lines(line_img, avg_lines, color=[138,43,226])
return line_img
def average_lines(lines, img):
'''
img should be a regioned canny output
'''
if lines is None: return lines
positive_slopes = []
positive_xs = []
positive_ys = []
negative_slopes = []
negative_xs = []
negative_ys = []
min_slope = .3
max_slope = 1000
for line in lines:
for x1, y1, x2, y2 in line:
slope = (y2-y1)/(x2-x1)
if abs(slope) < min_slope or abs(slope) > max_slope: continue # Filter our slopes
# We only need one point sample and the slope to determine the line equation
positive_slopes.append(slope) if slope > 0 else negative_slopes.append(slope)
positive_xs.append(x1) if slope > 0 else negative_xs.append(x1)
positive_ys.append(y1) if slope > 0 else negative_ys.append(y1)
# We need to calculate our region_top_y from the canny image so we know where to extend our lines to
ysize, xsize = img.shape[0], img.shape[1]
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
white = img == 255
YY[~white] = ysize*2 # Large number because we will take the min
region_top_y = np.amin(YY)
new_lines = []
if len(positive_slopes) > 0:
m = np.mean(positive_slopes)
avg_x = np.mean(positive_xs)
avg_y = np.mean(positive_ys)
b = avg_y - m*avg_x
# We have m and b, so with a y we can get x = (y-b)/m
x1 = int((region_top_y - b)/m)
x2 = int((ysize - b)/m)
new_lines.append([(x1, region_top_y, x2, ysize)])
if len(negative_slopes) > 0:
m = np.mean(negative_slopes)
avg_x = np.mean(negative_xs)
avg_y = np.mean(negative_ys)
b = avg_y - m*avg_x
# We have m and b, so with a y we can get x = (y-b)/m
x1 = int((region_top_y - b)/m)
x2 = int((ysize - b)/m)
new_lines.append([(x1, region_top_y, x2, ysize)])
return np.array(new_lines)
def weighted_img(initial_img, img, a=0.8, b=1., l=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, a, img, b, l)
def save_img(img, name):
mpimg.imsave('./images/output/{0}'.format(name if '.' in name else '{0}.png'.format(name)), img)
In [167]:
import os
image_names = [name for name in os.listdir("./images") if '.' in name]
image_names.sort()
print(image_names)
images = [mpimg.imread('./images/{0}'.format(name)) for name in image_names]
Build the pipeline and run your solution on all test images. Make copies into the test images directory, and you can use the images in your writeup report.
Try tuning the various parameters, especially the low and high Canny thresholds as well as the Hough lines parameters.
In [182]:
# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.
def detect_lines(img, debug=False):
ysize, xsize = img.shape[0], img.shape[1]
blur_gray = gaussian_blur(grayscale(img), kernel_size=5)
ht = 150 # First detect gradients above. Then keep between low and high if connected to high
lt = ht//3 # Leave out gradients below
canny_edges = canny(blur_gray, low_threshold=lt, high_threshold=ht)
if debug: save_img(canny_edges, 'canny_edges_{0}'.format(index))
# Our region of interest will be dynamically decided on a per-image basis
regioned_edges, region_lines = region_of_interest(canny_edges)
rho = 2
theta = 3*np.pi/180
min_line_length = xsize//16
max_line_gap = min_line_length//2
threshold = min_line_length//4
lines = hough_lines(regioned_edges, rho, theta, threshold, min_line_length, max_line_gap)
# Let's combine the hough-lines with the canny_edges to see how we did
overlayed_lines = weighted_img(img, lines)
# overlayed_lines = weighted_img(weighted_img(img, region_lines, a=1), lines)
if debug: save_img(overlayed_lines, 'overlayed_lines_{0}'.format(index))
return overlayed_lines
for index, img in enumerate(images):
print('Image:', index)
# debug = (True if index == 0 else False)
debug = True
detect_lines(img, debug)
You know what's cooler than drawing lanes over images? Drawing lanes over video!
We can test our solution on two provided videos:
solidWhiteRight.mp4
solidYellowLeft.mp4
Note: if you get an import error
when you run the next cell, try changing your kernel (select the Kernel menu above --> Change Kernel). Still have problems? Try relaunching Jupyter Notebook from the terminal prompt. Also, check out this forum post for more troubleshooting tips.
If you get an error that looks like this:
NeedDownloadError: Need ffmpeg exe.
You can download it by calling:
imageio.plugins.ffmpeg.download()
Follow the instructions in the error message and check out this forum post for more troubleshooting tips across operating systems.
In [15]:
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML
In [16]:
def process_image(image):
# NOTE: The output you return should be a color image (3 channel) for processing video below
# TODO: put your pipeline here,
# you should return the final output (image where lines are drawn on lanes)
return detect_lines(image)
Let's try the one with the solid white lane on the right first ...
In [183]:
white_output = './videos/output/white.mp4'
clip1 = VideoFileClip("./videos/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)
Play the video inline, or if you prefer find the video in your filesystem (should be in the same directory) and play it in your video player of choice.
In [9]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(white_output))
Out[9]:
At this point, if you were successful with making the pipeline and tuning parameters, you probably have the Hough line segments drawn onto the road, but what about identifying the full extent of the lane and marking it clearly as in the example video (P1_example.mp4)? Think about defining a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform. As mentioned previously, try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. You can see an example of the result you're going for in the video "P1_example.mp4".
Go back and modify your draw_lines function accordingly and try re-running your pipeline. The new output should draw a single, solid line over the left lane line and a single, solid line over the right lane line. The lines should start from the bottom of the image and extend out to the top of the region of interest.
Now for the one with the solid yellow lane on the left. This one's more tricky!
In [184]:
yellow_output = './videos/output/yellow.mp4'
clip2 = VideoFileClip('./videos/solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)
In [11]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(yellow_output))
Out[11]:
If you're satisfied with your video outputs, it's time to make the report writeup in a pdf or markdown file. Once you have this Ipython notebook ready along with the writeup, it's time to submit for review! Here is a link to the writeup template file.
In [178]:
challenge_output = './videos/output/challenge.mp4'
clip2 = VideoFileClip('./videos/challengeShadowCurve.mp4')
challenge_clip = clip2.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)
In [ ]:
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(challenge_output))