In [1]:
%%HTML
<style> code {background-color : lightblue !important;} </style>



In [2]:
#Run the code in the cell below to extract object points 
#and image points for camera calibration.
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

In [3]:
nx = 9 #inside x corners
ny = 6 #inside y corners

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((nx*ny,3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.

# Make a list of calibration images
images = glob.glob('camera_cal/calibration*.jpg')

# Step through the list and search for chessboard corners
for idx, fname in enumerate(images):
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    # Find the chessboard corners
    ret, corners = cv2.findChessboardCorners(gray, (nx,ny), None)

    # If found, add object points, image points
    if ret == True:
        objpoints.append(objp)
        imgpoints.append(corners)    
    else:
        print('findChessboardCorners failed:', fname)


findChessboardCorners failed: camera_cal/calibration1.jpg
findChessboardCorners failed: camera_cal/calibration4.jpg
findChessboardCorners failed: camera_cal/calibration5.jpg

In [4]:
def get_undistorted_img(img, mtx, dist):
    undistorted_img = cv2.undistort(img, mtx, dist, None, mtx)
    return undistorted_img

In [5]:
#If the above cell ran sucessfully, you should now have objpoints and imgpoints needed for camera calibration. 
#Run this cell  to calibrate, calculate distortion coefficients, and test undistortion on an image!

# Test undistortion on an image
img = cv2.imread('camera_cal/calibration1.jpg')
img_size = (img.shape[1], img.shape[0])

#Caliberate the camera and compute the camera matrix and distortion coefficients
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)

undistorted_img = get_undistorted_img(img, mtx, dist)
cv2.imwrite('camera_cal/pattern.jpg',undistorted_img)

# Visualize undistortion
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
ax1.imshow(img)
ax1.set_title('Original Image', fontsize=30)
ax2.imshow(undistorted_img)
ax2.set_title('Undistorted Image', fontsize=30)

plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
#plt.savefig("combined_signs_vehicles_xygrad.png")
plt.show()



In [6]:
# 1) Convert to grayscale
def get_grayscale_img(image):
    grayscale_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) #since image was read using mpimg.imread()
    return grayscale_img
    #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if image was read using cv2.imread()

In [7]:
def abs_sobel_thresh(gray_img, orient='x', sobel_kernel=3, thresh=(0, 255)):
    # 2) Take the derivative in x or y given orient = 'x' or 'y'
    if orient == 'x':
        sobel_value = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    elif orient == 'y':
        sobel_value = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    else:
        print("You must specify an orientation - x or y")
    # 3) Take the absolute value of the derivative or gradient
    abs_sobel = np.absolute(sobel_value)
    # 4) Scale to 8-bit (0 - 255) then convert to type = np.uint8
    max_sobel = np.max(abs_sobel)
    scaled_sobel = np.uint8(255*abs_sobel/max_sobel)
    # 5) Create a mask of 1's where the scaled gradient magnitude 
    # is > thresh_min and < thresh_max
    grad_binary = np.zeros_like(scaled_sobel)
    # 6) Return this mask as your binary_output image
    grad_binary[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
    #grad_binary = np.copy(img) # Remove this line
    return grad_binary

In [8]:
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
    # 2) Take the gradient in x and y separately
    sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # 3) Calculate the gradient magnitude 
    magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
    # 4) Scale to 8-bit (0 - 255) and convert to type = np.uint8
    scaled_8bit_magnitude = np.uint8(255*magnitude/np.max(magnitude))
    # 5) Create a binary mask where mag thresholds are met
    mag_binary = np.zeros_like(scaled_8bit_magnitude)
    mag_binary[(scaled_8bit_magnitude >= mag_thresh[0]) & (scaled_8bit_magnitude <= mag_thresh[1])] = 1
    # 6) Return this mask as your binary_output image
    #mag_binary = np.copy(img) # Remove this line
    return mag_binary

In [9]:
def dir_threshold(gray_img, sobel_kernel=3, thresh=(0, np.pi/2)):
    # 2) Take the gradient in x and y separately
    sobel_x = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobel_y = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # 3) Take the absolute value of the x and y gradients
    abs_sobel_x = np.absolute(sobel_x)
    abs_sobel_y = np.absolute(sobel_y)
    # 4) Use np.arctan2(abs_sobely, abs_sobelx) to calculate the direction of the gradient 
    gradient_dir = np.arctan2(abs_sobel_y, abs_sobel_x)
    gradient_dir = np.absolute(gradient_dir)
    # 5) Create a binary mask where direction thresholds are met
    dir_binary = np.zeros_like(gradient_dir)
    dir_binary[(gradient_dir >= thresh[0]) & (gradient_dir <= thresh[1])] = 1
    # 6) Return this mask as your binary_output image
    #dir_binary = np.copy(img) # Remove this line
    return dir_binary

In [10]:
K_SIZE = 17 # Choose a larger odd number to smooth gradient measurements

In [11]:
def get_hls_image(img): 
    #img = np.copy(img) 
    #hls_img = cv2.cvtColor(img, cv2.COLOR_BGR2HLS).astype(np.float)
    hls_img = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float) 
    return hls_img

