我有一个MATLAB.mat
文件,我想从中读取值并存储在python列表obect中。.mat
-文件包含我想要读取并存储在列表中的图像的像素值。
正如这篇文章所建议的那样,我使用scipy
进行了尝试:
from scipy.io import loadmat
annots = loadmat('C://Users//user//Downloads//OCR//dataset//Icdar.Train.Robust.all.mat')
print(annots['e'][0])
这是我得到的输出。
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))]
如何将这些数组值读取到python列表中?能否请您提供用于将这些像素值读取到列表中的python代码。谢谢
我不确定list
是否是存储图像像素值的最佳数据类型。
最合适的格式是NumPyndarray(对象(数据类型
这就是在加载.mat
文件时获取数组的原因。
在将数据保存到mat
文件之前,很难从您的帖子中判断出原始格式(在MATLAB中(。
我假设保存数据的代码如下所示:
I = imread('peppers.png');
e = {};
e{1} = [65, 65, 65, 57, 57, 57];
e{2} = I;
save('sample.mat', 'e');
我不确定e
是否应该是一个单元阵列e
的第一个元素是矢量(而不是图像(e
的第二个元素是类型为uint8
的3D(RGB(矩阵。
用于读取mat
文件并转换为两个列表的Python代码:
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
显示图像而不转换为列表:
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"