python+opencv实现车道线检测


Posted in Python onFebruary 19, 2021

python+opencv车道线检测(简易实现),供大家参考,具体内容如下

技术栈:python+opencv

实现思路:

1、canny边缘检测获取图中的边缘信息;
2、霍夫变换寻找图中直线;
3、绘制梯形感兴趣区域获得车前范围;
4、得到并绘制车道线;

效果展示:

python+opencv实现车道线检测

代码实现:

import cv2
import numpy as np


def canny():
 gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
 #高斯滤波
 blur = cv2.GaussianBlur(gray, (5, 5), 0)
 #边缘检测
 canny_img = cv2.Canny(blur, 50, 150)
 return canny_img


def region_of_interest(r_image):
 h = r_image.shape[0]
 w = r_image.shape[1]
 # 这个区域不稳定,需要根据图片更换
 poly = np.array([
 [(100, h), (500, h), (290, 180), (250, 180)]
 ])
 mask = np.zeros_like(r_image)
 # 绘制掩膜图像
 cv2.fillPoly(mask, poly, 255)
 # 获得ROI区域
 masked_image = cv2.bitwise_and(r_image, mask)
 return masked_image


if __name__ == '__main__':
 image = cv2.imread('test.jpg')
 lane_image = np.copy(image)
 canny = canny()
 cropped_image = region_of_interest(canny)
 cv2.imshow("result", cropped_image)
 cv2.waitKey(0)

霍夫变换加线性拟合改良:

效果图:

python+opencv实现车道线检测

代码实现:

主要增加了根据斜率作线性拟合过滤无用点后连线的操作;

import cv2
import numpy as np


def canny():
 gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
 blur = cv2.GaussianBlur(gray, (5, 5), 0)

 canny_img = cv2.Canny(blur, 50, 150)
 return canny_img


def region_of_interest(r_image):
 h = r_image.shape[0]
 w = r_image.shape[1]

 poly = np.array([
 [(100, h), (500, h), (280, 180), (250, 180)]
 ])
 mask = np.zeros_like(r_image)
 cv2.fillPoly(mask, poly, 255)
 masked_image = cv2.bitwise_and(r_image, mask)
 return masked_image


def get_lines(img_lines):
 if img_lines is not None:
 for line in lines:
 for x1, y1, x2, y2 in line:
 # 分左右车道
 k = (y2 - y1) / (x2 - x1)
 if k < 0:
  lefts.append(line)
 else:
  rights.append(line)


def choose_lines(after_lines, slo_th): # 过滤斜率差别较大的点
 slope = [(y2 - y1) / (x2 - x1) for line in after_lines for x1, x2, y1, y2 in line] # 获得斜率数组
 while len(after_lines) > 0:
 mean = np.mean(slope) # 计算平均斜率
 diff = [abs(s - mean) for s in slope] # 每条线斜率与平均斜率的差距
 idx = np.argmax(diff) # 找到最大斜率的索引
 if diff[idx] > slo_th: # 大于预设的阈值选取
 slope.pop(idx)
 after_lines.pop(idx)
 else:
 break

 return after_lines


def clac_edgepoints(points, y_min, y_max):
 x = [p[0] for p in points]
 y = [p[1] for p in points]

 k = np.polyfit(y, x, 1) # 曲线拟合的函数,找到xy的拟合关系斜率
 func = np.poly1d(k) # 斜率代入可以得到一个y=kx的函数

 x_min = int(func(y_min)) # y_min = 325其实是近似找了一个
 x_max = int(func(y_max))

 return [(x_min, y_min), (x_max, y_max)]


if __name__ == '__main__':
 image = cv2.imread('F:\\A_javaPro\\test.jpg')
 lane_image = np.copy(image)
 canny_img = canny()
 cropped_image = region_of_interest(canny_img)
 lefts = []
 rights = []
 lines = cv2.HoughLinesP(cropped_image, 1, np.pi / 180, 15, np.array([]), minLineLength=40, maxLineGap=20)
 get_lines(lines) # 分别得到左右车道线的图片

 good_leftlines = choose_lines(lefts, 0.1) # 处理后的点
 good_rightlines = choose_lines(rights, 0.1)

 leftpoints = [(x1, y1) for left in good_leftlines for x1, y1, x2, y2 in left]
 leftpoints = leftpoints + [(x2, y2) for left in good_leftlines for x1, y1, x2, y2 in left]

 rightpoints = [(x1, y1) for right in good_rightlines for x1, y1, x2, y2 in right]
 rightpoints = rightpoints + [(x2, y2) for right in good_rightlines for x1, y1, x2, y2 in right]

 lefttop = clac_edgepoints(leftpoints, 180, image.shape[0]) # 要画左右车道线的端点
 righttop = clac_edgepoints(rightpoints, 180, image.shape[0])

 src = np.zeros_like(image)

 cv2.line(src, lefttop[0], lefttop[1], (255, 255, 0), 7)
 cv2.line(src, righttop[0], righttop[1], (255, 255, 0), 7)

 cv2.imshow('line Image', src)
 src_2 = cv2.addWeighted(image, 0.8, src, 1, 0)
 cv2.imshow('Finally Image', src_2)

 cv2.waitKey(0)

