3

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!

2
  • To know how to help you, you need to provide a little bit more information about the images themselves. What format are they saved in? as a 1D array of 4096 numbers? Commented Sep 5, 2016 at 8:40
  • Sorry, I forgot to write that the images are saved in bmp and the arrays are 1D of 4096 float64 numbers. Commented Sep 5, 2016 at 8:58

1 Answer 1

2

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

2 Comments

nice, I did not even see that the 2 changed places. I think this might be the solution OP is looking for
Thank you for your suggestion! Unfortunately I keep getting black images.

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.