Thresholding

In [1]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline


image = mpimg.imread("./test_images/test1.jpg")

# Edit this function to create your own pipeline.
def pipeline(img, s_thresh=(170, 255), sx_thresh=(20, 100)):
    img = np.copy(img)
    # Convert to HSV color space and separate the V channel
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float)
    l_channel = hsv[:,:,1]
    s_channel = hsv[:,:,2]
    # Sobel x
    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

    #combine
    color_binary = np.zeros_like(scaled_sobel)
    color_binary[(sxbinary == 1) | (s_binary ==1)] = 1
    return color_binary
    
result = pipeline(image)
In [2]:
plt.imshow(image)
Out[2]:
<matplotlib.image.AxesImage at 0x102f27d68>
In [3]:
plt.imshow(result, cmap="gray")
Out[3]:
<matplotlib.image.AxesImage at 0x10b798da0>

Birds Eye View

In [4]:
def warper(img):
    img_size = (img.shape[1], img.shape[0])
    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]])
    M = cv2.getPerspectiveTransform(src, dst)
    Minv = cv2.getPerspectiveTransform(dst, src)
    warped = cv2.warpPerspective(img, M, img_size, flags=cv2.INTER_NEAREST)  # keep same size as input image

    return warped, Minv
In [5]:
image = mpimg.imread("./test_images/straight_lines1.jpg")
plt.imshow(image)
Out[5]:
<matplotlib.image.AxesImage at 0x116ef1ac8>
In [6]:
warped, _ = warper(image)
plt.imshow(warped)
Out[6]:
<matplotlib.image.AxesImage at 0x116f61780>

Polynomial

In [7]:
image = mpimg.imread("./test_images/test2.jpg")
warped, Minv = warper(image)
binary_warped, _ = warper(pipeline(image))
plt.imshow(image)
Out[7]:
<matplotlib.image.AxesImage at 0x1100ab9b0>