Python OpenCV车道线识别侦测

简介: Python OpenCV车道线识别侦测
程序示例精选
Python OpenCV车道线识别侦测
如需安装运行环境或远程调试,可点击
博主头像进入个人主页查看博主联系方式,由专业技术人员远程协助!

前言

这篇博客针对《Python OpenCV车道线识别侦测》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。

运行结果

文章目录

一、所需工具软件
二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助

一、所需工具软件

1. Pycharm
2. Python

二、使用步骤

代码如下(示例):

import cv2 as cv
import numpy as np


def do_canny(frame):
    gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
    blur = cv.GaussianBlur(gray, (5, 5), 0)
    canny = cv.Canny(blur, 50, 150)
    return canny

def do_segment(frame):
    height = frame.shape[0]
    polygons = np.array([
                            [(0, height), (800, height), (380, 290)]
                        ])
    mask = np.zeros_like(frame)
    cv.fillPoly(mask, polygons, 255)
    segment = cv.bitwise_and(frame, mask)
    return segment

def calculate_lines(frame, lines):
    left = []
    right = []
    for line in lines:
        x1, y1, x2, y2 = line.reshape(4)
        slope = parameters[0]
        y_intercept = parameters[1]

        if slope < 0:
            left.append((slope, y_intercept))
        else:
            right.append((slope, y_intercept))

    left_avg = np.average(left, axis = 0)
    right_avg = np.average(right, axis = 0)

    left_line = calculate_coordinates(frame, left_avg)
    right_line = calculate_coordinates(frame, right_avg)
    return np.array([left_line, right_line])

def calculate_coordinates(frame, parameters):
    slope, intercept = parameters
    y1 = frame.shape[0]
    y2 = int(y1 - 150)
    return np.array([x1, y1, x2, y2])

cap = cv.VideoCapture("input.mp4")
while (cap.isOpened()):
    ret, frame = cap.read()
    canny = do_canny(frame)
    cv.imshow("canny", canny)

    segment = do_segment(canny)
    hough = cv.HoughLinesP(segment, 2, np.pi / 180, 100, np.array([]), minLineLength = 100, maxLineGap = 50)

    lines = calculate_lines(frame, hough)
    lines_visualize = visualize_lines(frame, lines)
    cv.imshow("hough", lines_visualize)
    output = cv.addWeighted(frame, 0.9, lines_visualize, 1, 1)
    cv.imshow("output", output)
    if cv.waitKey(10) & 0xFF == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

运行结果

三、在线协助:

如需安装运行环境或远程调试,可点击博主头像,进入个人主页查看博主联系方式,由专业技术人员远程协助!

1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作

博主个人主页:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主所有文章点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主联系方式点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
相关文章
|
6天前
|
机器学习/深度学习 监控 自动驾驶
如何使用 Python 和 OpenCV 进行实时目标检测
如何使用 Python 和 OpenCV 进行实时目标检测
|
3天前
|
算法 计算机视觉 Python
python+opencv实现车牌定位
python+opencv实现车牌定位
|
6天前
|
机器学习/深度学习 监控 算法
使用Python和OpenCV实现简单的人脸识别系统
使用Python和OpenCV实现简单的人脸识别系统
14 0
|
10天前
|
算法 计算机视觉 Python
openCV 3计算机视觉 Python语言实现 笔记 第4章 深度估计与分割
openCV 3计算机视觉 Python语言实现 笔记 第4章 深度估计与分割
|
10天前
|
算法 计算机视觉 Python
openCV 3计算机视觉 Python语言实现 笔记 第三章 使用OpenCV 3处理图像
openCV 3计算机视觉 Python语言实现 笔记 第三章 使用OpenCV 3处理图像
|
10天前
|
计算机视觉 索引 Python
openCV 3计算机视觉 Python语言实现 笔记__第二章 处理文件、摄像头和图形用户界面
openCV 3计算机视觉 Python语言实现 笔记__第二章 处理文件、摄像头和图形用户界面
|
10天前
|
机器人 计算机视觉 Python
openCV 3计算机视觉 Python语言实现 笔记__第一章
openCV 3计算机视觉 Python语言实现 笔记__第一章
|
11天前
|
机器学习/深度学习 算法 数据库
【功能超全】基于OpenCV车牌识别停车场管理系统软件开发【含python源码+PyqtUI界面+功能详解】-车牌识别python 深度学习实战项目
【功能超全】基于OpenCV车牌识别停车场管理系统软件开发【含python源码+PyqtUI界面+功能详解】-车牌识别python 深度学习实战项目
|
11天前
|
机器学习/深度学习 编解码 文字识别
【开源】轻松实现车牌检测与识别:yolov8+paddleocr【python源码+数据集】
【开源】轻松实现车牌检测与识别:yolov8+paddleocr【python源码+数据集】
|
11天前
|
机器学习/深度学习 存储 监控
基于YOLOv8深度学习的120种犬类检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、狗类检测、犬种识别
基于YOLOv8深度学习的120种犬类检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、狗类检测、犬种识别