将mat文件转为png
将图片转换为mat格式
将mat文件转为png花费了很大力气做这件事,总是出现各种错误,现在终于解决了
from PIL import Image
import matplotlib.pyplot as plt
import glob
import os
import numpy as np
import mat73
# 数据矩阵转图片的函数
def MatrixToImage(data):
data = data*255
new_im = Image.fromarray(data.astype(np.uint8))
return new_im
def mkdir(path):
folder = os.path.exists(path)
if not folder: # 判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) # makedirs 创建文件时如果路径不存在会创建这个路径
print("--- create new folder... ---")
else:
print("--- There is this folder! ---")
# Get all png files under the input folder
input_img_path = glob.glob("I:/CCCC--数据集/去噪/dnd_2017/input/*.mat")
save_path = "blur13x13/"
mkdir(save_path) # 调用函数
i = 0
for file in input_img_path:
file_name = file.split('\\')[-1]
try:
mat = mat73.loadmat(file)
new_name = str(mat.keys())
key_name = list(mat.keys())[-1]
key_name = mat[key_name]
print(key_name.shape)
new_im = MatrixToImage(key_name)
plt.imshow(key_name, interpolation='nearest')
new_im.save(save_path+'{}.webp'.format(file_name))
except Exception as e:
pass
i = i + 1
print("The", i, "picture is currently being processed")
continue
完整代码如上,只需要修改输入的mat文件夹路径即可~
将图片转换为mat格式import cv2
import numpy as np
import h5py
import math
import glob
import os
import scipy.io as io
def save_to_mat(img,output_name):
new_data_path = os.path.join(os.getcwd(),"matType")
if not os.path.isdir(new_data_path):
os.mkdir(new_data_path)
npy_data = np.array(img,dtype= "uint16")
np.save(new_data_path+'/{}.npy'.format(output_name),npy_data)
npy_load = np.load(new_data_path+'/{}.npy'.format(output_name))
io.savemat(new_data_path+'/{}.mat'.format(output_name),{'data':npy_load})
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易知道(ezd.cc)。