In [12]:
def get_binary_img(img, s_thresh=(170, 200), sx_thresh=(10, 200)): #s_thresh = 170, 200 and sx_thresh = 20,100
    
    #hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS).astype(np.float) 
    hls = get_hls_image(img)
    h_channel = hls[:,:,0]
    l_channel = hls[:,:,1] 
    s_channel = hls[:,:,2] 
    
    sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0) # Take the derivative in x 
    abs_sobelx = np.absolute(sobelx) # Absolute x derivative to accentuate lines away from horizontal 
    scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx)) 
    
    # Threshold x gradient 
    sxbinary = np.zeros_like(scaled_sobel) 
    sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1 
    
    # Threshold color channel 
    s_binary = np.zeros_like(s_channel) 
    s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1 

    # Stack each channel
    # Note color_binary[:, :, 0] is all 0s, effectively an all black image. It might 
    # be beneficial to replace this channel with something else. 
    color_binary = np.dstack(( np.zeros_like(sxbinary), sxbinary, s_binary))
    
    # Combine the two binary thresholds
    combined_binary_img = np.zeros_like(sxbinary)
    combined_binary_img[(s_binary == 1) | (sxbinary == 1)] = 1
 
    ##height, width = gray.shape
    
    img_height, img_width, channel_num = img.shape
    '''
    print(img_height)
    print(img_width)
    print(channel_num)
    '''
    
    # region_of_interest (ROI) mask
    roi_mask = np.zeros_like(combined_binary_img)
    roi_vertices = np.array([[0,img_height-1], [img_width/2, int(0.5*img_height)], [img_width-1, img_height-1]], dtype=np.int32)
    #print("region_of_interest_vertices", roi_vertices)
    cv2.fillPoly(roi_mask, [roi_vertices], 1)
    
    masked_binary_img = cv2.bitwise_and(combined_binary_img, roi_mask)
    
    return masked_binary_img
    '''
    return color_binary_img, combined_binary_img 
    '''

In [13]:
img = mpimg.imread('test_images/straight_lines1.jpg')
img_size = img.shape
print(img_size)

sx_thresh_min = 20
sx_thresh_max = 100
sx_thresh = (sx_thresh_min, sx_thresh_max)

s_thresh_min = 170
s_thresh_max = 255
s_thresh = (s_thresh_min, s_thresh_max)

l_thresh_min = 10
l_thresh_max = 125
l_thresh = (l_thresh_min, l_thresh_max)

#color_binary_img, combined_binary_img = get_binary_img(img, s_thresh, sx_thresh) 
#color_binary_img, combined_binary_img = get_binary_img2(img, s_thresh, sx_thresh, l_thresh) 
masked_binary_img = get_binary_img(img, s_thresh, sx_thresh)


(720, 1280, 3)

