An OpenCV implementation of road lane detection written in Python.
This project requires
- matplotlib
- cv2
To run, clone this repository and use
python laneDetector.py --path <path to image>
- Thresholding
- Canny Edge Detection
- Region of Interest Selection
- Hough Transform Line Detection
- Line Averaging
I used an image from the Cityscapes dataset.
Thresholding is possibly a better option than converting an RGB image to HSV or HSL colour space as you're not always guaranteed of lines being an exact white or yellow.
Threshold is applied using
retval, threshold = cv2.threshold(convertGrayScale(img), 115, 255, cv2.THRESH_BINARY)
The parameters supplied to threshold can be tweaked. 115, 255 worked well for me. This gives
A gaussian kernel of size 3 is applied to blur out any rough lines and gives
The lanes, more often than not, fall within the polygon below. The vertices correspond to
a = [0, 1023]
b = [0, 630]
c = [720, 455]
d = [1505, 511]
e = [2047, 689]
f = [2047, 1023]
After filtering the image becomes
The parameters ho – distance resolution of the accumulator in pixels, theta – angle resolution of the accumulator in radians, threshold - only those lines are returned that get more votes than the threshold, minLineLength – segments shorter than this are rejected, and maxLineGap – maximum allowed gap between points on the same line to link them, can be tweaked to vary output. I used
cv2.HoughLinesP(image, rho = 1, theta = np.pi / 180, threshold = 20, minLineLength = 20, maxLineGap = 300)
Each of the multiple line sets are averaged into one line. To do this I found lines with similar slopes and averaged them into one line. One limitation of this implementation is that it only considers two lanes for final output. However the two lanes can both be left lanes or right lanes unlike other implementations that look for one left and one right lane.