Lane-Line detection for Self-Driving cars using Images
Hello readers, in today’s tutorial we will be learning about the Lane-Line detection an important algorithm for self-driving cars and its working in detail.
We know that technology has been emerging infinitely in almost every sector. This has added to making human life comfortable and luxurious. But, with intensifying population and transportation, the frequent episode of road traffic, road accidents and traffic congestion have increased to a greater extent.
However, as a solution, autonomous and self-directed driving cars that use Lane-Line detection are becoming more important in reducing traffic accidents.
Lane-Line detection has charmed plenty of institutes and multi-national car generation companies. With an intention to meet all the real-time requirements of the self-driving cars, we propose a framework of lane line detection. The proper working of the model is also explained in detail. This approach is a very nicely-suitable framework to be used in self-driving cars. This proposed method is essentially is a pipeline of computer vision algorithms. It enhances and absorbs uncooked RGB photographs via the camera set up. This image is then processed and segmented to detect the line and boundaries of the road for the car.
Lane-Line Detection Model Steps
- Step 1 : Loading, Capturing and decoding video file.
- Step 2 : Grayscale conversion of image.
- Step 3 : Reduce noise.
- Step 4 : Canny Edge Detector.
- Step 5 : Region of Interest.
- Step 6 : Hough Line Transform.
- Step 7 : Blending the images and displaying the Final results
Lets Get started with the code for the model.
Lane-Line detection for Self-Driving cars using Images
Importing required Python ibraries.
Firstly, we will import all the required libraries for the project.
import matplotlib.pylab as plt import numpy as np import pandas as pd import cv2
Step 1 : Loading, Capturing and decoding image file.
Now that we have our libraries imported, lets proceed towards loading the image for the model to detect lines from.
image = cv2.imread('test_image.jpg") cv2.imshow('result',image) cv2.waitKey(0)
As a Result , the about image is been displayed. We have successfully loaded out image and now can proceed further.
Step 2 : Grayscale conversion of image.
After loading the image and displaying it we proceed with making a copy of the original image. this is to avoid making changes on the original image. Here after we will be working on this copy of the original image. We convert the imported color image into grayscale image. This is done to reduce the computational time that is been taken by the RGB images. We even do this as it is easy to work with Grayscale images when it comes to detecting lines.
image = cv2.imread('test_image.jpg') lane_image = np.copy(image) gray = cv2.cvtColor (lane_image, cv2.COLOR_RGB2GRAY) cv2.imshow("result", gray) cv2.waitKey(0)
Step 3 : Reduce noise.
The next step is to reduce noise and smoothen our image. While detecting images, it’s important to catch as many edges in the images as possible. To do so, we must filter out any image noise. It is imperative to filter out image noise because it can create false edges and ultimately affect edge detection. This filtering and smoothening is done using the Gaussian filter.
blur = cv2.GaussianBlur(gray, (5,5) ,0 ) cv2.imshow("result", blur) cv2.waitKey(0)
Step 4 : Canny Edge Detector.
Now its time to apply the Canny method to identify the edges in our image. An edge corresponds to a particular region in an image where there is a sharp change in intensity.
canny = cv2.Canny(blur, 50, 150) cv2.imshow("result", canny) cv2.waitKey(0)
Step 5 : Region of Interest.
We will now specify the region of interest in the image that we’re going to use to detect our lane lines
def region_of_interest(image): height = image.shape polygons = np.array([[200, height), (1100, height), (550,250)]]) mask =np.zeros_like(image) cv2.fillPoly(mask, polygons, 255) return mask lane_image = np.copy(image) canny = canny(lane_image) cv2.imshow("result", region_of_interest(canny)) cv2.waitKey(0)
Step 6 : Hough Line Transform.
Now we make use of a technique that will detect straight lines in the image. This technique is known as Hough Transform. it is basically used to identify and detect straight lines.
def display_lines(image, lines): line_image = np.zeros_like(image) if lines is not None: for line in lines: x1, y1, x2, y2 = line.reshape(4) cv2.line(line_image, (x1, y1), (x2, y2), (255,255,0),5) return line_image
This shows the lines that we detected using Hough transform and it displayed them on a black image
Step 7 : Blending the images and displaying the Final results.
The final step is to blend the line image to our original color image. So ultimately the lines show up on the road lane instead of any black image screen.
combo_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1) cv2.imshow("result", combo image) cv2.waitKey(0)
Now we need to optimize how these lines are displayed. Sometimes, in the result there can be multiple lines that are displayed. We can optimize these lines as a single line to show the final result.
def average_slope_intercept(image, lines): left_fit =  right_fit =  for line in lines: x1, y1, x2, y2 = line.reshape(4) parameters = np.polyfit((x1, x2), (y1, y2),1) slope = parameters intercept = parameters if slope < 0: left_fit.append((slope, intercept)) else: right_fit.append( (slope, intercept)) left_fit_average = np.average(left_fit, axis=0) right_fit_average = np.average(right_fit, axis = 0) left_line= make_coordinates(image, left_fit_average) right_line= make_coordinates(image, right_fit_average) return np.array([left_line, right_line])
Now , lets display the ultimate result.
image = cv2.imread("test_image.jpg") lane_image = np.copy(image) canny_image = canny(lane_image) cropped_image = region_of_interest(canny_image) lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 120, np.array(), minLineLength=40, maxLineGap=5) averaged_lines = average_slope_intercept(lane_image, lines) line_image = display_lines(lane_image, averaged_lines) combo_image = cv2.addWeighted (lane_image, 0.8, line_image, 1, 1) cv2.imshow("result", combo_image) cv2.waitKey(0)
We have taken the average of or lines and displayed one line on each side. This looks a lot smoother than the earlier image.
In this tutorial, we have successfully created and learned how lane line detection works. We have even seen the practical implementation of the lane line detection algorithm on images. I hope you all enjoyed this tutorial. Happy Learning 🙂
Want to add your thoughts? Need any further help? Leave a comment below and I will get back to you ASAP
For further reading: