Paola
Paola

Reputation: 33

How do I reshape array of images obtained from .h5 file in python?

I'm new to python and I apologize in advance if my question seems trivial.

I have a .h5 file containing pairs of images in greyscale organized in match and non-match groups. For my final purpose I need to consider each pair of images as a single image of 2 channels (where each channel is in fact an image).

To use my data I proceed like this:

  1. I read the .h5 file putting my data in numpy arrays (I read both groups match and non-match, both with shape (50000,4096)):

     with h5py.File('supervised_64x64.h5','r') as hf:
         match = hf.get('/match')
         non_match = hf.get('/non-match')
         np_m = np.asarray(match)
         np_nm = np.asarray(non_match)
         hf.close()
    
  2. Then I try to reshape the arrays:

    reshaped_data_m = np_m.reshape(250000,2,4096)
    

Now, if I reshape the arrays as (250000,2,4096) and then I try to show the corresponding images what I get is actually right. But I need to reshape the arrays as (25000,64,64,2) and when I try to do this I get all black images.

Can you help me? Thank you in advance!

Upvotes: 3

Views: 950

Answers (1)

Horia Coman
Horia Coman

Reputation: 8781

I bet you need to first transpose your input matrix from 250000x2x4096 to 250000x4096x2, after which you can do the reshape.

Luckly, numpy offers the transpose function which should do the trick. See this question for a bigger discussion around transposing.

In your particular case, the invocation would be:

transposed_data_m = numpy.transpose(np_m, [1, 3, 2])
reshaped_data_m = tranposed_data_m.reshape(250000, 64, 64, 2)

Upvotes: 2

Related Questions