Python 读取指定文件夹下的所有图像方法

时间:2022-08-26 22:57:12

(1)数据准备

数据集介绍:

数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标号为样本标签,"."后的标号为样本序号

(2)利用python读取文件夹中所有图像

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'''
load the image files form the folder
input:
  imgdir: the direction of the folder
  imgname:the name of the folder
output:
  data:the data of the dataset
  label:the label of the datset
'''
def load_img(imgdir,imgfoldname):
  imgs = os.listdir(imgdir+imgfoldname)
  imgnum = len(imgs)
  data = np.empty((imgnum,1,12,12),dtype="float32")
  label = np.empty((imgnum,),dtype="uint8")
  for i in range (imgnum):
    img = image.open(imgdir+imgfoldname+"/"+imgs[i])
    arr = np.asarray(img,dtype="float32")
    data[i,:,:,:] = arr
    label[i] = int(imgs[i].split('.')[0])
  return data,label

这里得到的data和label都是ndarray数据

data: (1223,1,12,12)

Python 读取指定文件夹下的所有图像方法

label:(1223,)

Python 读取指定文件夹下的所有图像方法

注:nddary数据类型是numpy提供的一个数据类型,即n-dimensional array,它弥补了python中array不支持多维的缺陷

(3)调用方式

?
1
2
3
craterdir = "./data/craterimg/adjust/"
foldname = "east_crateradjust12"
data, label = load_img(craterdir,foldname)

以上这篇python 读取指定文件夹下的所有图像方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/tina_ttl/article/details/51034831