更新时间:2022-06-24 22:38:18
您可以使用 scikit-image的 view_as_windows
创建那些滑动窗口然后我们需要置换轴和重塑 -
You can use scikit-image's view_as_windows
to create those sliding windows and then we need to permute axes and reshape -
from skimage.util.shape import view_as_windows
def get_kernels_vectorized(im, k_size):
X, Y = im.shape[1] + 1 - k_size, im.shape[0] + 1 - k_size
new = view_as_windows(im,(k_size,k_size,1))[...,0].transpose(0,1,3,4,2)
return new.reshape(X * Y, k_size ** 2 * 3)
使用 view_as_windows $ c的说明$ c>
Explanation on usage of view_as_windows
view_as_windows
的想法是输入参数arg window_shape
作为一个长度的元组,与需要滑动窗口的输入数组中的维数相同。此外,我们不需要滑动的轴将作为 1s
提供。所以, window_shape
的输入值是(k_size,k_size,1)
,因为最后一个轴是颜色通道,我们不需要幻灯片。
The idea with view_as_windows
is that we feed in the input arg window_shape
as a tuple of length same as the number of dimensions in the input array whose sliding windows are needed. Also, the axes along which we don't need to slide are fed as 1s
. So, the input value for window_shape
here is (k_size, k_size, 1)
as the last axis is of color channels, along which we don't need to slide.
运行验证样本 -
In [186]: np.random.seed(0)
...: im = np.random.randint(0,9,(6,8,3))
In [189]: out1 = get_kernels(im, k_size=3)
In [190]: out2 = get_kernels_vectorized(im, k_size=3)
In [191]: np.array_equal(out1, out2)
Out[191]: True
3264x2448图片
上的时间,内核尺寸= 3
-
Timings on 3264x2448 image
with kernel size = 3
-
In [177]: np.random.seed(0)
...: im = np.random.randint(0,9,(3264,2448,3))
In [178]: %timeit get_kernels(im, k_size=3)
1 loop, best of 3: 5.46 s per loop
In [179]: %timeit get_kernels_vectorized(im, k_size=3)
1 loop, best of 3: 327 ms per loop
16x +
加速这里。