如何将.mat转换为图像文件

时间:2017-06-09 16:49:32

标签: python scipy

我试图将SVHN格式2(32x32裁剪图像)数据集转换为图像目录。目前,当您下载SVHN图像的格式2时,它是.mat格式。如何更改此.mat格式以png或jpg格式提供所有图像?我已经使用SciPy加载了图像,但在那之后,我被卡住了。我知道SciPy从包含两个键(X和Y)的.mat文件中创建一个字典。我知道X是图像数据,Y是它们各自的标签。如何将此字典转换为所有裁剪图像?

import scipy.io

train_data = scipy.io.loadmat('train_32x32.mat')

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

import scipy.io

train_data = scipy.io.loadmat('train_32x32.mat', variable_names='X').get('X')
train_labels = scipy.io.loadmat('train_32x32.mat', variable_names='y').get('y')
test_data = scipy.io.loadmat('test_32x32.mat', variable_names='X').get('X')
test_labels = scipy.io.loadmat('test_32x32.mat', variable_names='y').get('y')

现在,如果你打印形状,你会看到......

print(train_data.shape, train_labels.shape)
print(test_data.shape, test_labels.shape)

结果是:

(32,32,3,73257)(73257,1)

(32,32,3,26032)(26032,1)

我建议使用这种形状:

train_dataset = train_data.transpose((3,0,1,2))
train_label = train_labels[:,0]
test_dataset = test_data.transpose((3,0,1,2))
test_label = test_labels[:,0]

print(train_dataset.shape, train_label.shape)
print(test_dataset.shape, test_label.shape)

现在的形状是:

(73257,32,32,3)(73257,)

(26032,32,32,3)(26032,)