Inspired by this lane detection program written in Python.
cmake . && make
./video path-to-video
Example: ./video videos/dashcam1.mp4
./image path-to-image
Example: ./image images/road1.png
-
Filter out pixels that are not yellow or white (or gray if video is taken during the night/evening).
-
Convert image to grayscale and apply Gaussian blur. This is an important step since it reduces noise, and the algorithm for detecting edges (Canny Edge Detection) is susceptible to noise.
-
Use the Canny Edge Detection algorithm to detect edges. We're interested in the edges of the lanes.
-
Find the region of interest. For this program that means creating a mask with the use of two trapezoids (a big one and a smaller one that goes inside the big one). We get the following result after applying the mask on the canny image:
-
Detect lines using the Hough Line Transform. HLT is used to detect straight lines. Every detected line is made up of two points (starting and ending point). Here are the detected lines drawn:
-
As you can see in the picture above, the right and left lane consist of multiple detected lines and as said before, a line is just two points. Our goal is to form two lines from those points, one line for the right lane and one for the left lane. This brings us to the fun part: using linear regression to find the line of best fit through the points. To do that, we first filter out the points that form a line with a slope below a certain threshold, then we isolate the left points from the right points and finally we use linear regression to obtain two well-fitting lines. And to make it a bit more clear, we fill in the space between the lines with a lighter color. Result:
This program is very basic. It can only be used for detecting straight (and slightly curved) lanes. Furthermore, the dashcam videos and images used while creating this program all have a size of 1280x960 and they were all taken from the same perspective (the built-in front camera of my car). If you want to work with videos/images taken from a different perspective and/or with a different size, the parameters used for calculating the region of interest most likely have to be changed. One other thing important to note is that this program does not work well in situations where the light makes it difficult to distinguish the lanes from the surroundings. Consider these images:
Example #1 | Example #2 |
---|---|
The setting sun makes it difficult to see the lanes clearly, the camera is almost completely blinded by it. In these types of situations this program won't be able to detect the lanes.