scipy.ndimage.

binary_dilation#

scipy.ndimage.binary_dilation(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False, *, axes=None)[源]#

使用给定结构元素进行多维二值膨胀。

参数:
inputarray_like

要膨胀的二值 array_like。非零 (True) 元素构成要膨胀的子集。

structurearray_like, optional

用于膨胀的结构元素。非零元素被视为 True。如果未提供结构元素,将生成一个方形连通性等于一的元素。

iterationsint, optional

膨胀重复 iterations 次(默认为一次)。如果 iterations 小于 1,膨胀将重复直到结果不再改变。只接受整数次迭代。

maskarray_like, optional

如果给定掩码,则在每次迭代中,只有对应掩码元素为 True 的元素才会被修改。

outputndarray, optional

与输入形状相同的数组,输出结果将放置其中。默认情况下,会创建一个新数组。

border_valueint (转换为 0 或 1), optional

输出数组中的边界值。

originint or tuple of ints, optional

滤波器的放置位置,默认为 0。

brute_forceboolean, optional

内存条件:如果为 False,则只跟踪上次迭代中值发生变化的像素作为当前迭代中要更新(膨胀)的候选像素;如果为 True,则所有像素都被视为膨胀的候选像素,无论上一次迭代中发生了什么。默认为 False。

axestuple of int or None

应用滤波器的轴。如果为 None,则 input 将沿所有轴进行过滤。如果提供了 origin 元组,其长度必须与轴数匹配。

返回:
binary_dilationndarray of bools

输入由结构元素膨胀后的结果。

说明

膨胀 [1] 是一种数学形态学操作 [2],它使用结构元素来扩展图像中的形状。图像的二值膨胀是指当结构元素的中心位于图像的非零点内时,结构元素所覆盖点的轨迹。

参考文献

示例

>>> from scipy import ndimage
>>> import numpy as np
>>> 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.]])
>>> ndimage.binary_dilation(a)
array([[False, False, False, False, False],
       [False, False,  True, False, False],
       [False,  True,  True,  True, False],
       [False, False,  True, False, False],
       [False, False, False, False, False]], dtype=bool)
>>> ndimage.binary_dilation(a).astype(a.dtype)
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.]])
>>> # 3x3 structuring element with connectivity 1, used by default
>>> struct1 = ndimage.generate_binary_structure(2, 1)
>>> struct1
array([[False,  True, False],
       [ True,  True,  True],
       [False,  True, False]], dtype=bool)
>>> # 3x3 structuring element with connectivity 2
>>> struct2 = ndimage.generate_binary_structure(2, 2)
>>> struct2
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> ndimage.binary_dilation(a, structure=struct1).astype(a.dtype)
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(a, structure=struct2).astype(a.dtype)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> ndimage.binary_dilation(a, structure=struct1,\
... iterations=2).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.]])