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实现选择排序
Jun 04 Python
Python django实现简单的邮件系统发送邮件功能
Jul 14 Python
Python批量提取PDF文件中文本的脚本
Mar 14 Python
python中使用zip函数出现错误的原因
Sep 28 Python
Python基于百度云文字识别API
Dec 13 Python
Python制作exe文件简单流程
Jan 24 Python
python 计算积分图和haar特征的实例代码
Nov 20 Python
Django 404、500页面全局配置知识点详解
Mar 10 Python
python实现opencv+scoket网络实时图传
Mar 20 Python
python读取图像矩阵文件并转换为向量实例
Jun 18 Python
深入了解Python 变量作用域
Jul 24 Python
详解Python中*args和**kwargs的使用
Apr 07 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
dede全站URL静态化改造[070414更正]
2007/04/17 PHP
php 生成文字png图片的代码
2011/04/17 PHP
浅析php数据类型转换
2014/01/09 PHP
php常见的魔术方法详解
2014/12/25 PHP
JavaScript中的Location地址对象
2008/01/16 Javascript
JavaScript mapreduce工作原理简析
2012/11/25 Javascript
Jquery AJAX POST与GET之间的区别
2013/11/14 Javascript
详细解读JavaScript的跨浏览器事件处理
2015/08/12 Javascript
利用CSS3在Angular中实现动画
2016/01/15 Javascript
教你如何终止JQUERY的$.AJAX请求
2016/02/23 Javascript
jQuery增加和删除表格项目及实现表格项目排序的方法
2016/05/30 Javascript
Validform表单验证总结篇
2016/10/31 Javascript
详解vue.js之绑定class和style的示例代码
2017/08/24 Javascript
使用vue的v-for生成table并给table加上序号的实例代码
2017/10/27 Javascript
vue.js前后端数据交互之提交数据操作详解
2018/04/24 Javascript
JS异步错误捕获的一些事小结
2019/04/26 Javascript
vue使用better-scroll实现滑动以及左右联动
2020/06/30 Javascript
node koa2 ssr项目搭建的方法步骤
2020/12/11 Javascript
Vue实现小购物车功能
2020/12/21 Vue.js
python定时采集摄像头图像上传ftp服务器功能实现
2013/12/23 Python
Python实现测试磁盘性能的方法
2015/03/12 Python
使用python加密自己的密码
2015/08/04 Python
python BlockingScheduler定时任务及其他方式的实现
2019/09/19 Python
PyTorch的SoftMax交叉熵损失和梯度用法
2020/01/15 Python
Python通过Tesseract库实现文字识别
2020/03/05 Python
MAC平台基于Python Appium环境搭建过程图解
2020/08/13 Python
HTML5中indexedDB 数据库的使用实例
2017/05/11 HTML / CSS
使用canvas压缩图片大小的方法示例
2019/08/02 HTML / CSS
英国最大的电子产品和家电零售企业:Currys PC World
2016/09/24 全球购物
公积金转移接收函
2014/01/11 职场文书
社区八一活动方案
2014/02/03 职场文书
给学校的建议书范文
2014/05/15 职场文书
档案工作汇报材料
2014/08/21 职场文书
2014单位领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
纯 CSS 自定义多行省略的问题(从原理到实现)
2021/11/11 HTML / CSS