In [14]:
# Plot the result
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(img)
ax1.set_title('Original Image', fontsize=30)
ax2.imshow(masked_binary_img, cmap='gray')
ax2.set_title('Combined Binary Masked Image', fontsize=30)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
plt.show()



In [15]:
src_bottom_left = [220,720]
src_bottom_right = [1110, 720]
src_top_left = [570, 470]
src_top_right = [720, 470]

# Destination points are chosen such that straight lanes 
# appear more or less parallel in the transformed image.
dest_bottom_left = [320,720]
dest_bottom_right = [920, 720]
dest_top_left = [320, 1]
dest_top_right = [920, 1]

'''
src = np.float32(
     [[(img_size[0] / 2) - 55, img_size[1] / 2 + 100],
     [((img_size[0] / 6) - 10), img_size[1]],
     [(img_size[0] * 5 / 6) + 60, img_size[1]],
     [(img_size[0] / 2 + 55), img_size[1] / 2 + 100]])
dst = np.float32(
     [[(img_size[0] / 4), 0],
     [(img_size[0] / 4), img_size[1]],
     [(img_size[0] * 3 / 4), img_size[1]],
     [(img_size[0] * 3 / 4), 0]])

print(src)
print(dst)
'''


Out[15]:
'\nsrc = np.float32(\n     [[(img_size[0] / 2) - 55, img_size[1] / 2 + 100],\n     [((img_size[0] / 6) - 10), img_size[1]],\n     [(img_size[0] * 5 / 6) + 60, img_size[1]],\n     [(img_size[0] / 2 + 55), img_size[1] / 2 + 100]])\ndst = np.float32(\n     [[(img_size[0] / 4), 0],\n     [(img_size[0] / 4), img_size[1]],\n     [(img_size[0] * 3 / 4), img_size[1]],\n     [(img_size[0] * 3 / 4), 0]])\n\nprint(src)\nprint(dst)\n'

In [16]:
def get_warped_img(masked_binary_img):
    # Vertices extracted manually for performing a perspective transform

    src = np.float32([src_bottom_left, src_bottom_right, src_top_right, src_top_left])
    dst = np.float32([dest_bottom_left, dest_bottom_right, dest_top_right, dest_top_left])

    M = cv2.getPerspectiveTransform(src, dst)
    M_inv = cv2.getPerspectiveTransform(dst, src)

    img_size = (masked_binary_img.shape[1], masked_binary_img.shape[0])

    warped_img = cv2.warpPerspective(masked_binary_img, M, img_size , flags=cv2.INTER_LINEAR)
    
    return warped_img, M, M_inv

In [17]:
def plot_warped_img(warped_img):  
    
    plot_pts = np.array([src_bottom_left, src_bottom_right, src_top_right, src_top_left], np.int32)
    plot_pts = plot_pts.reshape((-1,1,2))
    img_copy = img.copy()
    cv2.polylines(img_copy,[plot_pts],True,(255,0,0), thickness=3)
    
    plot_pts = np.array([dest_bottom_left, dest_bottom_right, dest_top_right, dest_top_left], np.int32)
    plot_pts = plot_pts.reshape((-1,1,2))
    warped_img_copy = warped_img.copy()
    cv2.polylines(warped_img_copy,[plot_pts],False,(255,0,0), thickness=3)

    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
    f.tight_layout()
    ax1.imshow(img_copy)
    ax1.set_title('Original Image', fontsize=30)
    ax2.imshow(warped_img, cmap='gray')
    ax2.set_title('Warped Image', fontsize=30)
    plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)

In [18]:
warped_img, M, M_inv = get_warped_img(masked_binary_img)
#warped_img = get_warped_img(img)
plot_warped_img(warped_img)



