import cv2
import numpy as np
file = 'data_batch_1'
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo,encoding='latin1')
return dict
dict1 = unpickle(file)
for i in range(1000):#我只保存的100张
img = dict1["data"][i]#得到图片的数据
img = np.reshape(img, (3, 32,32)) #转为三维图片数组
img = img.transpose((1,2,0))#通道转换为CV2的要求形式
img_name = dict1["filenames"][i]#拿到图片的名字
img_label = str(dict1["labels"][i])#拿到图片的标签
cv2.imwrite("./p/"+img_label+"_"+img_name,img)#保存
python:cifar10的data_batch_1提取为png图片
最新推荐文章于 2023-08-01 17:21:52 发布
本文介绍了一种从CIFAR-10数据集中读取图像并将其转换为CV2格式的方法。通过使用pickle库加载数据批文件,对每张图像进行三维数组重塑,调整通道顺序,并保存到指定目录。此过程适用于批量处理大量图像数据。
2186

被折叠的 条评论
为什么被折叠?



