import cv2 as cv
import os
import numpy as np
def getFileList(dir, Filelist, ext=None):
"""
获取文件夹及其子文件夹中文件列表
输入 dir:文件夹根目录
输入 ext: 扩展名
返回: 文件路径列表
"""
newDir = dir
if 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 Filelist
def onmouse(event,x,y,flags,param):
cv.imshow("img", img)
if event == cv.EVENT_LBUTTONDBLCLK:
print("("+str(x)+","+str(y)+")的HSV为"+str(img_hsv[y,x]))
path = "D:\pythonProject18_fakeC++\\task1"
imglist = getFileList(path, [])
for imgpath in imglist:
img = cv.imread(imgpath)
img_hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)
cv.namedWindow("img")
cv.setMouseCallback("img", onmouse)
cv.waitKey(0)
cv.destroyAllWindows()