1 minute read

Introduction

One of the key technologies behind self-driving is image processing. In this post, we will use OpenCV in Python to process a road-driving video and generate a new video with the detected lane lines overlaid on it.

Before we start, please note that the code snippets in this post are fragmentary and incomplete. If you would like to see the full source code, visit the GitHub repository here.

Display the Image

Image

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# reading in an image
image = mpimg.imread('solidWhiteCurve.jpg')

# printing out some stats and plotting the image
print('This image is:', type(image), 'with dimensions:', image.shape)
plt.imshow(image)
plt.show()

Convert the Image to Grayscale

Image

To detect the lane lines, we don’t need colors. Therefore, we convert the image to grayscale.

import numpy as np
import cv2
import math
from moviepy import VideoFileClip
from IPython.display import HTML

...

gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

Although the color image of the displayed result is not gray, you don’t need to worry. It is not an error. It is simply the default colormap used by the library. you can display it in ‘true’ grayscale, but it is not necessary.

Detect the Edges

Image

Next, we need to detect the edges in the image, where the pixel intensity(brightness) changes rapidly. For this, we use the Canny Edge detection algorithm. Fortunately, we don’t need to understand the mathematical details for it. OpenCV already implements this algorithm.

cannyed_image = cv2.Canny(gray_image, 100, 200)

Crop the Image to a Triangular Region

Image

Draw Red Lines over the Lane Markings

Image

Connect the Line Segments

Image

Apply to a Video

In this step, we apply the what we learned to a video. You can download the unedited video here.

Conclusion

In this post, we generate the video with detected lane lines overlaid on it. However, real self-driving car must detect lane lines and make driving decision on them in real time.

We will implement this process by real self-drving mini-car built with Raspberry Pi.

Source

https://medium.com/@mrhwick/simple-lane-detection-with-opencv-bfeb6ae54ec0 https://github.com/udacity/CarND-LaneLines-P1/blob/master/test_videos/solidWhiteRight.mp4

Updated:

Leave a comment