我正在尝试阅读以下网站中给出的mat文件,ufldl.stanford.edu/housenumbers,在文件train.tar.gz中,有一个名为digitStruct.mat的mat文件。
当我使用 scipy.io 读取 mat 文件时,它会提醒我消息“请使用 hdf reader for matlab v7.3 files”。
原始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
如上所示,mat文件有键“digitStruct”,在“digitStruct”内可以找到键“name”和“bbox”,我使用h5py API来读取该文件。
import h5py
f = h5py.File('train.mat')
print len( f['digitStruct']['name'] ), len(f['digitStruct']['bbox'] )
我可以读取数组,但是当我循环数组时,如何读取每个项目?
for i in f['digitStruct']['name']:
print i # only print out the HDF5 ref