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

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

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):half = follow.shape[1] // 2  # 从下往上扫描赛道,最下端取图片中线为分割线for y in range(follow.shape[0] - 1, -1, -1):if (mask[y][0:half] == np.zeros_like(mask[y][0:half])).all():  # 分割线左端无赛道left = 0  # 取图片左边界为赛道左边界else:left = np.average(np.where(mask[y][0:half] == 255))  # 计算分割线左端平均位置if (mask[y][half:follow.shape[1]] == np.zeros_like(mask[y][half:follow.shape[1]])).all():  # 分割线右端无赛道right = follow.shape[1]  # 取图片右边界为赛道右边界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 = "./B"
imglist = getFileList(path, [])
for imgpath in imglist:n += 1if n < 63:continueimg = cv.imread(imgpath)# 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/107548.html

相关文章:

  • MyBatis-Plus 和达梦数据库实现高效数据持久化
  • 已注销【888】
  • Ceph错误汇总
  • DataTable过滤某些数据
  • JAVASE---继承和多态
  • Centos7升级gcc、g++版本(转载)
  • 第一章:继承
  • git面试题
  • Github Copilot在JetBrains软件中登录Github失败的解决方案
  • 使用 github 同步谷歌浏览器书签
  • Eclipse进行debug
  • 13-5_Qt 5.9 C++开发指南_基于信号量的线程同步_Semaphore
  • golang使用泛型实现mapreduce操作
  • 2023华数杯数学建模C题思路分析 - 母亲身心健康对婴儿成长的影响
  • 【汇总】解决Ajax请求后端接口,返回ModelAndView页面不跳转
  • 网络安全进阶学习第九课——SQL注入介绍
  • 一个计算机专业的学生数据结构这门课学到什么程度才能算学的还不错?
  • [语义分割] ASPP不同版本对比(DeepLab、DeepLab v1、DeepLab v2、DeepLab v3、DeepLab v3+、LR-ASPP)
  • anaconda创建虚拟环境在D盘
  • Java设计模式之工厂设计模式
  • uniapp使用阿里图标
  • 20230803激活手机realme GT Neo3
  • Spring Cloud Feign+Ribbon的超时机制
  • 使用docker 搭建nginx + tomcat 集群
  • 从Spring的角度看Memcached和Redis及操作
  • 【C语言学习】C语言的基础数据类型
  • 使用AIGC工具提升安全工作效率
  • HBase概述
  • el-popover全屏不显示(bug记录)
  • react中使用redux-persist做持久化储存