python打开mat图像文件_python使用h5py读取mat文件数据,并保存图像

1 安装h5py

sudo apt-get install libhdf5-dev

sudo pip install h5py

假设你已经安装好python和numpy模块

2 读取mat文件数据

import numpy as np

import h5py

f = h5py.File('data.mat')

data = f['cell_name'][:]

cell_name是元胞数组的名称,假如有多级元胞目录,可以指定任意的元胞数组进行读取,比如data = f['cell_name/.../指定的元胞数组'][:]

3 保存图像

img = images[i,...].transpose((2, 1, 0))

file = 'make3d_dataset_f460/images/'+str(i+1)+'.jpg'

img = img*255

img = img.astype('uint8')

cv2.imwrite(file, img)

#pyplot.imsave(file, img)

整个代码流程:

import cv2

import numpy as np

import h5py

from matplotlib import pyplot

height = 460

width = 345

def extract_data():

with h5py.File('make3d_dataset_f460.mat','r') as f:

images = f['make3d_dataset_fchange/images'][:]

image_num = len(images)

for i in range(image_num):

img = images[i,...].transpose((2, 1, 0))

file = 'make3d_dataset_f460/images/'+str(i+1)+'.jpg'

img = img*255

img = img.astype('uint8')

cv2.imwrite(file, img)

#pyplot.imsave(file, img)

def extract_labels():

with h5py.File('make3d_dataset_f460.mat','r') as f:

depths = f['make3d_dataset_fchange/depths'][:]

depth_num = len(depths)

for i in range(depth_num):

img = depths[i,...].transpose((1, 0))

file = 'make3d_dataset_f460/depths/'+str(i+1)+'.jpg'

depth = img

depth = depth.astype('uint8')

cv2.imwrite(file, depth)

#pyplot.imsave(file, img)

def main(argv=None):

# Input and groundtruth producer

extract_data()

extract_labels()

print("Training data is converted into images!")

if __name__ == '__main__':

main()

python读取mat文件

一.mat文件 mat数据格式是Matlab的数据存储的标准格式.在Matlab中主要使用load()函数导入一个mat文件,使用save()函数保存一个mat文件.对于文件 load('data.m ...

python中读取mat文件

mat数据格式是Matlab的数据存储的标准格式 在python中可以使用scipy.io中的函数loadmat()读取mat文件. import scipy.io as scio path = 'e ...

python 读取.mat文件

导入所需包 from scipy.io import loadmat 读取.mat文件 随便从下面文件里读取一个: m = loadmat('H_BETA.mat') # 读出来的 m 是一个dict ...

Java读取mat文件

概述 使用ujmp中的jmatio模块读取.mat文件到java程序中. 其实,ujmp主要是在模块core中实现了矩阵运算,其余模块都是复用了已有的开源库.模块jmatio是复用了已有的JMatIo ...

深入学习python解析并读取PDF文件内容的方法

这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

上传读取Excel文件数据

/// /// 上传读取Excel文件数据 /// 来自http://www.cnblogs.com/cielwater /// // ...

Unity 用C#脚本读取JSON文件数据

读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...

spring 框架的xml文件如何读取properties文件数据

spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值