1

I have a MATLAB .mat-file from which I want to read values and store in a python list obect. The .mat-file contains pixel values of images which I wish to read and store in a list.

I tried it using scipy as this post suggests:

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.

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? Could you please provide the python code for reading these pixel values into the list. Thank you

max
  • 3,915
  • 2
  • 9
  • 25
Deveshi
  • 61
  • 3

1 Answers1

0

I am not sure that list is the best data type for storing the pixel values of the images.

The most suitable format is NumPy ndarray (object) data type.
That is the reason why you are getting arrays when loading the .mat file.

It's hard to tell from your post what was the original format (in MATLAB) before saving the data to mat file.

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.
The fist element of e is a vector (not an image).
The second element of e is a 3D (RGB) matrix of type uint8.

Python code for reading the mat file and converting to two lists:

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"
Rotem
  • 30,366
  • 4
  • 32
  • 65