Finding Lane Lines with Colour Thresholds

Joshua Owoyemi
4 min readMar 3, 2017

This solution is part of a full project called Advanced Lane Finding. The 4th project of the first term of Udacity Self-Driving Car Nanodegree. For self-driving cars, being able to follow a lane is very important. So knowing where the lane lines are is very key.

In this post I will share how I am able to find lane lines from an image that represents a view from one of the cameras mounted on a car. This is perhaps the most important part of the project. A full repository of the project, including this solution can be found here:

Here is a typical image from the camera;

View from camera mounted in car.

Our objective is to be able to effectively find the lane lines in front of the car. A quick observation shows that most lane lines are either yellow or white. So we will use this information to find places in the image that are lane lines.

Images are usually represented in colour spaces, the most popular being RGB. What this means is that every pixel has a Red, Green and Blue value. So, for each pixel in the image we would have a set of numbers e.g. (255,255,255) which signifies the level of each colour component in the pixel. For RBG colour space, white is represented as (255,255,255), while black is (0,0,0).

We also have other colour spaces such as HSV, HSL, YUV. I used RGB and HSL in this project, so I will be using them to explain.

We know that white is represented by (255,255,255) in both RGB and HSL colour space, but we have to ascertain the values for the yellow lane colour. To do this, I pulled out colour picker from the Inkscape software, just to have a visual representation.

Values for white in RGB and HSL

Playing around with the values we can find the values that corresponds to the yellow we are looking for. You can ignore the A (Alpha) value, its not necessary here.

Values for Yellow in RGB and HSL

The idea, however is to use a range that covers the yellow colour spectrum and also cover the white colour spectrum. So I chose the following values after experimenting with the ranges.

Colour and Threshold values

Next is to implement this with python and some opencv functions. Below is the snippet of the function that performs this operation.

Taking RGB threshold as an example. We define lower and upper threshold values, then we create a mask using opencv cv2.inRange() function. Next, we remove pixels that are not in the range using cv2.bitwise_and() function. At this point the image is still in RGB or HLS colour space, so we convert to grayscales and then change all the remaining pixels to binary value of 1 (bin_it() function). Find below result for each colour thresholds.

Result of RGB white colour thresholds
Result of RGB yellow colour thresholds. Note that it also recognizes the white lanes. It is however not as robust as the first one.
Result of HLS white colour thresholds. This is very robust to only Yellow colours.

Lastly, we combine these individual results into one for a more robust solution.

Combined threshold result
Summary of solution

The rest of the project involves using this result to plot and fit lane lines onto the road. Find below a video of the final result.

If you have any better ideas, please share in the comment below.

Thanks for reading.

--

--