[Python] 读取存储.mat数据 scipy.io.loadmat("FileName")和scipy.io.savemat('XXX.mat', {'x': x,'y': y,'z': z})

读取.mat数据

scipy.io.loadmat("FilePath")

功能:
读取路径为"FilePath".mat文件,函数返回值为字典类型dict。之后还需要通过后续操作将值提取出来(字典操作来提取键值对的值)。

关于为什么scipy.io.loadmat("FilePath")的结果是一个字典,这是因为一个.mat文件中存在多个变量,每一个变量名都对应相应的数据,也就是变量名和变量值的键值对

语法:

scipy.io.loadmat("FilePath")

示例(包括):

from scipy import io
import numpy as np

FilePath = "C:\Datasets\PaviaU\PaviaU_gt.mat"
data = io.loadmat(FilePath)
print(data)
# {'__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 20 18:25:52 2011', '__version__': '1.0', '__globals__': [], 'paviaU_gt':
# array([[0, 0, 0, ..., 0, 0, 0],
#        [0, 0, 0, ..., 0, 0, 0],
#        [0, 0, 0, ..., 0, 0, 0],
#        ...,
#        [2, 2, 2, ..., 0, 0, 0],
#        [2, 2, 2, ..., 0, 0, 0],
#        [2, 2, 2, ..., 0, 0, 0]], dtype=uint8)}
print(type(data))
# <class 'dict'>

PaviaU_gt_load = data['paviaU_gt']
print(PaviaU_gt_load)
# [[0 0 0 ... 0 0 0]
#  [0 0 0 ... 0 0 0]
#  [0 0 0 ... 0 0 0]
#  ...
#  [2 2 2 ... 0 0 0]
#  [2 2 2 ... 0 0 0]
#  [2 2 2 ... 0 0 0]]
print(type(PaviaU_gt_load))
# <class 'numpy.ndarray'>
print(np.shape(PaviaU_gt_load))
# (610, 340)

可以看到,在data = io.loadmat(FilePath)之后,data是字典类型。这个字典只存在一个键值对:

  • 键:’paviaU_gt’,字符串类型。(大写字母被降为小写字母)

  • 值:

    array([[0, 0, 0, ..., 0, 0, 0],
      	     [0, 0, 0, ..., 0, 0, 0],
             [0, 0, 0, ..., 0, 0, 0],
             ...,
             [2, 2, 2, ..., 0, 0, 0],
             [2, 2, 2, ..., 0, 0, 0],
             [2, 2, 2, ..., 0, 0, 0]], dtype=uint8)}
    

然后通过字典操作PaviaU_gt_load = data['paviaU_gt'],得到字典data的键paviaU_gt的对应的值,返回值为numpy.ndarray类型。

array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [2, 2, 2, ..., 0, 0, 0],
       [2, 2, 2, ..., 0, 0, 0],
       [2, 2, 2, ..., 0, 0, 0]], dtype=uint8)}

存储.mat类型

io.savemat('SavedData.mat',{'key1':data1, 'key2':data2})

功能:
ndarray类型的数据data1data2key1key2为变量名,保存在SavedData.mat中。
其中SavedData.mat会被保存到项目文件下(该.py文件的相同路径)。

语法:

io.savemat('SavedData.mat',{'key1':data1, 'key2':data2})

示例:

from scipy import io
import numpy as np
x = np.array([1,2,3,4,5,6,7,8,9])
x = x.reshape(3,3)
print(x)
print(x.shape)

io.savemat('SavedData.mat',{'x':x})

在这里插入图片描述
保存路径
在这里插入图片描述

  • 10
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用OpenCV和SciPy的函数来实现将Mat数据保存为PythonScipy.io.savemat格式。下面是一个简单的示例代码: ```c++ #include <opencv2/opencv.hpp> #include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <cmath> #include <cstdlib> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <boost/filesystem.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/algorithm/string.hpp> #include "json.hpp" using namespace std; using namespace cv; namespace pt = boost::property_tree; using json = nlohmann::json; int main(int argc, char** argv) { // 读取Mat数据 Mat img = imread("test.jpg"); // 将Mat数据转换为数组形式 vector<int> img_data; for(int i=0;i<img.rows;i++) { for(int j=0;j<img.cols;j++) { Vec3b pixel = img.at<Vec3b>(i, j); img_data.push_back(pixel[0]); img_data.push_back(pixel[1]); img_data.push_back(pixel[2]); } } // 定义保存数据的json对象 json data; data["img"] = img_data; // 将json对象保存为文件 ofstream out_file("test.mat"); out_file << data; out_file.close(); return 0; } ``` 这个示例程序将OpenCV读取的图像数据转换为一个vector<int>形式的数组,然后将这个数组保存到一个json对象,最后使用ofstream将这个json对象保存到文件。由于Scipy.io.savemat格式也是以JSON格式保存数据的,所以这个程序可以将Mat数据保存为Scipy.io.savemat格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值