当前位置:   article > 正文

python3_批量读取指定文件夹下的图像_图像预处理(切割、降维、特征提取)+ 决策树应用实例_python3批量读取图片文件

python3批量读取图片文件

1.调用的库

  1. # PIL - Image: python呈像库
  2. from PIL import Image
  3. import numpy as np
  4. import os
  5. import re

2.主函数思路

  1. def myMain():
  2. # 1.获取图片名称
  3. path = "./images/images/"
  4. imgNames = getName(path)
  5. # 2.加载图片
  6. data, labels = load_Img(path, imgNames)
  7. np.save("water_data.npy", data)
  8. np.save("water_labels.npy", labels)

3.模块函数getName(path)

  1. def getName(path):
  2. """
  3. 批量读取图片名称
  4. :param imgDir: 图片所在路径
  5. :param imgName: 图片名称
  6. :return: 图片名称列表
  7. """
  8. filenames = os.listdir(path)
  9. # Return a list of all non-overlapping matches in the string
  10. # def findall(pattern, string, flags=0)
  11. # pattern:待查找文件名称
  12. # string:查找位置
  13. imgNames = []
  14. for i in filenames:
  15. name = re.findall(r"\b\d_\d+\.jpg", i)
  16. imgNames.extend(name) # 直接添加元素
  17. return imgNames

4.模块函数imgToArray(r, g, b)

  1. def imgToArray(r, g, b):
  2. """
  3. 将图片转换为数组
  4. :param r: 红
  5. :param g: 绿
  6. :param b: 蓝
  7. :return: 数组
  8. """
  9. return [np.asarray(r), np.asarray(g), np.asarray(b)]

5.求图片的各阶颜色距Var(rd=None, gd=None, bd=None)

  1. def Var(rd=None, gd=None, bd=None):
  2. """
  3. 求图片的颜色距
  4. :param rd: 三基色
  5. :return: 三阶颜色距
  6. """
  7. r1 = np.mean(rd) # 一阶颜色距
  8. r2 = np.mean(gd) # 一阶颜色距
  9. r3 = np.mean(bd) # 一阶颜色距
  10. s4 = np.std(rd) # 二阶颜色距
  11. s5 = np.std(gd) # 二阶颜色距
  12. s6 = np.std(bd) # 二阶颜色距
  13. t1 = np.mean((rd - rd.mean())**3)
  14. t2 = np.mean((gd - gd.mean())**3)
  15. t3 = np.mean((bd - bd.mean())**3)
  16. return [r1, r2, r3, s4, s5, s6, np.sign(np.mean(t1)*abs(t1)**(1/3)), np.sign(np.mean(t2)*abs(t2)**(1/3)), np.sign(np.mean(t3)*abs(t3)**(1/3))]

6.模块函数load_Img(path, fileNameList)

  1. def load_Img(path, fileNameList):
  2. """
  3. 加载图片,并预处理
  4. :param fileNameList:
  5. :return:
  6. """
  7. n = len(fileNameList) # 获取图片数量
  8. data = np.zeros([n, 9]) # 样本自变量
  9. labels = np.zeros([n]) # 样本标签
  10. # 遍历每一张图片
  11. imgList = []
  12. for i in range(n):
  13. img = Image.open(path+fileNameList[i])
  14. M, N = img.size
  15. img = img.crop((M/2-50,N/2-50,M/2+50,N/2+50)) # 选取图片中心位置
  16. r, g, b = img.split() # 将颜色通道分开
  17. img_list = imgToArray(r, g, b) # 将图片转换为数组
  18. for j in range(9):
  19. data[i, j] = Var(img_list[0], img_list[1], img_list[2])[j]
  20. labels[i] = fileNameList[i][0] # 存储图片标签
  21. return data, labels

6.应用实例(决策树)

  1. import numpy as np
  2. from sklearn.tree import DecisionTreeClassifier
  3. from sklearn.model_selection import train_test_split
  4. from sklearn import metrics
  5. data = np.load("water_data.npy")
  6. labels = np.load("water_labels.npy")
  7. data_tr, data_te, target_tr, target_te = train_test_split(data, labels, test_size=0.2, random_state=10)
  8. model = DecisionTreeClassifier(random_state=10).fit(data_tr, target_tr)
  9. res = model.predict(data_te)
  10. pre = model.score(data_te, target_te)
  11. print(target_te)
  12. print(res)
  13. # 精度
  14. print(sum(res==target_te)/len(res))
  15. print(pre)
  16. # 报告
  17. print(metrics.classification_report(target_te, res))
  18. print(metrics.confusion_matrix(target_te, res))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/91053
推荐阅读
相关标签
  

闽ICP备14008679号