OpenCV+ moviepy + tkinter 视频车道线智能识别项目源码

项目完整源代码,使用 OpenCV 的Hough 直线检测算法,提取出道路车道线并绘制出来。通过tkinter 提供GUI界面展示效果。
在这里插入图片描述

1、导入相关模块

import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip
import math

2. 用掩码获取ROI区域

def interested_region(img, vertices):if len(img.shape) > 2: mask_color_ignore = (255,) * img.shape[2]else:mask_color_ignore = 255cv2.fillPoly(np.zeros_like(img), vertices, mask_color_ignore)return cv2.bitwise_and(img, np.zeros_like(img))

3、Hough变换空间, 转换像素到直线

def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)lines_drawn(line_img,lines)return line_img

4、在Hough变换后,每帧增加两条线

def lines_drawn(img, lines, color=[255, 0, 0], thickness=6):global cacheglobal first_frameslope_l, slope_r = [],[]lane_l,lane_r = [],[]α =0.2        # 希腊字母阿尔法for line in lines:for x1,y1,x2,y2 in line:slope = (y2-y1)/(x2-x1)if slope > 0.4:slope_r.append(slope)lane_r.append(line)elif slope < -0.4:slope_l.append(slope)lane_l.append(line)img.shape[0] = min(y1,y2,img.shape[0])if((len(lane_l) == 0) or (len(lane_r) == 0)):print ('no lane detected')return 1slope_mean_l = np.mean(slope_l,axis =0)slope_mean_r = np.mean(slope_r,axis =0)mean_l = np.mean(np.array(lane_l),axis=0)mean_r = np.mean(np.array(lane_r),axis=0)if ((slope_mean_r == 0) or (slope_mean_l == 0 )):print('dividing by zero')return 1x1_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l) x2_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l)   x1_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r)x2_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r)if x1_l > x1_r:x1_l = int((x1_l+x1_r)/2)x1_r = x1_ly1_l = int((slope_mean_l * x1_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))y1_r = int((slope_mean_r * x1_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0]))y2_l = int((slope_mean_l * x2_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))y2_r = int((slope_mean_r * x2_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0]))else:y1_l = img.shape[0]y2_l = img.shape[0]y1_r = img.shape[0]y2_r = img.shape[0]present_frame = np.array([x1_l,y1_l,x2_l,y2_l,x1_r,y1_r,x2_r,y2_r],dtype ="float32")if first_frame == 1:next_frame = present_frame        first_frame = 0        else :prev_frame = cachenext_frame = (1-α)*prev_frame+α*present_framecv2.line(img, (int(next_frame[0]), int(next_frame[1])), (int(next_frame[2]),int(next_frame[3])), color, thickness)cv2.line(img, (int(next_frame[4]), int(next_frame[5])), (int(next_frame[6]),int(next_frame[7])), color, thickness)cache = next_frame

5、处理每帧画面


def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):return cv2.addWeighted(initial_img, α, img, β, λ)def process_image(image):global first_framegray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)img_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)lower_yellow = np.array([20, 100, 100], dtype = "uint8")upper_yellow = np.array([30, 255, 255], dtype="uint8")mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow)mask_white = cv2.inRange(gray_image, 200, 255)mask_yw = cv2.bitwise_or(mask_white, mask_yellow)mask_yw_image = cv2.bitwise_and(gray_image, mask_yw)# gause blue gauss_gray= cv2.GaussianBlur(mask_yw_image, (5, 5), 0)# detect edgecanny_edges=cv2.Canny(gauss_gray, 50, 150,apertureSize=3)imshape = image.shapelower_left = [imshape[1]/9,imshape[0]]lower_right = [imshape[1]-imshape[1]/9,imshape[0]]top_left = [imshape[1]/2-imshape[1]/8,imshape[0]/2+imshape[0]/10]top_right = [imshape[1]/2+imshape[1]/8,imshape[0]/2+imshape[0]/10]vertices = [np.array([lower_left,top_left,top_right,lower_right],dtype=np.int32)]roi_image = interested_region(canny_edges, vertices)theta = np.pi/180line_image = hough_lines(roi_image, 4, theta, 30, 100, 180)result = weighted_img(line_image, image, α=0.8, β=1., λ=0.)return result

:

6、用moviepy的videofileClip 读出视频,调用process_image方法处理后保存至文件

first_frame = 1
white_output = '__path_to_output_file__'
clip1 = VideoFileClip("__path_to_input_file__")
new_clip = clip1.fl_image(process_image)
new_clip.write_videofile(white_output, audio=False)  

7、用tkinter编写车道线检测项目的GUI图形界面