In [19]:
def get_histogram_peaks(binary_warped_img):
    histogram = np.sum(binary_warped_img[binary_warped_img.shape[0]//2:,:], axis=0)
    return histogram

In [20]:
peakPixels = get_histogram_peaks(warped_img) #find peaks
# Peak in the first half indicates the likely position of the left lane
midpoint = np.int(peakPixels.shape[0]//2)
leftx_peak = np.argmax(peakPixels[:midpoint])

# Peak in the second half indicates the likely position of the right lane
rightx_peak = np.argmax(peakPixels[midpoint:]) + midpoint
print(leftx_peak, rightx_peak)


307 912

In [21]:
plt.plot(peakPixels)


Out[21]:
[<matplotlib.lines.Line2D at 0x11d94b278>]

In [22]:
global left_fit 
global right_fit 
left_fit = []
right_fit = []

In [23]:
def findAndPlotLanePixels(warped_img): 
    # Assuming you have created a warped binary image called "binary_warped"
    # Take a histogram of the bottom half of the image
    histogram = np.sum(warped_img[warped_img.shape[0]//2:,:], axis=0)
    # Create an output image to draw on and  visualize the result
    out_img = np.dstack((warped_img, warped_img, warped_img))*255
    # Find the peak of the left and right halves of the histogram
    # These will be the starting point for the left and right lines
    midpoint = np.int(histogram.shape[0]/2)
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # Choose the number of sliding windows
    nwindows = 9
    # Set height of windows
    window_height = np.int(warped_img.shape[0]/nwindows)
    # Identify the x and y positions of all nonzero pixels in the image
    nonzero = warped_img.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    # Current positions to be updated for each window
    leftx_current = leftx_base
    rightx_current = rightx_base
    # Set the width of the windows +/- margin
    margin = 100
    # Set minimum number of pixels found to recenter window
    minpix = 50
    # Create empty lists to receive left and right lane pixel indices
    left_lane_inds = []
    right_lane_inds = []

    # Step through the windows one by one
    for window in range(nwindows):
        # Identify window boundaries in x and y (and right and left)
        win_y_low = warped_img.shape[0] - (window+1)*window_height
        win_y_high = warped_img.shape[0] - window*window_height
        win_xleft_low = leftx_current - margin
        win_xleft_high = leftx_current + margin
        win_xright_low = rightx_current - margin
        win_xright_high = rightx_current + margin
        # Draw the windows on the visualization image
        cv2.rectangle(out_img,(win_xleft_low,win_y_low),(win_xleft_high,win_y_high),(0,255,0), 2) 
        cv2.rectangle(out_img,(win_xright_low,win_y_low),(win_xright_high,win_y_high),(0,255,0), 2) 
        # Identify the nonzero pixels in x and y within the window
        good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
        good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]
        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)
        # If you found > minpix pixels, recenter next window on their mean position
        if len(good_left_inds) > minpix:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
        if len(good_right_inds) > minpix:        
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    # Concatenate the arrays of indices
    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)

    # Extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds] 
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds] 

    # Fit a second order polynomial to each
    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)
    
    '''
    print("left_fit", left_fit)
    print("right_fit", right_fit)
    '''
    
    # Generate x and y values for plotting
    ploty = np.linspace(0, warped_img.shape[0]-1, warped_img.shape[0] )
    left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
    right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
    
    out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0]
    out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255]
    plt.imshow(out_img)
    plt.plot(left_fitx, ploty, color='yellow')
    plt.plot(right_fitx, ploty, color='yellow')
    plt.xlim(0, 1280)
    plt.ylim(720, 0)
    
    return left_fit, right_fit

In [24]:
left_fit, right_fit = findAndPlotLanePixels(warped_img)



In [25]:
print(left_fit)
print(right_fit)


[ -2.31393329e-05   1.93677423e-02   3.08931807e+02]
[  5.26823500e-05  -2.68228731e-02   9.12351672e+02]

In [26]:
#Using lanes that have already been detected to find lane pixels
#This way you do not have to search for sliding window again once the lane is detected
# Assume you now have a new warped binary image 
# from the next frame of video (also called "binary_warped" or "warped_image")
# It's now much easier to find line pixels!

