3
votes

I am trying to load .mat image exported from Tensorflow with Scipy.io using OpenCV.

I can modify the Tensorflow code to export the .mat image with only 3 channels directly but I will lose a lot of data and it doesn't look correct even.

And that's why I am trying to export the raw data as it is.

In my case I load the .mat file with scipy.io and get the numpy array which looks like this

(640, 640, 128)

and I want to reshape it because OpenCV cannot load an image with 128 channels.

(640, 640, 3)

I am not fully understanding the concept of reshaping and I think I am doing it wrong.

I am getting this error:

ValueError: cannot reshape array of size 52428800 into shape (640,640,3)

Thank you and have a good day,

Hesham


Edit 1: That's the code:

import cv2
import scipy.io as sio
import numpy as np

matfile = 'docia.mat'
img = sio.loadmat(matfile)

img_reshaped = img['embedmap'].reshape(640, 640, 3)

cv2.imshow("segmented_map", img['embedmap'])
cv2.waitKey(0)`
1
Please add some code you have tried - parlad
I edited the post with the code. - Mahmoud Hesham

1 Answers

2
votes

Re-shaping is using when you want to retain all of the data but in a different shape. I believe that you are trying to drop 125 of the 128 channels. To do this you can just use indexing to get the first 3 channels:

img_reshaped = img['embedmap'][:, :, :3]

Also you are passing img['embedmap'], not the reshaped img_reshaped into cv2.imshow().

Although I would recommend looking at them 1 by 1 in grey scale.

for i in range(128):
    cv2.imshow("segmented_map", img['embedmap'][:, :, i])
    cv2.waitKey(0)