简体   繁体   English

如何从 .mat 文件中读取 python 列表?

[英]How to read from a .mat file into a python list?

I have a MATLAB .mat -file from which I want to read values and store in a python list obect.我有一个 MATLAB .mat文件,我想从中读取值并存储在 python 列表对象中。 The .mat -file contains pixel values of images which I wish to read and store in a list. .mat文件包含我希望读取并存储在列表中的图像的像素值。

I tried it using scipy as this post suggests:正如这篇文章所暗示的,我使用scipy进行了尝试:

from scipy.io import loadmat
annots = loadmat('C://Users//user//Downloads//OCR//dataset//Icdar.Train.Robust.all.mat')
print(annots['e'][0])

This is the output I get.这是我得到的 output。

runfile('C:/Users/user/Downloads/OCR/OCR_try/TextRecogUnsupervised/try.py', wdir='C:/Users/user/Downloads/OCR/OCR_try/TextRecogUnsupervised')
[(array([[65, 65, 65, ..., 57, 57, 57]], dtype=uint8), array([[[ 83, 178, 146, ...,  53,  62, 169],
        [131, 179, 150, ...,  57,  62, 168],
        [191, 182, 155, ...,  55,  63, 168],
        ...,
        [173, 167, 174, ...,  49,  45, 129],
        [179, 170, 164, ...,  46,  43, 122],
        [181, 173, 160, ...,  50,  41, 120]],

       [[136, 177, 143, ...,  65,  56, 158],
        [190, 178, 147, ...,  66,  59, 159],
        [202, 180, 151, ...,  63,  61, 164],
        ...,
        [183, 168, 173, ...,  50,  42, 126],
        [187, 171, 161, ...,  48,  45, 121],
        [191, 173, 156, ...,  51,  45, 120]],

       [[186, 178, 140, ...,  95,  47, 140],
        [187, 177, 142, ...,  96,  52, 148],
        [180, 176, 145, ...,  99,  56, 162],
        ...,
        [185, 172, 170, ...,  51,  42, 119],
        [185, 173, 156, ...,  51,  48, 118],
        [190, 174, 150, ...,  51,  52, 116]],

       ...,

       [[108, 137, 138, ...,  99,  49, 100],
        [ 96, 130, 140, ...,  93,  49, 105],
        [103, 122, 139, ...,  88,  48, 115],
        ...,
        [184, 154, 137, ...,  58,  46,  95],
        [185, 154, 140, ...,  57,  45,  97],
        [188, 151, 144, ...,  56,  44,  98]],

       [[129, 142, 151, ...,  93,  54, 104],
        [118, 143, 154, ...,  89,  51, 109],
        [105, 144, 153, ...,  88,  48, 114],
        ...,
        [194, 167, 151, ...,  59,  44,  95],
        [184, 169, 153, ...,  59,  43,  95],
        [185, 166, 156, ...,  58,  42,  94]],

       [[202, 148, 158, ...,  95,  55, 107],
        [180, 155, 162, ...,  93,  53, 109],
        [159, 162, 161, ...,  93,  49, 111],
        ...,
        [179, 175, 157, ...,  60,  44,  95],
        [173, 178, 159, ...,  60,  43,  94],
        [184, 175, 161, ...,  59,  41,  91]]], dtype=uint8))]

How do I read these array values into a python list?如何将这些数组值读入 python 列表? Could you please provide the python code for reading these pixel values into the list.您能否提供 python 代码以将这些像素值读入列表。 Thank you谢谢

I am not sure that list is the best data type for storing the pixel values of the images.我不确定该list是存储图像像素值的最佳数据类型。

The most suitable format is NumPy ndarray (object) data type.最合适的格式是 NumPy ndarray(对象)数据类型。
That is the reason why you are getting arrays when loading the .mat file.这就是加载.mat文件时获得 arrays 的原因。

It's hard to tell from your post what was the original format (in MATLAB) before saving the data to mat file.在将数据保存到mat文件之前,很难从您的帖子中分辨出原始格式(在 MATLAB 中)是什么。

I assume the code for saving the data looks as follows:我假设保存数据的代码如下所示:

I = imread('peppers.png');
e = {};
e{1} = [65, 65, 65, 57, 57, 57];
e{2} = I;

save('sample.mat', 'e');

I am not sure if e should be a cell array.我不确定e是否应该是一个单元格数组。
The fist element of e is a vector (not an image). e的第一个元素是向量(不是图像)。
The second element of e is a 3D (RGB) matrix of type uint8 . e的第二个元素是uint8类型的 3D (RGB) 矩阵。

Python code for reading the mat file and converting to two lists: Python 代码用于读取mat文件并转换为两个列表:

from scipy.io import loadmat

annots = loadmat('sample.mat')

#print(annots['e'][0])

a = list(annots['e'][0][0])  # Vector elements
b = list(annots['e'][0][1])  # Image pixel elements

Showing the image without converting to a list:显示图像而不转换为列表:

from matplotlib import pyplot as plt
from scipy.io import loadmat

annots = loadmat('sample.mat')

# The image in located at the second index of annots['e'][0]
img = annots['e'][0][1]

plt.imshow(img)
plt.show(block=True) # Show image with "blocking"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM