How a 13-year-old told a Computer to Detect Lane Lines

Rishi Mehta
The Startup
Published in
6 min readOct 29, 2019

--

A world in 100 years. Something crazy to think about. I say that because 100 years ago no one would have ever believed that society has become what it is. Looking into the future, you might think of vehicles that can hover and drive or fly on their own. And well, you're not wrong.

Those autonomous vehicles aren't just going to be a core part of our future, but they are already starting to make a name for themselves. Companies like Tesla and Waymo are disrupting the industry at an incredible rate. Seeing all this happen right in front of me, I couldn’t just observe it anymore. No, it was time to start doing something.

I then realized that similar to anything else, if you want to disrupt an industry, you have to get good at all the aspects that are being used to disrupt that industry. For me, the first aspect I wanted to get good at was lane detection. For cars especially, being able to identify lane lines is essential to make sure that the passengers in the car and everyone else on the road are safe.

Observing the road

In order to detect lanes, there has to be some way of observing them. This is done by using a simple camera that is placed somewhere in the front of the car. That camera records a live video of the road that is in front of them. Then that same video is broken into frames. Then every single frame is analyzed using the rest of the algorithm and then identifies the lane.

This is an example of a video being broken down into individual images

Identifying edges

Converting the image to greyscale

The end goal is for our image to be made up of edges that mark the lane lines. The first step in this process is converting the image to greyscale, this way the image is essentially black and white. But that raises the question, why can’t we just keep the image that is in colour?

The reason is simplicity and speed. When dealing with colour images, it’s much more complicated than a greyscale image. Colour images have 3 different channels, red, green and blue. Each of these colours has a value from 0–255. While black and white images only have one channel. The more channels there are the longer it takes to process the image.

Reducing noise

After the image is converted to greyscale we now have to reduce all image noise. This is essentially where images have too many details, thus being really hard to read. To do that, we have to use something called the gaussian blur.

The Gaussian Blur being applied to my sample image

How does it work? In order to answer that we first need to understand that every single image is made up of pixels. Now, in a grayscale image, each pixel is represented by one number from 0–255, zero being black and 255 being white. Then it will essentially average out the value of a pixel with the average value of the pixels around it. Averaging out the pixels in the image to reduce noise will be done with the kernel.

The kernel is run across our entire image and sets each pixel about equal to the weighted average of the neighboring pixels thus smoothing our image. What we’re doing is applying a gaussian blur on a greyscale image with a 5 by 5 kernel. It’s important to remember the size of the kernel is dependent on specific situations, a 5 by 5 kernel is a good size for most cases.

Finding the edges

A computer doesn't analyze the black and white image just by looking at the picture. Instead to detect lane lines the computer draws edges in the given image. As I mentioned, every pixel in a grayscale image is represented by a number value.

In order to find an edge, the computer looks at groups of pixels. If it realizes that there is a big difference in pixel value, the computer will draw an edge. Repeating this process multiple times is how it can produce a canny:

This is what a canny looks like in our sample image

Taking out the unwanted edges

The issue now is that we have too many edges. This makes it significantly harder to find the lane. If you looked at the canny map above, you wouldn’t be able to tell what is what for the most part.

What we have to do is specify what edges we want. If we specify that we only want the lanes in the picture, the computer will process the image much faster, thus being more safe and efficient. The output would look something like this:

This is the result of taking out the unwanted edges

Using the “cv2.bitwise_and`” functions, we can find overlaps between the mask(the image that only has our lines of interest) and the canny image. By using the “and” operator, it can check whether a pixel in the mask and the canny image have a binary value of 1 where white is equal to 1 and black equals 0.

If the same pixels on both the mask and the canny equal 1(which means they are white) then that means that is in the region of interest. All other pixels becomes 0(which means they are black).

Hough transform function

Now the computer knows exactly what lines we care about. But it's still not clear where the lanes are. What we want is a geometrical representation of the lanes. But right now the “lanes” are just a sequence of pixels. We can loop through all pixels, and somehow figure out the slope and intercept, but that is completely unnecessary.

So we need a mechanism that gives more weight to pixels that are already in a line. Oh look at that, this is exactly what the Hough Transform Function does, can you believe our luck? This function lets each point on the image “vote”.

Because of the mathematical properties of the transform, this “voting” allows us to figure out prominent lines in the image. After this process, it highlights the lane lines in a purple/blue colour, thus completing our algorithm. This highlights the lanes in the image:

Reflecting

After I finished my algorithm, I was pretty happy with myself. No, that’s just me trying to be humble, I was absolutely ecstatic. In a way, there was so much more to it than just coding. Every time I make something using computer programming, I feel a bigger sense of accomplishment compared to if I would get perfect on a test or do really well on any presentation.

Now I’m starting to realize that the reason for that is the journey. Since I’ve been coding there are very few things that get me more frustrated. I noticed that I was so happy because of all the frustrating times where I said: “why can you just work”? It really opened my eyes to show me that it’s worth it in the end.

I feel as if now more than ever my life is really accelerating. So much has happened in the past two months and I can’t describe how excited I am to keep learning and growing. Only after experiencing it, it really internalized with me that:

“The Greater The Struggle The Greater The Reward”

--

--

Rishi Mehta
The Startup

17 y/o working on building a fall detection system for seniors | fallyx.com