python使用h5py读取mat文件数据,并保存图像
2016-04-18 17:13
961 查看
1 安装h5py
假设你已经安装好python和numpy模块
2 读取mat文件数据
3 保存图像
整个代码流程:
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使用h5py读取mat文件数据
- Show me the code之Python练习册Q23-留言板
- 用Python实现感知机 (python机器学习一)
- Python 3学习笔记1
- Python 保留字符
- Python学习笔记(一)——基础知识
- 解决python中文编码混乱的问题
- python实现的html转xlsx,xlsx转html
- Python 入门学习笔记2
- Python函数
- 数据挖掘:python数据清洗cvs里面带中文字符
- python数据类型转换 python字符串转浮点数 python字符串转float
- Python学习笔记——文件
- Python下操作sqlite3
- python进阶学习笔记(四)--多线程thread
- python:if __name__ == '__main__': 的作用
- windows环境python安装第三方库
- python操作日期和时间的方法
- Python BDD 学习资源
- python之引入外援模块