当前位置: 首页 > news >正文

[openCV]基于拟合中线的智能车巡线方案V4

import cv2 as cv
import os
import numpy as np# 遍历文件夹函数
def getFileList(dir, Filelist, ext=None):"""获取文件夹及其子文件夹中文件列表输入 dir:文件夹根目录输入 ext: 扩展名返回: 文件路径列表"""newDir = dirif os.path.isfile(dir):if ext is None:Filelist.append(dir)else:if ext in dir[-3:]:Filelist.append(dir)elif os.path.isdir(dir):for s in os.listdir(dir):newDir = os.path.join(dir, s)getFileList(newDir, Filelist, ext)return Filelistdef mid(follow, mask):crossroads = FalsehalfWidth = follow.shape[1] // 2half = halfWidth  # 从下往上扫描赛道,最下端取图片中线为分割线for y in range(follow.shape[0] - 1, -1, -1):# V2改动:加入分割线左右各半张图片的宽度作为约束,减小邻近赛道的干扰if (mask[y][max(0, half - halfWidth):half] == np.zeros_like(mask[y][max(0, half - halfWidth):half])).all():  # 分割线左端无赛道left = max(0, half - halfWidth)  # 取图片左边界else:left = np.average(np.where(mask[y][0:half] == 255))  # 计算分割线左端平均位置if (mask[y][half:min(follow.shape[1], half + halfWidth)] == np.zeros_like(mask[y][half:min(follow.shape[1], half + halfWidth)])).all():  # 分割线右端无赛道right = min(follow.shape[1], half + halfWidth)  # 取图片右边界else:right = np.average(np.where(mask[y][half:follow.shape[1]] == 255)) + half  # 计算分割线右端平均位置mid = (left + right) // 2  # 计算拟合中点vibra = abs(mid - half)  # 振荡偏差# V3改动:检测到异常振荡则判定为十字路口,并保持直行if vibra > 30 and y < 479:crossroads = Truemid = int(mid)half = mid  # 递归,从下往上确定分割线follow[y, mid] = 255  # 画出拟合中线if y == 360:  # 设置指定提取中点的纵轴位置mid_output = midif crossroads:print("crossroads!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")mid_output = halfWidthcv.circle(follow, (mid_output, 360), 5, 255, -1)  # opencv为(x,y),画出指定提取中点error = follow.shape[1] // 2 - mid_output  # 计算图片中点与指定提取中点的误差return follow, error  # error为正数右转,为负数左转n = -1
# 存放图片的文件夹路径
path = "./phone"
imglist = getFileList(path, [])
for imgpath in imglist:n += 1if n < 0:continueimg = cv.imread(imgpath)img = cv.resize(img, (640, 480))# HSV阈值分割img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)mask = cv.inRange(img_hsv, np.array([43, 60, 90]), np.array([62, 255, 255]))follow = mask.copy()follow, error = mid(follow, mask)print(n, f"error:{error}")cv.imshow("img", img)cv.imshow("mask", mask)cv.imshow("follow", follow)cv.waitKey(0)cv.destroyAllWindows()

http://www.lryc.cn/news/106367.html

相关文章:

  • 【网络云盘客户端】——上传文件的功能的实现
  • WebView2对比CefSharp的超强优势
  • 前端需要知道的计算机网络知识
  • [2023杭电多校5 1005] Snake (生成函数)
  • 【MyBtis】各种查询功能
  • H5打包封装小程序系统开发
  • SpringBoot集成jasypt,加密yml配置文件
  • 【C++】模板(初阶)
  • windows下的txt文档,传到ubuntu后,每行后面出现^M,怎么处理?
  • LabVIEW FPGA开发实时滑动摩擦系统
  • Prometheus服务器、Prometheus被监控端、Grafana、Prometheus服务器、Prometheus被监控端、Grafana
  • 常见的锁策略(面试八股文)
  • SO_KEEPALIVE、TCP_KEEPIDLE、TCP_KEEPINTVL、保活包
  • 【phaser微信抖音小游戏开发005】画布上添加图片
  • 【设计模式——学习笔记】23种设计模式——外观模式Facade(原理讲解+应用场景介绍+案例介绍+Java代码实现)
  • 消息队列 -提供上层服务接口
  • maven引入本地jar包的简单方式【IDEA】【SpringBoot】
  • 【爬虫逆向案例】某易云音乐(评论)js逆向—— params、encSecKey解密
  • 【uni-app】【Android studio】手把手教你运行uniapp项目到Android App
  • 多线程(JavaEE初阶系列6)
  • shell清理redis模糊匹配的多个key
  • 【电网异物检测硕士论文摘抄记录】电力巡检图像中基于深度学习的异物检测方法研究
  • C++共享数据的保护
  • MyBatisPlus学习记录
  • 如何开启一个java微服务工程
  • libhv之hio_t分析
  • C语言的转义字符
  • 【腾讯云 Cloud Studio 实战训练营】CloudStudio体验真正的现代化开发方式,双手插兜不知道什么叫对手!
  • Pandas时序数据分析实践—时序数据集
  • use strict 是什么意思?使用它区别是什么?