Python video lane line detection system based on OpenCV [source code & deployment tutorial]

1. Picture demo

2.png

3.png

4.png

5.png

2. Video demonstration

Python video lane line detection system based on OpenCV [source code & deployment tutorial]

3. Canny edge detection

CV2 provides the function canny to extract image edges. The algorithm idea is as follows:

  1. Use Gaussian Blur to remove noise points (cv2.GaussianBlur)
  2. Grayscale conversion (cv2.cvtColor)
  3. Use the sobel operator to calculate the gradient size and gradient direction of each point
  4. Use non-maximum suppression (only the largest remains) to eliminate spurious effects from edge detection
  5. Apply dual thresholds to determine true and potential edges
  6. Final edge detection is done by suppressing weak edges

1. Gaussian filter

gaussian = cv2.GaussianBlur(color_img, (gaussian_ksize,gaussian_ksize), gaussian_sigmax)

color_img Input image
gaussian_ksize Gaussian kernel size, which can be a square matrix or a rectangular
gaussian_sigmax Gaussian kernel standard deviation in the X direction

2. Image conversion

gray_img = cv2.cvtColor(input_image, flag)

Used for color space conversion. input_image is the image to be converted, flag is the type of conversion, and the return value is the image matrix after color space conversion. Flag corresponds to:
cv2.COLOR_BGR2GRAY BGR -> Gray
cv2.COLOR_BGR2RGB BGR -> RGB
cv2.COLOR_BGR2HSV BGR -> HSV
You can find out the difference between this function and cv2.imread()

3. Edge Detection

edge_img = cv2.Canny(gray_img,canny_threshold1,canny_threshold2)

imag is the image to be operated, threshold1 is the lower threshold, threshold2 is the upper threshold, and the return value is the edge map.
6.png

4.ROI and mask

Use the array to select the ROI (region of interest, the area of ​​interest, and then perform the Boolean operation (AND operation) with the original image.
7.png

poly_pts = numpy.array([[[0,368],[300,210],[340,210],[640,368]]])

The four arrays are created and stored in the variable poly_pts.

mask = np.zeros_like(gray_img)

Build an array of the same dimension as gray_img and initialize all variables to zero.

cv2.fillPoly(mask, pts, color)

Draw polygon function. mask is the drawing object, pts is the drawing range, and color is the drawing color.

img_mask = cv2.bitwise_and(gray_img, mask)

AND operation
8.png

5. Hough Transform

lines = cv2.HoughLinesP(edge_img,  1, np.pi / 180, 15, minLineLength=40, maxLineGap=20)

edge_img: The image matrix to be detected
Parameter 2: The precision of the distance r, the larger the value, the more lines are considered
Parameter 3: The precision of the distance theta, the larger the value, the more lines are considered
Parameter 4: The accumulated number threshold, the higher the value Small, consider more lines
minLineLength: the shortest length threshold, lines shorter than this length will be excluded
maxLineGap: the maximum distance between two points on the same line
Return value::
[np.array([[x_1,y_1, x_2, y_2]]),
np.array([[x_1, y_1, x_2, y_2]]),
…,
np.array([[x_1, y_1, x_2, y_2]])]

6. Outlier filtering

Loop to find slopes outside the set range and remove them.

idx = np.argmax(diff)

When one dimension is stored in the diff, find the largest value in the diff and return the position of the value.
Remove the idxth number from the list lines

7. Lane marking

cv2.line(img, tuple(line[0]), tuple(line[1]), color,thickness)

Draw a line on the picture, img is the marked target picture, ine[0] is the starting point coordinate, line[1] is the end point coordinate, and thickness is the line width.
Note that the two coordinates need to be in tuple tuple format, ie tuple(line[0])
5.png

8. System Integration

1.png

9. Complete source code & environment deployment video tutorial

Baidu Bread can download the source code by searching for the title name

8. References

Guess you like

Origin blog.csdn.net/cheng2333333/article/details/126755508