0

I have a 4d numpy array, I want to perform a calculation on several slices of it and then create a new array with all the values in. The main problem is I have fixed slices for 3 dimensions but then several ranges for the fourth axis how do I slice by ranges on the 4th axis please

1 Answers1

1

While I think there are several similar questions on here already (38 results for numpy 4d slice), here's my attempt to visually explain this. First, I downloaded a .gif from Giphy and converted it into an exemplary multidimensional np.array(); it is 200x200px in size, has 3 colour channels, 6 frames, resulting in a shape of (6, 200, 200, 3).

enter image description here

In the first row of the plot, I show each color channel separately, and the total sum of the frame with index 1 (note that the cmap keyword is disregarded here).

Next, I create a specific slice of the green channel (color index 1), which goes from 50-150px in y-direction, and 100-120px in x-direction like so: my_slice= frames[frame_index,50:150,100:120,1] — it has shape (100, 20). I multiply it by 0.1, and insert the result in the original position.

The second image row now shows how I reduced the green channel in this region, yielding to an overall pink hue in the total image.

If you now would like to perform this action on e.g. frames [0,4,5], it becomes a simple for frame_index in [0,4,5]: loop. Hope this helps.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageSequence

## downloaded from here: 
# https://media.giphy.com/media/uQCxA7u9CEdIA/200w_d.gif
img = Image.open("/path/to/Downloads/brainscan.gif")
frames = np.array([np.array(frame.copy().convert('RGB').getdata(),dtype=np.uint8).reshape(frame.size[1],frame.size[0],3) for frame in ImageSequence.Iterator(img)])

print(np.shape(frames))

frame_index=1

fig,axes=plt.subplots(2,4,figsize=(10,4))
axes[0,0].imshow(frames[frame_index,:,:,0],cmap="Reds_r")
axes[0,1].imshow(frames[frame_index,:,:,1],cmap="Greens_r")
axes[0,2].imshow(frames[frame_index,:,:,2],cmap="Blues_r")
axes[0,3].imshow(frames[frame_index,:,:,:],cmap="viridis")

my_slice= frames[frame_index,50:150,100:120,1]
print(np.shape(my_slice[:]))
frames[frame_index,50:150,100:120,1]=0.1*my_slice

axes[1,0].imshow(frames[frame_index,:,:,0],cmap="Reds_r")
axes[1,1].imshow(frames[frame_index,:,:,1],cmap="Greens_r")
axes[1,2].imshow(frames[frame_index,:,:,2],cmap="Blues_r")
axes[1,3].imshow(frames[frame_index,:,:,:],cmap="viridis")


plt.show()
Asmus
  • 5,117
  • 1
  • 16
  • 21