import cv2 as cv
import os
import numpy as npimport time# 遍历文件夹函数
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, img):height = follow.shape[0] # 输入图像高度width = follow.shape[1] # 输入图像宽度half = int(width / 2) # 输入图像中线# 从下往上扫描赛道,最下端取图片中线为分割线for y in range(height - 1, -1, -1):if y == height - 1: # 刚开始从底部扫描时left = 0right = width - 1left_scale = 0.5 # 初始赛道追踪范围right_scale = 0.5 # 初始赛道追踪范围elif left == 0 and right == width - 1: # 下层没有扫描到赛道时left_scale = 0.25 # 赛道追踪范围right_scale = 0.25 # 赛道追踪范围elif left == 0: # 仅左下层没有扫描到赛道时left_scale = 0.25 # 赛道追踪范围right_scale = 0.2 # 赛道追踪范围elif right == width - 1: # 仅右下层没有扫描到赛道时left_scale = 0.2 # 赛道追踪范围right_scale = 0.25 # 赛道追踪范围else:left_scale = 0.2 # 赛道追踪范围right_scale = 0.2 # 赛道追踪范围# 根据下层左线位置和scale,设置左线扫描范围left_range = mask[y][max(0, left - int(left_scale * width)):min(left + int(left_scale * width), width - 1)]# 根据下层右线位置和scale,设置右线扫描范围right_range = mask[y][max(0, right - int(right_scale * width)):min(right + int(right_scale * width), width - 1)]# 左侧规定范围内未找到赛道if (left_range == np.zeros_like(left_range)).all():left = left # 取图片最左端为左线else:left = int((max(0, left - int(left_scale * width)) + np.average(np.where(left_range == 255))) * 0.4 + left * 0.6) # 取左侧规定范围内检测到赛道像素平均位置为左线# 右侧规定范围内未找到赛道if (right_range == np.zeros_like(right_range)).all():right = right # 取图片最右端为右线else:right = int((max(0, right - int(right_scale * width)) + np.average(np.where(right_range == 255))) * 0.4 + right * 0.6) # 取右侧规定范围内检测到赛道像素平均位置为右线mid = int((left + right) / 2) # 计算中点# follow[y, mid] = 255 # 画出拟合中线,实际使用时为提高性能可省略# img[y, max(0, left - int(left_scale * width)):min(left + int(left_scale * width), width - 1)] = [0, 0, 255]# img[y, max(0, right - int(right_scale * width)):min(right + int(right_scale * width), width - 1)] = [0, 0, 255]if y == int((360 / 480) * follow.shape[0]): # 设置指定提取中点的纵轴位置mid_output = midcv.circle(follow, (mid_output, int((360 / 480) * follow.shape[0])), 5, 255, -1) # opencv为(x,y),画出指定提取中点error = (half - mid_output) / width * 640 # 计算图片中点与指定提取中点的误差return follow, error, img # error为正数左转,为负数右转n = -1
# 存放图片的文件夹路径
path = "./d1"
imglist = getFileList(path, [])
for imgpath in imglist:n += 1if n < 0:continuestart_time = time.time()img = 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, img = mid(follow, mask, img)print(n, f"error:{error}")end_time = time.time()print("time:", end_time - start_time, "s")cv.imshow("img", img)cv.imshow("mask", mask)cv.imshow("follow", follow)cv.waitKey(0)cv.destroyAllWindows()