Street Lanes Finder - Detecting Street Lanes for Self-Driving Cars 🚗
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.
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
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
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.
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.
Finally, we need to overlay our input image with derived lines.
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.
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 🙂.