车道检测代码python

  • n9_978279
    了解作者
  • 3.8MB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-25 02:00
    上传日期
Python编写,视频检测
车道检测代码.rar
  • lane.jpg
    66.3KB
  • lane2.jpg
    72.9KB
  • origin.mp4
    2.5MB
  • work.py
    4.6KB
  • processed.mp4
    1.1MB
  • video.py
    4.6KB
内容介绍
# -*- coding: utf-8 -*- """ Created on Fri Apr 17 13:44:41 2020 @author: dell """ import cv2 import numpy as np from moviepy.editor import VideoFileClip # 高斯滤波核大小 blur_ksize = 5 # Canny边缘检测高低阈值 canny_lth = 50 canny_hth = 150 # 霍夫变换参数 rho = 1 theta = np.pi / 180 threshold = 15 min_line_len = 40 max_line_gap = 20 def process_an_image(img): # 1. 灰度化、滤波和Canny gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) blur_gray = cv2.GaussianBlur(gray, (blur_ksize, blur_ksize), 1) edges = cv2.Canny(blur_gray, canny_lth, canny_hth) # 2. 标记四个坐标点用于ROI截取 rows, cols = edges.shape points = np.array([[(0, rows), (460, 325), (520, 325), (cols, rows)]]) # [[[0 540], [460 325], [520 325], [960 540]]] roi_edges = roi_mask(edges, points) # 3. 霍夫直线提取 drawing, lines = hough_lines(roi_edges, rho, theta, threshold, min_line_len, max_line_gap) # 4. 车道拟合计算 draw_lanes(drawing, lines) # 5. 最终将结果合在原图上 result = cv2.addWeighted(img, 0.9, drawing, 0.2, 0) return result def roi_mask(img, corner_points): # 创建掩膜 mask = np.zeros_like(img) cv2.fillPoly(mask, corner_points, 255) masked_img = cv2.bitwise_and(img, mask) return masked_img def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap): # 统计概率霍夫直线变换 lines = cv2.HoughLinesP(img, rho, theta, threshold, minLineLength=min_line_len, maxLineGap=max_line_gap) # 新建一副空白画布 drawing = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) # 画出直线检测结果 # draw_lines(drawing, lines) return drawing, lines def draw_lines(img, lines, color=[0, 0, 255], thickness=1): for line in lines: for x1, y1, x2, y2 in line: cv2.line(img, (x1, y1), (x2, y2), color, thickness) def draw_lanes(img, lines, color=[255, 0, 0], thickness=8): # a. 划分左右车道 left_lines, right_lines = [], [] for line in lines: for x1, y1, x2, y2 in line: k = (y2 - y1) / (x2 - x1) if k < 0: left_lines.append(line) else: right_lines.append(line) if (len(left_lines) <= 0 or len(right_lines) <= 0): return # b. 清理异常数据 clean_lines(left_lines, 0.1) clean_lines(right_lines, 0.1) # c. 得到左右车道线点的集合,拟合直线 left_points = [(x1, y1) for line in left_lines for x1, y1, x2, y2 in line] left_points = left_points + [(x2, y2) for line in left_lines for x1, y1, x2, y2 in line] right_points = [(x1, y1) for line in right_lines for x1, y1, x2, y2 in line] right_points = right_points + \ [(x2, y2) for line in right_lines for x1, y1, x2, y2 in line] left_results = least_squares_fit(left_points, 325, img.shape[0]) right_results = least_squares_fit(right_points, 325, img.shape[0]) # 注意这里点的顺序 vtxs = np.array( [[left_results[1], left_results[0], right_results[0], right_results[1]]]) # d.填充车道区域 cv2.fillPoly(img, vtxs, (0, 255, 0)) # 或者只画车道线 # cv2.line(img, left_results[0], left_results[1], (0, 255, 0), thickness) # cv2.line(img, right_results[0], right_results[1], (0, 255, 0), thickness) def clean_lines(lines, threshold): # 迭代计算斜率均值,排除掉与差值差异较大的数据 slope = [(y2 - y1) / (x2 - x1) for line in lines for x1, y1, x2, y2 in line] while len(lines) > 0: mean = np.mean(slope) diff = [abs(s - mean) for s in slope] idx = np.argmax(diff) if diff[idx] > threshold: slope.pop(idx) lines.pop(idx) else: break def least_squares_fit(point_list, ymin, ymax): # 最小二乘法拟合 x = [p[0] for p in point_list] y = [p[1] for p in point_list] # polyfit第三个参数为拟合多项式的阶数,所以1代表线性 fit = np.polyfit(y, x, 1) fit_fn = np.poly1d(fit) # 获取拟合的结果 xmin = int(fit_fn(ymin)) xmax = int(fit_fn(ymax)) return [(xmin, ymin), (xmax, ymax)] if __name__ == "__main__": output = 'processed.mp4' clip = VideoFileClip("origin.mp4") out_clip = clip.fl_image(process_an_image) out_clip.write_videofile(output, audio=False)
评论
    相关推荐
    • Python
      Python
    • python
      Python 这是用于大多数基本和简单python代码的示例模板的存储库。
    • Python
      Python Dit是Mijn储存库van Python
    • python
      Python 基本的python编程
    • Python
      Python python和pyspark
    • Python教程
      本手册不会尝试涵盖 Python 的全部知识和每个特性,甚至不会涵盖所有常用的特性。相反地,它介绍了 Python 中许多最引人瞩目的特性,并且会给你一个关于语言特色和风格的认识。读完之后,你将能够阅读和编写 Python ...
    • python绑定
      适用于C ++的库的Python绑定或扩展演示 重点 使用boost.pythonPython.h使c ++工作。 :heavy_large_circle: 的展开方法boost.pythonpython.h :cross_mark: 笔记 没有 资料来源 使用Python.h。 使用boost.python...
    • python学习
      python学习 安装Python 我们转到Python页面进行初始下载:( )在为Mac OSX选择了最新版本(3.9.2)之后,我将其下载到了我的计算机上。 然后,我们搜索了“ Python入门”,并确认VS Code是适用于Python的可行代码...
    • Fluent Python
      This book was written for practicing Python programmers who want to become proficient in Python 3. If you know Python 2 but are willing to migrate to Python 3.4 or later, you should be fine. At the ...
    • Python
      Python 该存储库包含与Python编程研究相关的文档