步骤
- 主要使用Python的OpenCV库
- 使用高斯滤波获取平滑化图像,降低噪声的干扰
- 获取灰度数组,采用Canny算子、调整双阈值获取车道线的轮廓
- 获取掩膜,排除目标梯形区域以外的轮廓干扰
- 采用霍夫曼检测获取左右车道直线
代码
import cv2
import numpy as np
# 高斯滤波+canny边缘检测
# 高斯模糊卷积核只能是奇数,防止加的像素不对称。模糊半径越大,图像就越模糊。
def get_edge_img(color_img, gaussian_ksize=5, gaussian_sigmax=1,
canny_threshold1=50, canny_threshold2=100):
# color_img 输入图片 gaussian_ksize 高斯核大小 gaussian_sigmax X方向上的高斯核标准偏差
gaussian = cv2.GaussianBlur(color_img, (gaussian_ksize, gaussian_ksize), gaussian_sigmax)
gray_img = cv2.cvtColor(gaussian, cv2.IMREAD_GRAYSCALE)
# 灰度图片,低阈值,高阈值
edge_img = cv2.Canny(gray_img, canny_threshold1, canny_threshold2)
return edge_img
# 获取掩膜
def roi_mask(gray_img):
# 左上角为原点,从左到右为x正轴,以从上到下为y正轴
poly_pts = np.array([[[0, 368], [300, 210], [340, 210], [640, 368]]])
# zeros_like函数:保持原有的数据类型,但是将其值全变为0
# cv2.imshow('gray_img', gray_img)
mask = np.zeros_like(gray_img)
# cv2.imshow('mask', mask)
cv2.fillPoly(mask, pts=poly_pts, color=255)
img_mask = cv2.bitwise_and(gray_img, mask)
cv2.imshow('img_mask', img_mask)
return img_mask
# 获取车道线
def get_lines(edge_img):
# 斜率计算
def calculate_slope(line):
x_1, y_1, x_2, y_2 = line[0]
slope = (y_2 - y_1) / (x_2 - x_1)
return slope
# 离群值过滤
def reject_abnormal_lines(lines, threshold):
slopes = [calculate_slope(line) for line in lines]
while len(lines) > 0:
mean = np.mean(slopes)
# 取斜率绝对值
diff = [abs(s - mean) for s in slopes]
# argmax返回列表中最大值的索引
idx = np.argmax(diff)
# 如果最大值比阈值大
if diff[idx] > threshold:
slopes.pop(idx)
lines.pop(idx)
else:
break
return lines
# 最小二乘拟合
def least_squares_fit(lines):
# 1.线上的点的坐标集合
x_coords = np.ravel([[line[0][0], line[0][2]] for line in lines])
y_coords = np.ravel([[line[0][1], line[0][3]] for line in lines])
# 2. 进行直线拟合.得到多项式系数。x, y为各个点的横纵坐标,deg为拟合曲线的次数(多少次方),因为是求直线,所以deg为1
poly = np.polyfit(x_coords, y_coords, deg=1)
# 3. 根据多项式系数,计算两个直线上的点,用于唯一确定这条直线
# y轴是拟合计算后的点
point_min = (np.min(x_coords), np.polyval(poly, np.min(x_coords)))
point_max = (np.max(x_coords), np.polyval(poly, np.max(x_coords)))
# return np.array([point_min, point_max], dtype=np.int0)
# 将[point_min, point_max]转化为int32类型
return np.array([point_min, point_max], dtype=np.int32)
# 霍夫检测,没有用到lines参数
# image: 必须是二值图像,推荐使用canny边缘检测的结果图像;
# rho: 线段以像素为单位的距离精度,double类型的,推荐用1.0
# theta: 线段以弧度为单位的角度精度,推荐用numpy.pi / 180
# threshod: 累加平面的阈值参数,int类型,超过设定阈值才被检测出线段,值越大,基本上意味着检出的线段越长,检出的线段个数越少。根据情况推荐先用100试试
# lines:这个参数的意义未知,发现不同的lines对结果没影响,但是不要忽略了它的存在
# minLineLength:线段以像素为单位的最小长度,根据应用场景设置
# maxLineGap:同一方向上两条线段判定为一条线段的最大允许间隔(断裂),超过了设定值,则把两条线段当成一条线段,值越大,允许线段上的断裂越大,越有可能检出潜在的直线段
lines = cv2.HoughLinesP(edge_img, 1, np.pi / 180, 15, minLineLength=40, maxLineGap=20)
# 返回线段的两个端点
# 在它的坐标系里,左边的直线斜率小于0
left_lines = [line for line in lines if calculate_slope(line) < 0]
# 在它的坐标系里,右边的直线斜率大于0
right_lines = [line for line in lines if calculate_slope(line) > 0]
# 过滤掉斜率大于阈值的线段
left_lines = reject_abnormal_lines(left_lines, threshold=0.2)
right_lines = reject_abnormal_lines(right_lines, threshold=0.2)
return least_squares_fit(left_lines), least_squares_fit(right_lines)
# 绘制车道线
def draw_line(img, lines):
left_line, right_line = lines
cv2.line(img, tuple(left_line[0]), tuple(left_line[1]), color=(0, 255, 255), thickness=3)
cv2.line(img, tuple(right_line[0]), tuple(right_line[1]), color=(0, 255, 255), thickness=3)
# 依次调用以上函数,返回重绘车道线后的图
def show_lane(color_img):
edge_img = get_edge_img(color_img)
mask_gray_img = roi_mask(edge_img)
lines = get_lines(mask_gray_img)
draw_line(color_img, lines)
return color_img
if __name__ == '__main__':
# 如果为cv2.VideoCapture(0),表示打开笔记本的内置摄像头。
capture = cv2.VideoCapture('./video/video.mp4')
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
outfile = cv2.VideoWriter('output.avi', fourcc, 25, (1280, 368))
while True:
ret, frame = capture.read()
origin = np.copy(frame)
frame = show_lane(frame)
output = np.concatenate((origin, frame), axis=1)
outfile.write(output)
cv2.imshow('video', output)
cv2.waitKey(10)