python讀取mat數據集


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)

輸出結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM