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.

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.


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.
- 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.
- 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!
Donât forget to check the projectâs GitHub page.
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 đ.