def findAndPlotNextLanePixels(warped_img, left_fit, right_fit):
    nonzero = warped_img.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    margin = 100
    left_lane_inds = ((nonzerox > (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] - margin)) & (nonzerox < (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] + margin))) 
    right_lane_inds = ((nonzerox > (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] - margin)) & (nonzerox < (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] + margin)))  
    
    # Again, extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds] 
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]
    # Fit a second order polynomial to each
    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)
    
    # Generate x and y values for plotting
    ploty = np.linspace(0, warped_img.shape[0]-1, warped_img.shape[0] )
    left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
    right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
    
    #Create an image to draw on and an image to show the selection window
    out_img = np.dstack((warped_img, warped_img, warped_img))*255
    window_img = np.zeros_like(out_img)
    # Color in left and right line pixels
    out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0]
    out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255]

    # Generate a polygon to illustrate the search window area
    # And recast the x and y points into usable format for cv2.fillPoly()
    left_line_window1 = np.array([np.transpose(np.vstack([left_fitx-margin, ploty]))])
    left_line_window2 = np.array([np.flipud(np.transpose(np.vstack([left_fitx+margin, ploty])))])
    left_line_pts = np.hstack((left_line_window1, left_line_window2))
    right_line_window1 = np.array([np.transpose(np.vstack([right_fitx-margin, ploty]))])
    right_line_window2 = np.array([np.flipud(np.transpose(np.vstack([right_fitx+margin, ploty])))])
    right_line_pts = np.hstack((right_line_window1, right_line_window2))
    
    # Draw the lane onto the warped blank image
    cv2.fillPoly(window_img, np.int_([left_line_pts]), (0,255, 0))
    cv2.fillPoly(window_img, np.int_([right_line_pts]), (0,255, 0))
    result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
    
    
    plt.imshow(result)
    plt.plot(left_fitx, ploty, color='yellow')
    plt.plot(right_fitx, ploty, color='yellow')
    plt.xlim(0, 1280)
    plt.ylim(720, 0)

    
    '''
    print("ploty",ploty)
    print("left_fitx", left_fitx)
    print("right_fitx", right_fitx)
    '''
    
    return ploty, left_fitx, right_fitx

In [27]:
ploty, left_fitx, right_fitx = findAndPlotNextLanePixels(warped_img, left_fit, right_fit)
'''
print("ploty",ploty)
print("left_fitx", left_fitx)
print("right_fitx", right_fitx)
'''


Out[27]:
'\nprint("ploty",ploty)\nprint("left_fitx", left_fitx)\nprint("right_fitx", right_fitx)\n'

In [ ]:
def get_radius_of_curvature(ploty, left_fitx, right_fitx):
    # Define conversions in x and y from pixels space to meters
    ym_per_pix = 30/720 # meters per pixel in y dimension
    xm_per_pix = 3.7/700 # meters per pixel in x dimension
    # Define y-value where we want radius of curvature
    # I'll choose the maximum y-value, corresponding to the bottom of the image
    y_eval = np.max(ploty)

    # Fit new polynomials to x,y in world space
    left_fit_cr = np.polyfit(ploty*ym_per_pix, left_fitx*xm_per_pix, 2)
    right_fit_cr = np.polyfit(ploty*ym_per_pix, right_fitx*xm_per_pix, 2)
    # Calculate the new radii of curvature
    left_curve_rad = ((1 + (2*left_fit_cr[0]*y_eval*ym_per_pix + left_fit_cr[1])**2)**1.5) / np.absolute(2*left_fit_cr[0])
    right_curve_rad = ((1 + (2*right_fit_cr[0]*y_eval*ym_per_pix + right_fit_cr[1])**2)**1.5) / np.absolute(2*right_fit_cr[0])
    # Now our radius of curvature is in meters
    #print(left_curverad, 'm', right_curverad, 'm')
    # Example values: 632.1 m    626.2 m
    return left_curve_rad, right_curve_rad

