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

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

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/107034.html

相关文章:

  • vite+typescript项目 :找不到模块“./***.vue”或其相应的类型声明——解决方案
  • Gradio-YOLOv5-YOLOv7 搭建Web GUI
  • HTML模板生成word,pdf文档
  • ssl单向证书和双向证书校验测试及搭建流程
  • 【2种方法,jmeter用一个正则提取器提取多个值!】
  • 012-堆,结构体
  • GDAL C++ API 学习之路 OGRGeometry 多边形类 OGRPolygon
  • 文件传输协议FTP与托管文件传输MFT有什么区别?
  • js实现按照句号将一段文本进行分段
  • 环形链表的进一步探究
  • flink任务性能优化
  • vue2 el-carousel轮播图和文字一起改变
  • LangChain:打造自己的LLM应用 | 京东云技术团队
  • 字节跳动测试岗,3面都过了,HR告诉我这个原因被刷了...
  • Android 14重要更新预览
  • 快速上手字符串函数
  • linux(centos) docker 安装 nginx
  • SpringBoot 整合 Minio
  • 《吐血整理》高级系列教程-吃透Fiddler抓包教程(24)-Fiddler如何优雅地在正式和测试环境之间来回切换-中篇
  • 探索 GPTCache|GPT-4 将开启多模态 AI 时代,GPTCache + Milvus 带来省钱秘籍
  • 纯css实现登录表单动效
  • 【css】外边距margin
  • Cpp8 — 二叉搜索树
  • 【实操教程】如何开始用Qt Widgets编程?(一)
  • openmp和avx配置
  • 18 个JS优化技巧,可以解决 90% 的屎山代码!!!
  • go逆向符号恢复
  • 论文阅读- Uncovering Coordinated Networks on Social Media:Methods and Case Studies
  • 应急响应-Linux
  • 利用spinal的伴生对象简化集成rtl代码过程