我有一个4d的numpy数组,我想对它的几个切片执行计算,然后创建一个包含所有值的新数组。主要问题是我对3个维度有固定的切片,但是对第四轴有几个范围,请按第4轴的范围进行切片
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 eg 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.