sabrinazuraimi
sabrinazuraimi

Reputation: 735

Display image from Matlab mat file on Python

I'm currently trying to display an image from a Mat file downloaded from this site.

It's a .mat file so I tried loading it using spicy.io's load mat function but I can't seem to plot the image. What am I doing wrong?

import scipy
import sklearn
from sklearn.feature_extraction import image
from scipy.io import loadmat
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
images = loadmat('IMAGES.mat',variable_names='IMAGES',appendmat=True).get('IMAGES')

imgplot = plt.imshow(images[0])

plt.show()

What I get is this instead of an image

enter image description here

By the way, the output of images[0] is

array([[ 0.34344572,  0.12458728, -0.16325758, ...,  0.66119415,
         0.10742886,  0.45598051],
       [ 0.20327842,  0.12588207,  0.14950003, ...,  0.74659014,
        -0.04141214,  0.39051244],
       [ 0.27077097, -0.01716869,  0.27221447, ...,  0.83127338,
        -0.16380881,  0.42533499],
       ...,
       [-0.5974496 , -0.85362321, -0.30514711, ...,  0.41944146,
        -0.00949729,  0.49504656],
       [-0.58272219, -0.89612395,  0.22482066, ...,  0.31972334,
        -0.02557803,  0.47179163],
       [ 0.19115125, -0.68936276,  0.04859381, ...,  0.41277626,
        -0.08376407,  0.56580657]])

Thank you very much in advance!

Upvotes: 4

Views: 10050

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

You need a complete 3D slice and hence you should be using images[:,:,0]. i.e

imgplot = plt.imshow(images[:,:,0])

which gives:

result

Upvotes: 3

Related Questions