import tkinter as tk
from tkinter import *
import cv2
from PIL import Image, ImageTk
import os
import numpy as npglobal last_frame1                                   
last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8)
global last_frame2                                      
last_frame2 = np.zeros((480, 640, 3), dtype=np.uint8)
global cap1
global cap2
cap_input = cv2.VideoCapture("path_to_input_test_video")
cap_drawlane = cv2.VideoCapture("path_to_resultant_lane_detected_video")def show_input_video():                                       if not cap_input.isOpened():                             print("无法打开原始视频")flag1, frame1 = cap_input.read()frame1 = cv2.resize(frame1,(400,500))if flag1 is None:print ("原视频读帧错误")elif flag1:global last_frame1last_frame1 = frame1.copy()pic = cv2.cvtColor(last_frame1, cv2.COLOR_BGR2RGB)     img = Image.fromarray(pic)imgtk = ImageTk.PhotoImage(image=img)lmain.imgtk = imgtklmain.configure(image=imgtk)lmain.after(10, show_input_video)def show_drawlane_video():if not cap_drawlane.isOpened():                             print("无法打开车道线视频")flag2, frame2 = cap_drawlane.read()frame2 = cv2.resize(frame2,(400,500))if flag2 is None:print ("车道线视频读帧错误")elif flag2:global last_frame2last_frame2 = frame2.copy()pic2 = cv2.cvtColor(last_frame2, cv2.COLOR_BGR2RGB)img2 = Image.fromarray(pic2)img2tk = ImageTk.PhotoImage(image=img2)lmain2.img2tk = img2tklmain2.configure(image=img2tk)lmain2.after(10, show_drawlane_video)if __name__ == '__main__':root=tk.Tk()                                     lmain = tk.Label(master=root)lmain2 = tk.Label(master=root)lmain.pack(side = LEFT)lmain2.pack(side = RIGHT)root.title("车道线检测")            root.geometry("900x700+100+10") exitbutton = Button(root, text='退出',fg="red",command=   root.destroy).pack(side = BOTTOM,)show_input_video()show_drawlane_video()root.mainloop()                                  cap.release()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/670111.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面试复盘6——后端开发

前言 深圳的一家初创。下午的时候hr打电话聊了20分钟&#xff0c;还算&#x1f197;然后就约面了。晚上9点直接老板面的&#xff0c;老板是北大的博士华为好多年出来的超级大佬&#xff0c;和这种大佬交流真的也很荣幸。 面试内容 竞赛项目 分工、负责部分、做了什么。 ap…

Halcon 缺陷检测

文章目录 开闭运算&#xff0c;腐蚀膨胀的亮点问题灰度图像的开闭运算&#xff0c;腐蚀膨胀的亮点问题算子二值化算子 Halcon blob特征处理的方法检测缺陷Halcon Blob特征差分的方法检测缺陷Halcon 极坐标变换&#xff08;环形先转换坐标&#xff09;blob特征Halcon Blob局部二…

【WPF.NET开发】优化性能:其他建议

本文内容 画笔的不透明度与元素的不透明度导航到对象对大型 3D 图面进行命中测试CompositionTarget.Rendering 事件避免使用 ScrollBarVisibilityAuto配置字体缓存服务以缩短启动时间 本主题提供其他的性能改进建议。 1、画笔的不透明度与元素的不透明度 使用 Brush 设置元素…

系统分析师-22年-下午答案

系统分析师-22年-下午答案 更多软考知识请访问 https://ruankao.blog.csdn.net/ 试题一必答&#xff0c;二、三、四、五题中任选其中两题作答 试题一 (25分) 说明 某软件公司拟开发一套博客系统&#xff0c;要求能够向用户提供一个便捷发布自已心得&#xff0c;及时有效的…

公有云服务器:功能与性能的完美融合

在数字化浪潮下&#xff0c;云服务器已经成为企业及个人用户存储数据、运行应用的重要基础设施。其中&#xff0c;公有云服务器凭借其诸多优势&#xff0c;更是受到了市场的广泛欢迎。上海悠远将为您详细介绍公有云服务器的功能特点&#xff0c;以及如何进行性能测试。 一、公有…

npm install express -g报错或一直卡着,亲测可解决

问题描述&#xff1a; 最近学习vue3前端框架&#xff0c;安装Node.js之后&#xff0c;在测试是否可行时&#xff0c;cmd窗口执行了&#xff1a;npm install express -g&#xff0c;发现如下图所示一直卡着不动&#xff0c;最后还报错了&#xff0c;网上找了好久&#xff0c;各…

爬虫实战--人民网

文章目录 前言发现宝藏 前言 为了巩固所学的知识&#xff0c;作者尝试着开始发布一些学习笔记类的博客&#xff0c;方便日后回顾。当然&#xff0c;如果能帮到一些萌新进行新技术的学习那也是极好的。作者菜菜一枚&#xff0c;文章中如果有记录错误&#xff0c;欢迎读者朋友们…

【Redis】深入理解 Redis 常用数据类型源码及底层实现(3.详解String数据结构)

【Redis】深入理解 Redis 常用数据类型源码及底层实现&#xff08;1.结构与源码概述&#xff09;-CSDN博客 【Redis】深入理解 Redis 常用数据类型源码及底层实现(2.版本区别dictEntry & redisObject详解)-CSDN博客 紧接着前两篇的总体介绍&#xff0c;从这篇开始&#x…

