我怎样才能保留包含所有元素的补丁 1

How can I keep the patch which contain all the elements 1

from sklearn.feature_extraction.image import extract_patches
import numpy as np

data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0],
                 [1, 1 , 1 , 0 , 0 , 1 , 1 , 0],
                 [1, 1 , 0 , 1 , 1 , 0 , 0 , 0],
                 [0, 0 , 0 , 1 , 1 , 0 , 0 , 0],
                 [0, 0 , 0 , 1 , 1 , 0 , 0 , 1],
                 [1, 1 , 0 , 0 , 0 , 0 , 1 , 0],
                 [1, 1 , 0 , 0 , 0 , 0 , 0 , 0]])

patches = extract_patches(data, patch_shape=(2, 2))

如何保留包含所有元素 1 的补丁?

根据对您的 post 的更正,我相信您可能正在寻找一种方法来检测形状 (2,2) 的子矩阵在何处都是 1。任何不满足该条件的地方都应为零,但应优先考虑满足该条件的子矩阵,因为子矩阵可以重叠.

在那种情况下,您最有可能对该矩阵的交错网格感兴趣,只要该子矩阵的 4 个元素全为 1,该矩阵的每个 2x2 子矩阵的中心都有一个:

>>> import numpy as np
>>> from sklearn.feature_extraction.image import extract_patches # similar to numpy's stride_tricks
>>> 
>>> data = np.array([[1, 1, 0, 0, 0, 0, 1, 0],
...                  [1, 1, 1, 0, 0, 1, 1, 0],
...                  [1, 1, 0, 1, 1, 0, 0, 0],
...                  [0, 0, 0, 1, 1, 0, 0, 0],
...                  [0, 0, 0, 1, 1, 0, 0, 1],
...                  [1, 1, 0, 0, 0, 0, 1, 0],
...                  [1, 1, 0, 0, 0, 0, 0, 0]])
>>> 
>>> # to take boundary effects into account, append ones to the right and bottom
... # modify this to `np.zeros` if boundaries are to be set to zero
... data2 = np.ones((data.shape[0]+1, data.shape[1]+1))  
>>> data2[:-1,:-1] = data
>>> vert = np.logical_and(data2[:-1,:], data2[1:,])
>>> dual = np.logical_and(vert[:,:-1], vert[:,1:]) # dual is now the "dual" graph/staggered grid of the data2 array
>>> patches = extract_patches(data2, patch_shape=(2, 2))  # could've used numpy stride_tricks too
>>> patches[dual==0] = 0
>>> patches[dual] = 1  # Give precedence to the dual positives
>>> data2[:-1, :-1].astype(np.uint8)
array([[1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0]], dtype=uint8)

为了完整起见,矩阵的这种交错网格形式也可以通过 相关性np.ones((2,2)) 内核轻松获得。然而,这在计算上更繁重,因为需要完成更多的工作(乘法和求和)而不是简单的位操作。上述方法在速度方面优于基于相关性的方法。 上面的交错网格dual也可以通过以下方式生成:

patches = extract_patches(data, patch_shape=(2, 2))
dual = patches.all(axis=-1).all(axis=-1)

您将获得最终结果:

dual = patches.all(axis=-1).all(axis=-1)
patches[dual==False] = 0
patches[dual] = 1

虽然在边界发生的事情与以前的方法不同。

这是另一种方法,使用 minimum_filter and maximum_filter from scipy.ndimage。 (问题中的描述仍然太模糊——无论如何对我来说——所以这是基于@OliverW. 的回答中显示的结果。)

In [138]: from scipy.ndimage import minimum_filter, maximum_filter

In [139]: data
Out[139]: 
array([[1, 1, 0, 0, 0, 0, 1, 0],
       [1, 1, 1, 0, 0, 1, 1, 0],
       [1, 1, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 1],
       [1, 1, 0, 0, 0, 0, 1, 0],
       [1, 1, 0, 0, 0, 0, 0, 0]])

In [140]: m = minimum_filter(data, size=(2,2), mode='constant', origin=(-1,-1))

In [141]: result = maximum_filter(m, size=(2,2), mode='constant')

In [142]: result
Out[142]: 
array([[1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0]])