Python .mat图片文件读取

1. .mat文件用h5py读取

.mat格式的NYU v2数据集读取

import numpy as np
import h5py
import cv2


def h5_loader(path):
    h5f = h5py.File(path)
    # .mat文件里有哪些内容
    print(h5f)
    keys = list(h5f.keys())
    print("keys: ", keys)
    # images类别下的数据维度
    print("h5f['images'].shape: ", h5f['images'].shape)
    img = np.array(h5f['images'][0])
    print("img.shape: ", img.shape)
    img = np.transpose(img, (2, 1, 0))
    print("img.shape: ", img.shape)
    depth = np.array(h5f['depths'][0])
    print("depth.shape: :", depth.shape)
    return img, depth


if __name__ == '__main__':
    PATH = 'I:/datasets/NYU Depth Dataset v2/nyu_depth_v2_labeled.mat'
    img, depth = h5_loader(PATH)
    img = img.astype('float32')
    # 保存文件
    cv2.imwrite('1.png', img.astype(np.uint8))

结果展示:

<HDF5 file "nyu_depth_v2_labeled.mat" (mode r)>
keys:  ['#refs#', '#subsystem#', 'accelData', 'depths', 'images', 'instances', 'labels', 'names', 'namesToIds', 'rawDepthFilenames', 'rawDepths', 'rawRgbFilenames', 'sceneTypes', 'scenes']
h5f['images'].shape:  (1449, 3, 640, 480)
img.shape:  (3, 640, 480)
img.shape:  (480, 640, 3)
depth.shape: : (640, 480)

图片:

2. 结语

  努力去爱周围的每一个人,付出,不一定有收获,但是不付出就一定没有收获! 给街头卖艺的人零钱,不和深夜还在摆摊的小贩讨价还价。愿我的博客对你有所帮助(*^▽^*)(*^▽^*)!

  如果客官喜欢小生的园子,记得关注小生哟,小生会持续更新(#^.^#)(#^.^#)。

 

posted @ 2023-02-14 15:28  抚琴尘世客  阅读(525)  评论(0编辑  收藏  举报