数据如上所示,主文件夹下有/home/kls/Desktop/data/ucf101_fea_video/51_v_Lungers_g21_c05.mat,mat第一维为fea_tmp也是一个mat,第二维为fea_spa也是一个mat,我们要将fea_tmp转换为jpg
import cv2
import scipy.io as scio
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os
# 数据矩阵转图片的函数
def MatrixToImage(data):
new_im = Image.fromarray(data,mode='RGB')
return new_im
folder = r'/home/kls/Desktop/data/ucf101_fea_video/'
path = os.listdir(folder)
for each_mat in path:
first_name, second_name = os.path.splitext(each_mat)
# 拆分.mat文件的前后缀名字,注意是**路径**
#yong yu fen ge wen jian min yu kuo zhan min
each_mat = os.path.join(folder, each_mat)
# print(each_mat)
array_struct = scio.loadmat(each_mat)
fea_spa=array_struct['fea_spa']
fea_tmp=array_struct['fea_tmp']
for i in range(1,len(fea_spa)):
fea_spa_1=np.reshape(fea_spa[i],(32,32))
print('i=',i)
fea_spa_image = MatrixToImage(fea_spa_1)# 调用函数
path='/home/kls/Desktop/data/fea_spa/'+first_name+'/'
if os.path.exists(path) is False:
os.makedirs(path)
fea_spa_image.save(path+str(i)+'.jpg')# 保存图片
fea_tmp_1=np.reshape(fea_tmp[i],(32,32))
print('i1=',i)
fea_tmp_image = MatrixToImage(fea_tmp_1)# 调用函数
path='/home/kls/Desktop/data/fea_tmp/'+first_name+'/'
if os.path.exists(path) is False:
os.makedirs(path)
fea_tmp_image.save(path+str(i)+'.jpg')# 保存图片