采用Python OpenCV进行车道线检测

步骤

  • 主要使用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)

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用PythonOpenCV进行车道线检测的示例代码: ``` python import cv2 import numpy as np # 读取图像 image = cv2.imread('test.jpg') # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # 高斯滤波 kernel_size = 5 blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0) # Canny边缘检测 low_threshold = 50 high_threshold = 150 edges = cv2.Canny(blur_gray, low_threshold, high_threshold) # 区域选择 mask = np.zeros_like(edges) ignore_mask_color = 255 imshape = image.shape vertices = np.array([[(0,imshape[0]),(450, 325), (550, 325), (imshape[1],imshape[0])]], dtype=np.int32) cv2.fillPoly(mask, vertices, ignore_mask_color) masked_edges = cv2.bitwise_and(edges, mask) # Hough变换 rho = 1 theta = np.pi/180 threshold = 20 min_line_length = 20 max_line_gap = 300 line_image = np.copy(image)*0 lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) # 绘制车道线 for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10) # 将车道线绘制在原始图像上 color_edges = np.dstack((edges, edges, edges)) lines_edges = cv2.addWeighted(image, 0.8, line_image, 1, 0) combo = cv2.addWeighted(color_edges, 0.2, lines_edges, 1, 0) # 显示结果 cv2.imshow('result', combo) cv2.waitKey(0) cv2.destroyAllWindows() ``` 其中,`test.jpg`是测试图像的文件名。在代码中,分别进行了图像读取、灰度化、高斯滤波、Canny边缘检测、区域选择、霍夫变换、车道线绘制等步骤。最终,将车道线绘制在原始图像上,并显示结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值