Street Lanes Finder - Detecting Street Lanes for Self-Driving Cars 🚗

Lanes Detection with Computer Vision

In today’s article, we are going to use basic Computer Vision techniques to approach the street lanes detection problem which is crucial for self-driving cars. By the end of this article, you will be able to perform real-time lane detection with Python and OpenCV.

Real-Time Lane Detection

Implementation

You can find the full codebase for this project on GitHub and I encourage you to check it and follow along.

Let’s start with defining our problem.

Given an image of the road, we would like to detect street lanes on it.

In order to do it, let’s provide an image path and load it with OpenCV, then let’s invoke find_street_lanes pipeline with it.

test_image = cv2.imread(INPUT_FOLDER + TEST_IMAGE)
street_lanes = find_street_lanes(test_image)

And this is how our find_street_lanes pipeline looks like

Grayscale

The first step of our pipeline is an image conversion from color to grayscale. We do it because in our case color values don’t hold any valuable information so to make further processing simpler and faster, we convert from three channels to one.

grayscale = (r + g + b) / 3

Blur

Then, to make our image smoother and less noisy, we apply a gentle Gaussian Blur.

Gaussian Blur works by calculating every pixel’s value as a weighted average of the surrounding pixels.

Canny (Edge Detection)

This is where the fun part begins. Now having a smooth grayscale image, we need to detect edges within it.

FYI, it’s called ‘canny’ because it was invented by John Canny.

Without going into too many details, the core part of the canny edge detector is based on scanning the image and calculating derivatives (gradients) of neighboring pixel values. The higher the gradient, the more likely it is an edge.

An Example of a High Gradient Area

Region of Interest

Now, having our edges detected, we can clearly see where our street lanes are, but besides that, we can also see other edges that are redundant. In order to get rid of them, we should mask our image to a specific region that’s called a Region of Interest (ROI). Defining proper ROI is highly dependant on the camera calibration and its frame i.e what part of the road is visible.

This the defined mask where the white color shows ROI

and this is canny image with a masked region of interest.

Hough Lines

Now we have well defined lines that show where our street lanes are located. However, presenting them on the screen wouldn’t look appealing as they are noisy andflickery. In order to visualize them as single lines, we need to perform hough lines transformation.

  1. Firstly, let’s detect all lines. We define a line as [x1,y1,x2,y2] where (x1, y1) is its start and (x2, y2) is its end.
  2. Having our lines localized, we can calculate their slopes to determine whether they are right or left ones.

3. Then we need to average lines and derive single left and right line.

4. Ultimately, we can draw our final lines.

Image Overlay

Finally, we need to overlay our input image with derived lines.

Real-Time Detection

Having a pipeline that can detect lines for a single frame, we can run it real-time on a video stream performing detections on every frame.

What’s Next?

In this project, we’ve learned how to use basic Computer Vision techniques for real-life problems. Even though our results look very promising, street lanes detector is far from perfection and it might fail in some cases. That is why in the next part we are going to use Deep Learning approach (Holistically-Nested Edge Detection) to achieve better accuracy and more reliable detector. Stay tuned!

Questions? Comments? Feel free to leave your feedback in the comments section or contact me directly at https://gsurma.github.io.

And don’t forget to 👏 if you enjoyed this article 🙂.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store