如何在不遍历每个像素的情况下同时裁剪 window 大小的 numpy 数组的每个元素?
How to crop each element of a numpy array with a window size at the same time without looping over each pixel?
我有一个大小为 240 x 320 x 3
的 numpy 数组,我想使用 window(window 大小 ws=5
)在每个像素上滑动并裁剪出以该像素为中心的子阵列。最终输出维度应该是240 x 320 x ws x ws x 3
。所以我用 window 大小填充原始数组并使用 for loop
这样做。
height = 240
width = 320
image = np.random.rand((height, width, 3))
image = np.pad(image, ((ws//2, ws//2), (ws//2, ws//2), (0, 0)), mode='reflect')
patches = np.zeros((height, width, ws, ws, 3))
for i in range(height):
for j in range(width):
patches[i, j] = image[i:i+ws, j:j+ws]
有什么方法可以在采样时裁剪每个像素吗?就像不在每个像素上使用 for loop
一样?
您基本上是在图像上滑动 windows。我们可以利用 np.lib.stride_tricks.as_strided
based scikit-image's view_as_windows
to get sliding windows. .
from skimage.util.shape import view_as_windows
out = view_as_windows(image,(ws,ws,1)).transpose(0,1,4,3,2,5)[:-1,:-1,...,0]
# Alternatively :
out = view_as_windows(image,(ws,ws,1))[:-1,:-1,...,0].transpose(0,1,4,3,2)
另请注意,如果您有 for i in range(height+1)
和 for j in range(width+1)
,您将错过最后一个可能的 window。为了使用我们的解决方案获得相同的结果,最后的索引步骤将修改为 [...,0]
代替 [:-1,:-1,...,0]
,从而给我们 -
out = view_as_windows(image,(ws,ws,1))[...,0].transpose(0,1,4,3,2)
我有一个大小为 240 x 320 x 3
的 numpy 数组,我想使用 window(window 大小 ws=5
)在每个像素上滑动并裁剪出以该像素为中心的子阵列。最终输出维度应该是240 x 320 x ws x ws x 3
。所以我用 window 大小填充原始数组并使用 for loop
这样做。
height = 240
width = 320
image = np.random.rand((height, width, 3))
image = np.pad(image, ((ws//2, ws//2), (ws//2, ws//2), (0, 0)), mode='reflect')
patches = np.zeros((height, width, ws, ws, 3))
for i in range(height):
for j in range(width):
patches[i, j] = image[i:i+ws, j:j+ws]
有什么方法可以在采样时裁剪每个像素吗?就像不在每个像素上使用 for loop
一样?
您基本上是在图像上滑动 windows。我们可以利用 np.lib.stride_tricks.as_strided
based scikit-image's view_as_windows
to get sliding windows.
from skimage.util.shape import view_as_windows
out = view_as_windows(image,(ws,ws,1)).transpose(0,1,4,3,2,5)[:-1,:-1,...,0]
# Alternatively :
out = view_as_windows(image,(ws,ws,1))[:-1,:-1,...,0].transpose(0,1,4,3,2)
另请注意,如果您有 for i in range(height+1)
和 for j in range(width+1)
,您将错过最后一个可能的 window。为了使用我们的解决方案获得相同的结果,最后的索引步骤将修改为 [...,0]
代替 [:-1,:-1,...,0]
,从而给我们 -
out = view_as_windows(image,(ws,ws,1))[...,0].transpose(0,1,4,3,2)