Autonomous Driving Car is one of the most disruptive innovations in AI. Fuelled by Deep Learning algorithms, they are continuously driving our society forward and creating new opportunities in the mobility sector. An autonomous car can go anywhere a traditional car can go and does everything that an experienced human driver does. But it’s very essential to train it properly. One of the many steps involved during the training of an autonomous driving car is lane detection, which is the preliminary step. Today, we are going to learn how to perform lane detection using videos.
Lane detection involves the following steps:
- Capturing and decoding video file: We will capture the video using VideoCapture object and after the capturing has been initialized every video frame is decoded (i.e. converting into a sequence of images).
- Grayscale conversion of image: The video frames are in RGB format, RGB is converted to grayscale because processing a single channel image is faster than processing a three-channel colored image.
- Reduce noise: Noise can create false edges, therefore before going further, it’s imperative to perform image smoothening. Gaussian filter is used to perform this process.
- Canny Edge Detector: It computes gradient in all directions of our blurred image and traces the edges with large changes in intesity. For more explanation please go through this article: Canny Edge Detector
- Region of Interest: This step is to take into account only the region covered by the road lane. A mask is created here, which is of the same dimension as our road image. Furthermore, bitwise AND operation is performed between each pixel of our canny image and this mask. It ultimately masks the canny image and shows the region of interest traced by the polygonal contour of the mask.
- Hough Line Transform: The Hough Line Transform is a transform used to detect straight lines. The Probabilistic Hough Line Transform is used here, which gives output as the extremes of the detected lines
Dataset: The dataset consists of the video file of a road.
You can download the dataset from this GitHub link – Dataset
Now let’s start the implementation process:
Libraries required for this task:
- NumPy: It comes by default with anaconda
- Matplotlib: To install matplotlib, type – “pip install matplotlib” into your command line
- OpenCV: It can be installed in two ways, using anaconda or using pip.
To install using anaconda, type- “conda install -c conda-forge opencv”, or to install using pip, type-
“pip install opencv-python” into your command line
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Multiple Color Detection in Real-Time using Python-OpenCV
- YOLO : You Only Look Once - Real Time Object Detection
- Python | Real time weather detection using Tkinter
- Detect Cat Faces in Real-Time using Python-OpenCV
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Python | Corner Detection with Shi-Tomasi Corner Detection Method using OpenCV
- Python | Corner detection with Harris Corner Detection method using OpenCV
- Gun Detection using Python-OpenCV
- OpenCV C++ Program for coin detection
- Python | Smile detection using OpenCV
- Circle Detection using OpenCV | Python
- Text Detection and Extraction using OpenCV and OCR
- OpenCV C++ Program for Face Detection
- Pedestrian Detection using OpenCV-Python
- Opencv Python program for Face Detection
- White and black dot detection using OpenCV | Python
- Face Detection using Python and OpenCV with webcam
- Line detection in python with OpenCV | Houghline method
- Detection of a specific color(blue here) using OpenCV with Python
The canny function calculates derivative in both x and y directions, and according to that, we can see the changes in intensity value. Larger derivatives equal to High intensity(sharp changes), Smaller derivatives equal to Low intensity(shallow changes):
Masking our canny image after finding the region of interest:
We are going to find the coordinates of our road lane:
Differentiating left and right road lanes with the help of positive and negative slopes respectively and appending them into the lists, if the slope is negative then the road lane belongs to the left-hand side of the vehicle, and if the slope is positive then the road lane belongs to the right-hand side of the vehicle:
Fitting the coordinates into our actual image and then returning the image with the detected line(road with the detected lane):
Firstly, the video file is read and decoded into frames and using Houghline method the straight line which is going through the image is detected. Then we call all the functions.
Please go through this link and download the output video to visualize it more clearly – Output