In [ ]:
left_curve_rad, right_curve_rad = get_radius_of_curvature(ploty, left_fitx, right_fitx)
print(left_curve_rad, 'm', right_curve_rad, 'm')
print("average radius of curvature (in meters): ", ((left_curve_rad + right_curve_rad)/2) )

In [ ]:
def draw_driving_area(img, warped_img, M_inv, ploty, left_fitx, right_fitx):
    # Create an image to draw the lines on
    warp_zero = np.zeros_like(warped_img).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
    pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (0,255, 0)) #R=0;G=255;B=0

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, M_inv, (img.shape[1], img.shape[0])) 
    # Combine the result with the original image
    result = cv2.addWeighted(img, 1, newwarp, 0.3, 0)
    #result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)
    
    return result

In [ ]:
print("img", img)
print("warped_img", warped_img)
print("M_inv", M_inv)
print("ploty", ploty)
print("left_fitx", left_fitx)
print("right_fitx", right_fitx)

In [ ]:
result = draw_driving_area(img, warped_img, M_inv, ploty, left_fitx, right_fitx)

In [ ]:
plt.imshow(result)
plt.show()

In [ ]:
def define_thresholds():
    sx_thresh_min = 20
    sx_thresh_max = 100
    sx_thresh = (sx_thresh_min, sx_thresh_max)

    s_thresh_min = 170
    s_thresh_max = 255
    s_thresh = (s_thresh_min, s_thresh_max)

    l_thresh_min = 10
    l_thresh_max = 125
    l_thresh = (l_thresh_min, l_thresh_max)

In [ ]:
def define_src_and_dst_for_warping():
    src_bottom_left = [220,720]
    src_bottom_right = [1110, 720]
    src_top_left = [570, 470]
    src_top_right = [720, 470]

    # Destination points are chosen such that straight lanes appear more or less parallel in the transformed image.
    dest_bottom_left = [320,720]
    dest_bottom_right = [920, 720]
    dest_top_left = [320, 1]
    dest_top_right = [920, 1]

In [ ]:
def adv_lane_detection_pipeline(img):
    
    #define_thresholds
    define_thresholds()
    
    #get_masked_img
    masked_binary_img = get_binary_img(img, s_thresh, sx_thresh)    
    
    #define_src_and_dst_for_warping
    define_src_and_dst_for_warping()
    
    #get_warped_img
    warped_img, M, M_inv = get_warped_img(masked_binary_img)
    
    #get_histogram_peaks
    peakPixels = get_histogram_peaks(warped_img) #find peaks
    # Peak in the first half indicates the likely position of the left lane
    midpoint = np.int(peakPixels.shape[0]//2)
    leftx_peak = np.argmax(peakPixels[:midpoint])

    # Peak in the second half indicates the likely position of the right lane
    rightx_peak = np.argmax(peakPixels[midpoint:]) + midpoint

    #global left_fit 
    #global right_fit 
    left_fit = []
    right_fit = []
    
    #findAndPlotLanePixels
    left_fit, right_fit = findAndPlotLanePixels(warped_img)
    
    #findAndPlotNextLanePixels
    ploty, left_fitx, right_fitx = findAndPlotNextLanePixels(warped_img, left_fit, right_fit)
    
    #get_radius_of_curvature
    left_curve_rad, right_curve_rad = get_radius_of_curvature(ploty, left_fitx, right_fitx)

    #draw_driving_area
    result = draw_driving_area(img, warped_img, M_inv, ploty, left_fitx, right_fitx)
    
    return result

In [ ]:
img = mpimg.imread('test_images/test2.jpg')
result = adv_lane_detection_pipeline(img)
plt.imshow(result)
plt.show()

In [ ]:
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

In [ ]:
video_output = 'adv_lane_detection_output.mp4'
clip1 = VideoFileClip("project_video.mp4")
lane_clip = clip1.fl_image(adv_lane_detection_pipeline) #NOTE: this function expects color images!!
%time lane_clip.write_videofile(video_output, audio=False)