0

I have a dataset which comprises of the binary data of pixelated 50x50 images. The array shape is (50, 50, 90245). I want to reach 50x50 pixels of each of the 90245 images. How can I slice the array?

5
  • What would be the shape of your expected output array?
    – user17242583
    Mar 19, 2022 at 20:52
  • I suppose it should be (50x50) for every 90245
    – sonido
    Mar 19, 2022 at 20:53
  • So basically you have 90245 images that are each 50x50, and you want to get them all into a list? So maybe you want to change the shape to (90245, 50, 50)
    – user17242583
    Mar 19, 2022 at 20:55
  • 1
    Actually, the proposed solution in the answer worked for me. When I tried your way, it gave me different data
    – sonido
    Mar 19, 2022 at 21:03
  • 1
    I think you should read the numpy manual on indexing
    – Mr. T
    Mar 19, 2022 at 21:07

2 Answers 2

1

If data is the variable storing the image data, and i is the index of the image you want to access, then you can do:

data[:,:,i]

to get the desired image data.

0
0

If data is the variable storing the image data, and i is the index of the image you want to access, then you can do as @BrokenBenchmark suggested. In case you want a (50,50,1) 3D array as the output, you could do:

data[:,:,i:i+1]

to get the image as a 3D array.

Edit1: If you reshaped your data matrix to be of shape (90245,50,50), you can get the ith image by doing data[i,:,:] or just data[i] to get a (50,50) image. Similarly, to get a (1,50,50) image, you could do data[i:i+1,:,:] or just data[i:i+1].

Edit2: To reshape the array, you could use the swapaxes() function in numpy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.