Autonomous Driving Car is one of the most disruptive innovations in AI. One of the many steps involved during the training of an autonomous driving car is lane detection. Road Lane Detection requires to detection of the path of self-driving cars and avoiding the risk of entering other lanes. Lane recognition algorithms reliably identify the location and borders of the lanes by analyzing the visual input. Advanced driver assistance systems (ADAS) and autonomous vehicle systems both heavily rely on them. Today we will be talking about one of these lane detection algorithms.
-
Remove Noise :
- Apply Gaussian filter mask to make the image smoother.
- Remove unwanted pixel values that could interfere with lane detection.
-
Edge Detection :
- Convert the image to grayscale.
- Convert the grayscale image to binary.
- Use a special pattern to find edges by applying a row kernel to each pixel.
-
Region of Interest :
- Here we create a mask for the Edge detected image to isolate the region of interest (ROI) containing the lane lines.
- It creates a binary polygon mask using four points (pts) and fills it with blue color.
- The mask is then applied to the edges image using a bitwise AND operation, and
- The resulting masked image is returned as output.
-
Hough Lines Detection :
- This function performs the Hough transform on the img_mask image to detect line segments that may correspond to the lane boundaries.
- It uses the Probabilistic Hough Transform (cv::HoughLinesP()) and specifies the minimum line length, maximum gap between segments, and other parameters.
- The detected lines are returned as a vector of cv::Vec4i objects.
-
Line Classification :
- This function separates the detected lines into left and right lines based on their slopes and positions.
- It iterates through the lines vector, calculates the slope of each line segment, and checks if it meets certain conditions.
- Lines with slopes greater than a threshold and located on the appropriate sides of the image center are classified as right lines,
- while lines with negative slopes and located on the other side are classified as left lines.
- The resulting separated lines are returned as a vector of two vectors: output[0] contains the right lines, and output[1] contains the left lines.
-
Regression :
-
The regression function is defined within the LaneDetector class.
-
It takes two parameters: left_right_lines, which is a vector of vectors of cv::Vec4i representing the separated lines for the left and right lanes, and inputImage, which is the original input image. It returns a vector of cv::Point that represents the line segments forming the lane boundaries.
-
If right lines are being detected, fit a line using all the init and final points of the lines
-
This code block checks if the right_flag is set to true, indicating that right lines have been detected.
-
If so, it iterates through each line in left_right_lines[0], which represents the right lane lines.
-
The initial and final points of each line are extracted and added to the right_pts vector.
-
After collecting the points, the cv::fitLine function is used to fit a line to the points using the least squares method.
-
The resulting line is stored in right_line, and the slope (right_m) and y-intercept (right_b) of the line are calculated.
-
One the slope and offset points have been obtained, apply the line equation to obtain the line points
-
Here, the code calculates the y-coordinates (ini_y and fin_y) for the starting and ending points of the lane lines
-
Based on the image dimensions and a predefined value of 470. Using the slopes (right_m and left_m), y-intercepts (right_b and left_b), and the calculated y-coordinates, the x-coordinates of the lane lines are determined using the line equation.
-
Finally, the four points representing the lane boundaries are stored in the output vector.
-
-
Turn Prediction :
- This function predicts the turn direction based on the vanishing point of the lane boundaries.
- It calculates the x-coordinate of the vanishing point (vanish_x) using the slopes and intercepts of the right and left lines.
- Depending on the position of the vanishing point relative to the image center, it determines if the road is turning left, turning right, or going straight.
- The predicted turn direction is returned as a string (output).
-
Results :
- This function plots the detected lane boundaries, turn prediction message, and a transparent polygon covering the area inside the lane boundaries.
- It takes the original input image inputImage, the lane points lane, and the turn prediction turn.
- First, it creates a copy of the input image as output. Then, it creates a polygon using the lane points (poly_points) and fills it with red color.
- The filled polygon is blended with the output image using cv::addWeighted(), creating a transparent effect.
- Next, it draws the left and right lane lines using yellow color (cv::line()). It also puts the turn prediction text on the image using cv::putText().
- Finally, it displays the final output image in a window named "Lane" using cv::imshow(). The function returns 0 to indicate success.