scipy.ndimage.generate_binary_structure

scipy.ndimage.generate_binary_structure(rank, connectivity)[源代码]

为二进制形态运算生成二进制结构。

参数
rank集成

将应用结构化元素的数组的维数,由返回 np.ndim

connectivity集成

connectivity 确定输出数组的哪些元素属于该结构,即被视为中心元素的邻居。平方距离以下的元素 connectivity 都被认为是邻居。 connectivity 可以从1(没有对角线元素是相邻元素)到 rank (所有元素都是邻居)。

退货
output一群野猪

可用于二进制形态运算的结构元素,具有 rank 尺寸和所有尺寸都等于3。

注意事项

generate_binary_structure 只能创建尺寸等于3(即最小尺寸)的结构元素。对于例如对侵蚀大对象有用的较大的结构元素,可以使用 iterate_structure ,或者使用Numpy函数直接创建自定义数组,例如 numpy.ones

示例

>>> from scipy import ndimage
>>> struct = ndimage.generate_binary_structure(2, 1)
>>> struct
array([[False,  True, False],
       [ True,  True,  True],
       [False,  True, False]], dtype=bool)
>>> a = np.zeros((5,5))
>>> a[2, 2] = 1
>>> a
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> b = ndimage.binary_dilation(a, structure=struct).astype(a.dtype)
>>> b
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> ndimage.binary_dilation(b, structure=struct).astype(a.dtype)
array([[ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.]])
>>> struct = ndimage.generate_binary_structure(2, 2)
>>> struct
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> struct = ndimage.generate_binary_structure(3, 1)
>>> struct # no diagonal elements
array([[[False, False, False],
        [False,  True, False],
        [False, False, False]],
       [[False,  True, False],
        [ True,  True,  True],
        [False,  True, False]],
       [[False, False, False],
        [False,  True, False],
        [False, False, False]]], dtype=bool)