use*_*624 5 python matlab hdf5 h5py mat
我正在尝试阅读以下网站ufldl.stanford.edu/housenumbers中提供的mat文件,在文件train.tar.gz中,有一个名为digitStruct.mat的mat文件。
当我使用scipy.io读取mat文件时,它以消息“请为matlab v7.3文件使用hdf阅读器”提醒我。
原始的matlab文件提供如下
load digitStruct.mat
for i = 1:length(digitStruct)
im = imread([digitStruct(i).name]);
for j = 1:length(digitStruct(i).bbox)
[height, width] = size(im);
aa = max(digitStruct(i).bbox(j).top+1,1);
bb = min(digitStruct(i).bbox(j).top+digitStruct(i).bbox(j).height, height);
cc = max(digitStruct(i).bbox(j).left+1,1);
dd = min(digitStruct(i).bbox(j).left+digitStruct(i).bbox(j).width, width);
imshow(im(aa:bb, cc:dd, :));
fprintf('%d\n',digitStruct(i).bbox(j).label );
pause;
end
end
Run Code Online (Sandbox Code Playgroud)
如上所示,mat文件具有键“ digitStruct”,并且在“ digitStruct”中可以找到键“ name”和“ bbox”,我使用h5py API读取了文件。
import h5py
f = h5py.File('train.mat')
print len( f['digitStruct']['name'] ), len(f['digitStruct']['bbox'] )
Run Code Online (Sandbox Code Playgroud)
我可以读取数组,但是当我遍历数组时,如何读取每个项目?
for i in f['digitStruct']['name']:
print i # only print out the HDF5 ref
Run Code Online (Sandbox Code Playgroud)
用Matlab编写:
test = {'Hello', 'world!'; 'Good', 'morning'; 'See', 'you!'};
save('data.mat', 'test', '-v7.3') % v7.3 so that it is readable by h5py
Run Code Online (Sandbox Code Playgroud)

用Python读取(适用于任何数字或行或列,但假设每个单元格都是一个字符串):
import h5py
import numpy as np
data = []
with h5py.File("data.mat") as f:
for column in f['test']:
row_data = []
for row_number in range(len(column)):
row_data.append(''.join(map(unichr, f[column[row_number]][:])))
data.append(row_data)
print data
print np.transpose(data)
Run Code Online (Sandbox Code Playgroud)
输出:
[[u'Hello', u'Good', u'See'], [u'world!', u'morning', u'you!']]
[[u'Hello' u'world!']
[u'Good' u'morning']
[u'See' u'you!']]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8792 次 |
| 最近记录: |