python实现简单的车道线检测

该案例通过阈值分割+区域掩膜的方式实现简单的车道线检测,内容主要包括两个方面:1. 设定阈值划分,2. 设定几何区域掩膜(该部分采用三角形区域划分),摄像头传感器一般固定于车体,故其视野范围可认为固定区域,具体实现如下.

demo:

  1. 图像读取


import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# 读取image.
image = mping.imread("../data/loadline.jpg")
print(image.shape)

# 读取图像的分辨率shape,复制图片
y_size = image.shape[0]
x_size = image.shape[1]
color_select = np.copy(image)
line_image = np.copy(image)
  1. 设定阈值

red_threshold = 220
green_threshold = 220
blue_threshold = 220
rgb_threshold = [red_threshold, green_threshold, blue_threshold]

  1. 获取感兴趣的三角区域(也可定义为其他多边形区域)

left_bottom = [0, y_size-1]
right_bottom = [x_size-1, y_size-1]
apex = [370, 200]  # 设定三角形顶点
# 绘制三角区域,通过一阶函数拟合的方式定义三条边
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)  # 一阶拟合
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)

  1. 阈值划分并锁定三角形区域

# 过滤低于阈值的像素
color_threshold = (image[:, :, 0] < rgb_threshold[0]) |\
                  (image[:, :, 1] < rgb_threshold[1]) |\
                  (image[:, :, 2] < rgb_threshold[2])

# 找到线内区域(获取坐标矩阵)
XX, YY = np.meshgrid(np.arange(0, x_size), np.arange(0, y_size))
region_threshold = (YY > (XX*fit_left[0] + fit_left[1])) &\
                   (YY > (XX*fit_right[0] + fit_right[1])) &\
                   (YY < (XX*fit_bottom[0] + fit_bottom[1]))
  1. 画图验证并保存

# 查找在感兴趣区域的着色部分
color_select[color_threshold] = [0, 0, 0]
# line_image[~color_threshold & region_threshold] = [255, 0, 0]
line_image[region_threshold] = [255, 0, 0]

# 画图验证
plt.imshow(color_select)
# plt.imshow(line_image)
plt.savefig("../data/result_2.jpg")
plt.show()

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dragon&6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值