LLM是一个向量程序库,提示是查询语言

2013 年&#xff0c;Mikolov 等人在 Google。 注意到一些值得注意的事情。 他们正在构建一个模型&#xff0c;将单词嵌入到向量空间中——这个问题从 20 世纪 80 年代开始就已经有很长的学术历史了。 他们的模型使用了一个优化目标&#xff0c;旨在将单词之间的相关关系转化为…

Nicn的刷题日常之获得月份天数

目录 1.题目描述 描述 输入描述&#xff1a; 输出描述&#xff1a; 示例1 2.解题 1.题目描述 描述 KiKi想获得某年某月有多少天&#xff0c;请帮他编程实现。输入年份和月份&#xff0c;计算这一年这个月有多少天。 输入描述&#xff1a; 多组输入&#xff0c;一行有两…

AutoLinearLayout, 在行和列中自动( Android ),排列视图的自定义布局 流式布局

AutoLinearLayout, 在行和列中自动( Android )&#xff0c;排列视图的自定义布局.zip https://download.csdn.net/download/weixin_38744207/11766956?spm1001.2101.3001.6661.1&utm_mediumdistribute.pc_relevant_t0.none-task-download-2%7Edefault%7ECTRLIST%7EPaid-1-…

JVM Java虚拟机入门指南

文章目录 为什么学习JVMJVM的执行流程JVM的组成部分类加载运行时数据区本地方法接口执行引擎 垃圾回收什么样的对象是垃圾呢内存溢出和内存泄漏定位垃圾的方法对象的finalization机制垃圾回收算法分代回收垃圾回收器 JVM调优参数JVM调优工具Java内存泄漏排查思路CPU飙高排查方案…

Jmeter 01 -概述线程组

1、Jmeter:概述 1.1 是什么&#xff1f; Jmeter是Apache公司使用Java 开发的一款测试工具 1.2 为什么&#xff1f; 高效、功能强大 模拟一些高并发或多次循环等特殊场景 1.3 怎么用&#xff1f; 下载安装 1、下载jmeter&#xff0c;解压缩2、安装Java环境&#xff08;jmet…

SpringBoot Security安全认证框架初始化流程认证流程之源码分析

SpringBoot Security安全认证框架初始化流程&认证流程之源码分析 以RuoYi-Vue前后端分离版本为例分析SpringBoot Security安全认证框架初始化流程&认证流程的源码分析 目录 SpringBoot Security安全认证框架初始化流程&认证流程之源码分析一、SpringBoot Security安…

BUUCTF-Real-[struts2]s2-001

漏洞描述 struts2漏洞 S2-001是当用户提交表单数据且验证失败时&#xff0c;服务器使用OGNL表达式解析用户先前提交的参数值&#xff0c;%{value}并重新填充相应的表单数据。例如&#xff0c;在注册或登录页面中&#xff0c;如果提交失败&#xff0c;则服务器通常默认情况下将返…

Linux嵌入式开发+驱动开发-中断

swi汇编指令可以产生软中断&#xff0c;以下是硬件中断的产生到执行完毕的全过程&#xff1a; 在自己设计的芯片“CPU响应中断”程序的第四个步骤可以转向“中断向量控制器”&#xff0c;中断向量控制器中存储中断元服务地址即处理中断处理程序的地址&#xff0c;而不用使用0X1…

算法学习——LeetCode力扣链表篇2

算法学习——LeetCode力扣链表篇2 24. 两两交换链表中的节点 24. 两两交换链表中的节点 - 力扣&#xff08;LeetCode&#xff09; 描述 给你一个链表&#xff0c;两两交换其中相邻的节点&#xff0c;并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题&…

ppt怎么转成pdf文件?3种超实用PPT转PDF方法分享

ppt怎么转成pdf文件&#xff1f;在日常办公中&#xff0c;将PPT转换为PDF文件具有很多实际的好处。首先&#xff0c;PDF文件是一种通用的文件格式&#xff0c;可以在各种操作系统和设备上轻松打开和查看&#xff0c;不受源文件的限制。其次&#xff0c;将PPT转换为PDF可以很好地…

龙测科技荣获2023年度技术生态构建奖

本月&#xff0c;由极客传媒举办的“有被Q到”2024 InfoQ 极客传媒合作伙伴年会顺利举办&#xff0c;龙测科技喜获2023年度技术生态构建奖。 InfoQ是首批将Node.js、HTML5、Docker等技术全面引入中国的技术媒体之一&#xff0c;秉承“扎根社区、服务社区、引领社区”的理念&…

C语言中大小写字母转换详解

大家好&#xff0c;欢迎来到我的博客&#xff01;今天我们要一起探讨C语言中大小写字母转换的原理及使用方法。这个话题可能对于初学者来说有些晦涩&#xff0c;但我会尽力以简单易懂的方式向大家解释&#xff0c;让你在C语言的世界里更得心应手。 1. 背景介绍 首先&#xff…