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 相关文章推荐
python3.3教程之模拟百度登陆代码分享
Jan 16 Python
Python的Urllib库的基本使用教程
Apr 30 Python
Python中交换两个元素的实现方法
Jun 29 Python
selenium+python自动化测试之页面元素定位
Jan 23 Python
Django中ajax发送post请求 报403错误CSRF验证失败解决方案
Aug 13 Python
Pandas把dataframe或series转换成list的方法
Jun 14 Python
Python3 ffmpeg视频转换工具使用方法解析
Aug 10 Python
Python2与Python3关于字符串编码处理的差别总结
Sep 07 Python
Python CategoricalDtype自定义排序实现原理解析
Sep 11 Python
用python写PDF转换器的实现
Oct 29 Python
Python中的变量与常量
Nov 11 Python
Pygame Time时间控制的具体使用详解
Nov 17 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
PHP远程连接MYSQL数据库非常慢的解决方法
2008/07/05 PHP
php中inlcude()性能对比详解
2012/09/16 PHP
PHP实现的文件操作类及文件下载功能示例
2016/12/24 PHP
Laravel框架使用Seeder实现自动填充数据功能
2018/06/13 PHP
Laravel 实现添加多语言提示信息
2019/10/25 PHP
仿服务器端脚本方式的JS模板实现方法
2007/04/27 Javascript
用jquery与css打造个性化的单选框和复选框
2010/10/20 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
Nodejs学习笔记之NET模块
2015/01/13 NodeJs
Js实现自定义右键行为
2015/03/26 Javascript
教你如何在Node.js中使用jQuery
2016/08/28 Javascript
关于验证码在IE中不刷新的快速解决方法
2016/09/23 Javascript
简单谈谈关于 npm 5.0 的新坑
2017/06/08 Javascript
老生常谈js中的MVC
2017/07/25 Javascript
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
浅谈Node.js 中间件模式
2018/06/12 Javascript
JavaScript ES6 Class类实现原理详解
2020/05/08 Javascript
[50:48]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
Python中for循环和while循环的基本使用方法
2015/08/21 Python
Python编程产生非均匀随机数的几种方法代码分享
2017/12/13 Python
解决安装tensorflow遇到无法卸载numpy 1.8.0rc1的问题
2018/06/13 Python
python自动发微信监控报警
2019/09/06 Python
使用Python项目生成所有依赖包的清单方式
2020/07/13 Python
pycharm + django跨域无提示的解决方法
2020/12/06 Python
使用python tkinter开发一个爬取B站直播弹幕工具的实现代码
2021/02/07 Python
Mytheresa美国官网:德国知名的女性奢侈品电商
2017/05/27 全球购物
客服文员岗位职责
2013/11/29 职场文书
资产经营总监岗位职责
2013/12/04 职场文书
蜜蜂引路教学反思
2014/02/04 职场文书
2014年幼儿园老师工作总结
2014/12/05 职场文书
教师个人事迹材料
2014/12/17 职场文书
2015年幼儿园中班工作总结
2015/04/25 职场文书
活动经费申请报告
2015/05/15 职场文书
员工加薪申请报告
2015/05/15 职场文书
《正比例》教学反思
2016/02/23 职场文书
成功的商业计划书这样写才最靠谱
2019/07/12 职场文书