我有一个形状为(10,10,8)的图像的滚动窗口视图,其中最后一个维度是通道,窗口形状(3,3)和步幅为(1,1):
image = np.random.randn(10, 10, 8) # for example
view = view_as_windows(image, (3, 3, 1), (1, 1, 1))[..., 0].transpose((0, 1, 3, 4, 2))
view.shape
>>> (8, 8, 3, 3, 8)
我想执行相反的操作,所以我要查看一些图像,并且想要获得一些图像形状的数组,但这应该以特殊的方式完成:
view = np.random.randn(8,8,3,3,8)
result = np.zeros((10, 10, 8))
for i in range(len(view)):
for j in range(len(view[i])):
#view[i, j] is of shape (3, 3, 8)
result[slice(i, i + 3), slice(j, j + 3)] += view[i, j]
#all overlaps are summed up
result.shape
>>> (10, 10, 8)
上面的代码可以工作,但是效率很低,因为我将循环使用此代码。