最近找了一款车道线识别的代码,可以准确识别出车道线,里面的视频路径可以切换为自己的视频,现在我以自带的视频数据展示效果
图片中可以准确识别车的车道线:
下面给出一部分源代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def make_line(image, line_parameters):
slope = line_parameters[0]
intercept = line_parameters[1]
y1 = image.shape[0] #lane detection line starts from bottom of image
y2 = int(y1*(3/5)) #lane detection line ends at 3/5 of height of image
x1 = int((y1 - intercept)/slope)
x2 = int((y2 - intercept)/slope)
return np.array([x1, y1, x2, y2])
#convert to grayscale, blur and return the canny image
def canny(image):
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0) #(image, (kernel size), standard deviation)
canny_img = cv2.Canny(blur, 50, 150) #(image, low_threshold, high threshold)
return canny_img
def region_of_interest(image):
# plt.imshow(image)
# plt.show()
mask = np.zeros_like(image)
#Defining a 3 channel or 1 channel color to fill the mask with depending on the input image
if len(image.shape) > 2:
channel_count = image.shape[2]
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#We could have used fixed numbers as the vertices of the polygon,
#but they will not be applicable to images with different dimesnions.
rows, cols = image.shape[:2]
bottom_left = [cols * 0.1, rows * 0.95]
top_left = [cols * 0.4, rows * 0.6]
bottom_right = [cols * 0.9, rows * 0.95]
top_right = [cols * 0.6, rows * 0.6]
vertices = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32)
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_image = cv2.bitwise_and(image, mask)
return masked_image
~~~
需要的小伙伴可以参考一下代码