待改进:

代码实用性差,几乎不能用于实际,但是可以作为初学者的练手项目;
斑马线检测思路:获取车前感兴趣区域,判断白色像素点比例即可实现;
行人检测思路:opencv有内置行人检测函数,基于内置的训练好的数据集;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的with语句与上下文管理器学习总结
Jun 28 Python
Python字符编码判断方法分析
Jul 01 Python
利用Python实现Windows定时关机功能
Mar 21 Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
Apr 17 Python
python之pandas用法大全
Mar 13 Python
python实现守护进程、守护线程、守护非守护并行
May 05 Python
Python使用爬虫爬取静态网页图片的方法详解
Jun 05 Python
python 对给定可迭代集合统计出现频率,并排序的方法
Oct 18 Python
python命名空间(namespace)简单介绍
Aug 10 Python
python使用turtle库绘制奥运五环
Feb 24 Python
python实现信号时域统计特征提取代码
Feb 26 Python
用Python实现屏幕截图详解
Jan 22 Python
python UIAutomator2使用超详细教程
Feb 19 #Python
Python实现曲线拟合的最小二乘法
Feb 19 #Python
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
Feb 19 #Python
python绘制高斯曲线
Feb 19 #Python
Python绘制数码晶体管日期
Feb 19 #Python
Python Pygame实现俄罗斯方块
Feb 19 #Python
python实现图片转字符画
Feb 19 #Python
You might like
多文件上传的例子
2006/10/09 PHP
很实用的一个完整email发送程序
2006/10/09 PHP
Ajax PHP简单入门教程代码
2008/04/25 PHP
应用开发中涉及到的css和php笔记分享
2011/08/02 PHP
php 目录遍历、删除 函数的使用介绍
2013/04/28 PHP
PHP生成sitemap.xml地图函数
2013/11/13 PHP
PHP判断表单复选框选中状态完整例子
2014/06/24 PHP
模仿JQuery.extend函数扩展自己对象的js代码
2009/12/09 Javascript
jQuery 获取URL参数的插件
2010/03/04 Javascript
JavaScript定义类或函数的几种方式小结
2011/01/09 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
js实现日历与定时器
2017/02/22 Javascript
Vue中android4.4不兼容问题的解决方法
2018/09/04 Javascript
Seajs源码详解分析
2019/04/02 Javascript
关于layui flow loading占位图的实现方法
2019/09/21 Javascript
Python中使用MELIAE分析程序内存占用实例
2015/02/18 Python
python脚本内运行linux命令的方法
2015/07/02 Python
Python爬虫之模拟知乎登录的方法教程
2017/05/25 Python
python 字典修改键(key)的几种方法
2018/08/10 Python
Flask核心机制之上下文源码剖析
2018/12/25 Python
python 提取key 为中文的json 串方法
2018/12/31 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
2019/08/29 Python
python sorted函数的小练习及解答
2019/09/18 Python
python TK库简单应用(实时显示子进程输出)
2019/10/29 Python
Django项目基础配置和基本使用过程解析
2019/11/25 Python
网页中的电话号码如何实现一键直呼效果_附示例
2016/03/15 HTML / CSS
娇韵诗加拿大官网:Clarins加拿大
2017/11/20 全球购物
MSC邮轮官方网站:加勒比海、地中海和世界各地的假期
2018/08/27 全球购物
莫斯科的韩国化妆品店:Sifo
2019/12/04 全球购物
巴西箱包、背包、钱包和旅行配件购物网站:Inovathi
2019/12/14 全球购物
应聘文员自荐信范文
2014/03/11 职场文书
奠基仪式策划方案
2014/05/15 职场文书
花木兰观后感
2015/06/10 职场文书
法人代表证明书范本
2015/06/18 职场文书
创业计划书之服装
2019/10/07 职场文书
Python列表的索引与切片
2022/04/07 Python