Creating a Lane Detection Program Using OpenCV-Python

Kerry Ji
Kerry Ji
Mar 1 · 4 min read

Back in China, I used to stand atop bridges that overlook the bustling roads of Beijing and recite the brand of every car that zoomed by. “Benz…Toyota…Nissan…Mazda…” My interest and passion for cars led me to conduct more research about these inventions. During my research, I encountered a tutorial on how to code a lane detection program, which looked very interesting, but quite challenging.

For detecting lane lines, this project uses OpenCV-Python. The first step in this process is edge detection, which identifies regions in an image with sharp changes in color intensity. Images are made up of individual pixels, and their intensity is represented on a scale of 0–255. Figure 0 represents the color black, while 255 is white. To make processing this image easier, convert this image into grayscale, or black and white. This way, there is only one channel of color, instead of the usual red, blue, and green.

Next, apply a Gaussian Blur to smooth out the image and reduce noise. Applying a Gaussian Blur will minimize errors when detecting the lane lines.

Gaussian Blur

After applying the blur, use the Canny function to trace out areas with the strongest gradient, or change in color. Canny edge detection traces out areas with sharp changes in color intensity, which helps make the lane lines more discernible. The areas in black are the parts of the photo where the differences in intensity between pixels are small while the white lines represent parts with high changes in intensity.

Canny Function

With the lane lines marked clearly, focus on a specific region that encompasses the lane lines. The first step in doing so is using Matplotlib. Matplotlib is a plotting function in Python that creates coordinates. As shown in the image below, the three coordinates that encapsulate the region containing lane lines are as follows: (200, 700), (1100, 700), and (550, 250).

Matplotlib

The three points create a triangular region, which we will create a mask for. Create this mask using a NumPy array to specify the three coordinates and enter the command “np.zeros_like” to set the pixel intensity to 0, or black. Then, use OpenCV’s fillPoly function to fill our region of interest with white; we use “250” to set pixel intensity to the max. The mask helps focus on the region of interest only, instead of the entire image.

Image of Code
Region of Interest Mask

Next, find lines that best fit each lane line through the Hough transform function. Hough transform is a technique that is used to identify lines, among other things, using a voting procedure. The Hough space is similar to a regular x-y coordinate plane, also known as the Cartesian coordinate plane. Straight lines are usually represented by the equation y = mx + b, with ‘m’ as the slope and ‘b’ as the y-intercept. Such equations can be plotted into the Hough space as a single point, with m being the x coordinate and b as the y(m, b).

Cartesian Coordinate Plane vs. Hough Space | towardsdatascience

What if we had a single point, instead of a line? There are an infinite amount of lines that can pass through this point, each with a unique slope and y-intercept. For each line that passes through this point, plot a point using this line’s slope and y-intercept onto the Hough space. Eventually, the family of equations that passes through this point forms a line in the Hough space.

Now, add a point onto our coordinate plane, which allows for the plotting of the family of lines passing this point on the Hough space. Eventually, the Hough space will be filled with lines, formed by individual data points from linear equations in the x-y plane. However, the lane lines on the image are not perfectly straight lines due to the curvature of the road and other factors. To solve this issue, find the line of best fit: the line that passes through the most data points. The Hough space can be split into grids, and these lines are then graphed onto the Hough space. Whichever grid contains the most points of intersection will be the line we use. With the Hough transform, find the lines of best fit and apply them onto the image.

Lines of Best Fit

The final step is to blend the image with the lines of best fit with the original image so that the lines can show up on the lanes instead of a black screen.

Final Product

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store