2

I have a 4 dimensionnal set of data coming from a Tiff file.

image_stack = io.imread(path, plugin='tifffile')
print(image_stack.shape)
>>> (21, 10, 1331, 1126) 

So the last 2 Dimensions are the images resolutions. The 10 is because I have 10 slices of the same image (over the z axis) at a given time And 21 because all of those images are taken every second.

How can I swap the 21 and 10 dimensions ?

yatu
  • 86,083
  • 12
  • 84
  • 139
Warat
  • 29
  • 2
  • Does this answer your question? [Swapping the dimensions of a numpy array](https://stackoverflow.com/questions/23943379/swapping-the-dimensions-of-a-numpy-array) – NoDataDumpNoContribution May 19 '20 at 21:05
  • This answers your question https://stackoverflow.com/questions/61609319/swap-axes-of-a-3d-image-in-nilearn-numpy/61609457#61609457 – Ehsan May 19 '20 at 22:30

1 Answers1

3

You can just swap the two first axes with swapaxes:

a = np.random.rand(21, 10, 1331, 1126)
a.swapaxes(1,0).shape
# (10, 21, 1331, 1126)

Or move the second axis backwards with rollaxis:

np.rollaxis(a, 1).shape
# (10, 21, 1331, 1126)
yatu
  • 86,083
  • 12
  • 84
  • 139