以http://ufldl.stanford.edu/housenumbers/上的mat数据集为例
需要注意以下几点
从mat提取出来的数据以字典的形式保存,所以需要提取字典的key和value
import numpy as np
import os
from PIL import Image
import matplotlib.pyplot as plt
import scipy.io as scio
data_train=scio.loadmat(r'data/train_32x32.mat')
#print(data_train)
#取出需要的数据矩阵
data_train_data=data_train.get('X')
#print(data_train_data)
#输出字典里的X
#data_train_label=data_train.get('y')#提取标签
#print(len(data_train_label))
for i in range(10):
plt.subplot(2,5,i+1)
#plt.title(data_train_label[i][0])
#print("标签"+str(data_train_label[i][0]))
plt.imshow(data_train_data[...,i])
#print("数据"+str(data_train_data[...,i])+"------")
plt.axis('off')
plt.savefig('picture/1.png')
plt.show()
输出结果
批量处理
将mat文件里的所有图片进行输出并保存在不同的画布中
import numpy as np
import os
from PIL import Image
import matplotlib.pyplot as plt
import scipy.io as scio
data_train=scio.loadmat(r'data/train_32x32.mat')
print(data_train)
#取出需要的数据矩阵
data_train_data=data_train.get('X')
print(data_train_data)
#输出字典里的X
data_train_label=data_train.get('y')#提取标签
print(len(data_train_label))
for i in range(10):#如果要全部输出,这里应该改成73256
#plt.subplot(1,1,i+1)
#plt.figure()
plt.title(data_train_label[i][0])
print("标签"+str(data_train_label[i][0]))
plt.imshow(data_train_data[...,i])
print("数据"+str(data_train_data[...,i])+"------")
plt.axis('off')
plt.savefig('picture/' +str(i)+'.png')
plt.show()
#此函数读取特定文件夹下的mat格式图像,返回图片所在路径的列表
def get_imlist(path):
for f in os.listdir(path):
if f.endswith('.mat'):
return os.path.join(path,f)#路径加起来
#print(os.path.join(path,f))
c=get_imlist(path)
print(c)
d=len(path)
print(d)
输出结果