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网络编程学习笔记(三):socket网络服务器
Jun 09 Python
Python 基础教程之str和repr的详解
Aug 20 Python
关于Python如何避免循环导入问题详解
Sep 14 Python
Django admin美化插件suit使用示例
Dec 12 Python
Python中pillow知识点学习
Apr 30 Python
python requests 测试代理ip是否生效
Jul 25 Python
Django使用消息提示简单的弹出个对话框实例
Nov 15 Python
python循环嵌套的多种使用方法解析
Nov 29 Python
如何将你的应用迁移到Python3的三个步骤
Dec 22 Python
Python处理PDF与CDF实例
Feb 26 Python
从python读取sql的实例方法
Jul 21 Python
详解python安装matplotlib库三种失败情况
Jul 28 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
Zend Framework缓存Cache用法简单实例
2016/03/19 PHP
Laravel路由设定和子路由设定实例分析
2016/03/30 PHP
Apache无法自动跳转却显示目录的解决方法
2020/11/30 PHP
PHP实现对xml的增删改查操作案例分析
2017/05/19 PHP
jQuery查询数据返回object和字符串影响原因是什么
2013/08/09 Javascript
javascript的propertyIsEnumerable()方法使用介绍
2014/04/09 Javascript
jQuery切换网页皮肤并保存到Cookie示例代码
2014/06/16 Javascript
JavaScript的内存释放问题详解
2015/01/21 Javascript
JavaScript定时显示广告代码分享
2015/03/02 Javascript
基于jQuery实现选取月份插件附源码下载
2015/12/28 Javascript
判断JS对象是否拥有某属性的方法推荐
2016/05/12 Javascript
JS实现的添加弹出层并完成锁屏操作示例
2017/04/07 Javascript
Ionic2开发环境搭建教程
2020/08/20 Javascript
javascript ES6 新增了let命令使用介绍
2017/07/07 Javascript
微信小程序WebSocket实现聊天对话功能
2018/07/06 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
js实现类似iphone的网页滑屏解锁功能示例【附源码下载】
2019/06/10 Javascript
Vue 一键清空表单的实现方法
2020/02/07 Javascript
JavaScript面向对象核心知识与概念归纳整理
2020/05/09 Javascript
Python greenlet实现原理和使用示例
2014/09/24 Python
Python 常用的安装Module方式汇总
2017/05/06 Python
基于python(urlparse)模板的使用方法总结
2017/10/13 Python
Python解决N阶台阶走法问题的方法分析
2017/12/28 Python
python 重定向获取真实url的方法
2018/05/11 Python
Python 中字符串拼接的多种方法
2018/07/30 Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
2018/11/18 Python
Python编程快速上手——strip()函数的正则表达式实现方法分析
2020/02/29 Python
Python命令行参数定义及需要注意的地方
2020/11/30 Python
安全检查验收制度
2014/01/12 职场文书
党的群众路线学习材料
2014/05/16 职场文书
组工干部对照检查材料
2014/08/25 职场文书
2014年学校工作总结
2014/11/20 职场文书
2016廉政教育学习心得体会
2016/01/25 职场文书
vue前端工程的搭建
2021/03/31 Vue.js
IDEA 链接Mysql数据库并执行查询操作的完整代码
2021/05/20 MySQL
Python中json.load()和json.loads()有哪些区别
2021/06/07 Python