订阅
纠错
加入自媒体

使用Python+OpenCV进行实时车道检测

2020-05-30 10:14
磐创AI
关注

如上所示,对蒙版图像应用阈值后,我们只得到输出图像中的车道标线。现在我们可以通过霍夫线变换很容易地检测出这些标记。霍夫线变换霍夫线变换是一种检测任何可以用数学方法表示形状的方法。例如,它可以检测矩形、圆、三角形或直线等形状。我们感兴趣的是检测可以表示为直线的车道标线。这是相关文档:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html在执行图像阈值化后对图像应用霍夫线变换将提供以下输出:

我们需要对所有帧执行此过程,然后将生成的帧缝合到新视频中。用OpenCV在Python中实现车道检测是时候用Python实现这个车道检测项目了!我推荐使用Google Colab,因为构建车道检测系统需要计算能力。首先导入所需的库:import osimport reimport cv2import numpy as npfrom tqdm import tqdm_notebookimport matplotlib.pyplot as plt读取视频帧我已经从这个YouTube视频中抽取了一些视频片段。你可以从这个链接下载:https://drive.google.com/file/d/1e4cc4zFFna3Owyym6aq7ZXoquHA2l95O/view?usp=sharing。# 获取帧的文件名col_frames = os.listdir('frames/')col_frames.sort(key=lambda f: int(re.sub('D', '', f)))

# 加载帧col_images=[]for i in tqdm_notebook(col_frames):    img = cv2.imread('frames/'+i)    col_images.append(img)让我们绘制一个帧:# 指定一个索引idx = 457

# plot frameplt.figure(figsize=(10,10))plt.imshow(col_images[idx][:,:,0], cmap= "gray")plt.show()

帧掩码创建我们感兴趣的区域是一个多边形。我们想掩盖除了这个区域以外的一切。因此,我们首先必须指定多边形的坐标,然后使用它来准备帧掩码:# 创建0矩阵stencil = np.zeros_like(col_images[idx][:,:,0])

# 指定多边形的坐标polygon = np.array([[50,270], [220,160], [360,160], [480,270]])

# 用1填充多边形cv2.fillConvexPoly(stencil, polygon, 1)# 画出多边形plt.figure(figsize=(10,10))plt.imshow(stencil, cmap= "gray")plt.show()

# 应用该多边形作为掩码img = cv2.bitwise_and(col_images[idx][:,:,0], col_images[idx][:,:,0], mask=stencil)

# plot masked frameplt.figure(figsize=(10,10))plt.imshow(img, cmap= "gray")plt.show()

图像预处理我们必须对视频帧执行一些图像预处理操作来检测所需的车道。预处理操作包括:图像阈值化霍夫线变换1.图像阈值化# 应用图像阈值化ret, thresh = cv2.threshold(img, 130, 145, cv2.THRESH_BINARY)

# 画出图像plt.figure(figsize=(10,10))plt.imshow(thresh, cmap= "gray")plt.show()

2.霍夫线变换lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)

# 创建原始帧的副本dmy = col_images[idx][:,:,0].copy()

# 霍夫线for line in lines:  x1, y1, x2, y2 = line[0]  cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)

# 画出帧plt.figure(figsize=(10,10))plt.imshow(dmy, cmap= "gray")plt.show()

现在我们将对每个帧应用所有这些操作。我们还将结果帧保存在新目录中:cnt = 0

for img in tqdm_notebook(col_images):

 # 应用帧掩码  masked = cv2.bitwise_and(img[:,:,0], img[:,:,0], mask=stencil)

 # 应用图像阈值化  ret, thresh = cv2.threshold(masked, 130, 145, cv2.THRESH_BINARY)

 # 应用霍夫线变换  lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)  dmy = img.copy()

 #画出检测到的线  try:    for line in lines:      x1, y1, x2, y2 = line[0]      cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)

   cv2.imwrite('detected/'+str(cnt)+'.png',dmy)

 except TypeError:     cv2.imwrite('detected/'+str(cnt)+'.png',img)

 cnt+= 1视频准备# 输入帧的路径pathIn= 'detected/'

#输出视频路径pathOut = 'roads_v2.mp4'

# 视频每秒的帧数fps = 30.0from os.path import isfile, join

# 获取帧的文件名files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]files.sort(key=lambda f: int(re.sub('D', '', f)))接下来,我们将把检测到的车道上的所有帧放入一个列表中:frame_list = []

for i in tqdm_notebook(range(len(files))):    filename=pathIn + files[i]    #读取每一个文件    img = cv2.imread(filename)    height, width, layers = img.shape    size = (width,height)

   #将帧插入图像数组    frame_list.append(img)最后,我们现在可以使用下面的代码将帧合并为视频:# 写入视频out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

for i in range(len(frame_array)):    out.write(frame_array[i])

out.release()这就完成了Python中的车道检测系统。

结尾在本教程中,我们介绍了一种简单的车道检测技术。我们没有使用任何模型或复杂的图像特征,相反,我们的解决方案完全基于某些图像预处理操作。但是,在很多情况下,这个解决方案都无法工作。例如,当没有车道标线,或者道路上的车辆太多时,该系统将失败。在车道检测中有更复杂的方法来克服这些问题。

<上一页  1  2  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

人工智能 猎头职位 更多
扫码关注公众号
OFweek人工智能网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号