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

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

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):halfWidth= 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  # 计算拟合中点half = int(mid)  # 递归,从下往上确定分割线follow[y, int(mid)] = 255  # 画出拟合中线if y == 360:  # 设置指定提取中点的纵轴位置mid_output = int(mid)cv.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/107311.html

相关文章:

  • 软件测试环境讲解
  • mysql 面试
  • linux 安装FTP
  • 软考高项(六)项目管理概述 ★重点集萃★
  • 【vue】组件使用教训
  • 2023年华数杯数学建模B题思路代码分析 - 不透明制品最优配色方案设计
  • 百度飞桨助力高校培养AI大模型人才,2023年飞桨产学合作项目申报启动
  • 【NLP概念源和流】 02-稠密文档表示(第 2/20 部分)
  • 同构多核架构(SMP)和异构多核架构(AMP)
  • 【网络基础进阶之路】一文弄懂TCP的三次握手与四次断开
  • VUE框架、UNIAPP框架:vue2转vue3全面细节总结(1)vue框架:路由模式、路由跳转;uniapp框架:存储数据写法、引入数据写法;
  • GIS在地质灾害危险性评估与灾后重建中的应用教程
  • leetcode----JavaScript 详情题解(3)
  • PGsql-安装
  • 数据结构----c语言复习
  • 网络安全公司校招面试会面试那些问题?
  • vue element el-upload附件上传、在线预览、下载当前预览文件
  • 设计模式九:组合模式(Composite Pattern)
  • 【COlor传感器】通过扰动调制光传感实现智能光传输的占用分布估计研究(Matlab代码实现)
  • 微服务系列<3>---微服务的调用组件 rpc 远程调用
  • P1558 色板游戏
  • 大数据概论
  • 数据库访问中间件--springdata-jpa的基本使用
  • c++游戏制作指南(二):制作一个炫酷的启动界面(c++绘图)
  • spring.config.location 手动指定配置文件文件
  • 【uniapp 使用ECharts】
  • 数据结构--线性表2-2
  • 利用openTCS实现车辆调度系统(一)系统介绍
  • 销存管理系统ssm进销存仓库销售java jsp源代码mysql
  • 【Axure教程】移动端二级滑动选择器