3

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)`
2
  • Please add some code you have tried Commented Sep 27, 2018 at 10:37
  • I edited the post with the code. Commented Sep 27, 2018 at 10:44

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! that's what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.