Lane detection is a critical task in the field of autonomous vehicles and advanced driver assistance systems (ADAS). It involves identifying the location and shape of lanes on a road to ensure the vehicle stays within its intended path. In this blog, we will discuss how to implement lane detection in Python using the popular computer vision library, OpenCV.
To begin, we will import the necessary libraries, including OpenCV, numpy, and matplotlib. We will also read in an image of a road using the imread() function from OpenCV.
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('road.jpg')
Next, we will convert the image to grayscale using the cvtColor() function from OpenCV. This is because lane lines are often easier to detect in grayscale images as they contain less information than color images.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
We will then apply a Gaussian blur to the grayscale image using the GaussianBlur() function from OpenCV. This is to reduce noise and smooth out the image, making it easier to detect edges.
blur = cv2.GaussianBlur(gray, (5, 5), 0)
Next, we will use the Canny edge detection algorithm to detect edges in the blurred image. The Canny algorithm is a popular choice for edge detection as it is able to detect edges with a high level of accuracy.
edges = cv2.Canny(blur, 50, 150)
Lane masking is a technique used in lane detection to improve the performance and accuracy of the algorithm by removing pixels outside the region of interest (ROI) where the lanes are likely to be located.
We will do it manually by defining a polygon that encloses the ROI and now the mask is created by setting all pixels outside the ROI to zero and all pixels inside the ROI to one. The resulting binary image is then applied to the original image using a bitwise AND operation. This effectively removes all pixels outside the ROI, leaving only the pixels inside the ROI.
height, width, _ = img.shape
triangle = np.array([[(100, height), (475, 325), (width, height)]])
mask = np.zeros_like(edges)
mask = cv2.fillPoly(mask, triangle, 255)
mask = cv2.bitwise_and(edges, mask)
We will then use the HoughLinesP() function from OpenCV to detect lines in the edge-detected image. The HoughLinesP() function is a probabilistic Hough transform that is able to detect lines in an image with a high level of accuracy.
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=50, maxLineGap=10)
Finally, we will draw the detected lines on the original image using the cv2.line() function from OpenCV.
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
plt.imshow(img)
plt.show()
In this example, we were able to successfully detect the lane lines in the input image using OpenCV. The resulting image shows the detected lane lines drawn in green.
In conclusion, using OpenCV and Python, we were able to implement a lane detection system. The system was able to detect lane lines in an image with a high level of accuracy. This demonstrates the power of computer vision in solving real-world problems and the ease of use of OpenCV in implementing computer vision tasks in Python. To improve the results we can use other algorithms like Hough Transform, Probabilistic Hough Transform, and other edge detection methods.