文件的结构为组+数据集
其中组可以是多级的,类似文件夹的目录关系。
首先打开对应文件:
// An highlighted block
import h5py
import numpy as np
from PIL import Image
h5_file = "D:/GoProEvent/GOPRO/GOPRO/train/GOPR0372_07_00.h5"
file = h5py.File(h5_file, "r")
groups = [key for key in file.keys()]
print("该文件共有以下几组:", groups)
输出结果:
用file[‘group’]来“打开对应目录”
如果是多级group可以将相关信息打印出来查看:
// An highlighted block
h5_file = "D:/GoProEvent/GOPRO/GOPRO/train/GOPR0372_07_00.h5"
file = h5py.File(h5_file, "r")
groups = [key for key in file.keys()]
print("该文件共有以下几组:", groups)
a = file['masks']
agroup = [key for key in a.keys()]
print("该文件共有以下几组:", agroup)
输出结果:
通过对应的组别名称获取相应的数据:
对数据所在的group进行操作,对应以上代码即‘mask000000000’内获取数据。可以理解为名称为mask000000000的文件内的东西就是你要数据。(这里也可以理解为图像,只不过你需要的不是图像,而是图像内的像素集合)
// An highlighted block
for fg in a:
print("group:"+fg)
print(file['masks'][fg][:].shape)
img = file['masks'][fg][:]
print(img)
部分输出:
得到数据后,利用PIL依赖将数据保存为图片格式。
// An highlighted block
conut = 0
for fg in a:
print("group:"+fg)
print(file['masks'][fg][:].shape)
img = file['masks'][fg][:]
#print(type(img))
save_path = 'D:/GoProEvent/masks/' + str(conut) + ".png"
img = img.swapaxes(0, 1) //调整维度顺序
img = img.swapaxes(1, 2)
img = np.squeeze(img)
print(img.shape)
img = Image.fromarray(np.uint8(img)).convert("L")//针对单通道处理
img.save(save_path)
conut = conut + 1
需要注意的是给出的例子是一个单通道的灰度图。针对图像处理的Image.fromarray()函数貌似只能处理3通道或者是二维数组,所以利用img = np.squeeze(img)对通道数进行了处理。同时需要对初始数据维度进行校准,需要将维度顺序改为 HWC
如果需要对三通道进行处理,修改部分代码为:展示所有代码
// An highlighted block
import h5py
import numpy as np
from PIL import Image
h5_file = "D:/GoProEvent/GOPRO/GOPRO/train/GOPR0372_07_00.h5"
file = h5py.File(h5_file, "r")
groups = [key for key in file.keys()]
print("该文件共有以下几组:", groups)
a = file['masks']
conut = 0
for fg in a:
print("group:"+fg)
img = file['masks'][fg][:]
save_path = 'D:/GoProEvent/masks/' + str(conut) + ".png"
img = img.swapaxes(0, 1)
img = img.swapaxes(1, 2)
img = Image.fromarray(np.uint8(img))
img.save(save_path)
conut = conut + 1