2022年 11月 7日

python读取mat格式数据

一般而言,mat数据是在Matlab中存储的数据格式。
Matlab 中可以直接使用load()读取mat数据。
python中读取mat数据:

#首先导入包
from scipy.io import loadmat

#读取
filename = 'F:/img.mat'
image = loadmat(filename)
print(type(img))

>>><class 'dict'>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到直接读取得到的数据类型为字典类型,可是我们需要的是numpy.array。

img 的内容截图为:我们需要的是scene部分的图像数据
在这里插入图片描述


img2 = img['scene']
print(type(img2))
print(img2.shape)

>>><class 'numpy.ndarray'>
>>>(512, 512, 104)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

以上代码就得到了数组形式的mat数据。