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
-
1Can you give some examples of the shapes of your composite array, your slices and the result array you want? – blubberdiblub Apr 15 '19 at 07:45
-
`x[:, :, :, start:end]`? – Stop harming Monica Apr 15 '19 at 08:29
-
This I know how to do but I want where the 4th axis is entries10-20 35-45 and 60-70,thanks in advance! – Llangwyfan Apr 15 '19 at 08:40
-
Having that in the question, ideally as part of a [mcve], would be a huge improvement. – Stop harming Monica Apr 15 '19 at 09:11
1 Answers
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)
.
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()

- 5,117
- 1
- 16
- 21