scipy.ndimage.

binary_closing#

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

使用给定结构元素进行多维二值闭运算。

图像的闭运算是指先对图像进行膨胀,然后对膨胀结果进行腐蚀,两次操作都使用相同的结构元素。

参数:
inputarray_like

要进行闭运算的二值数组。非零(True)元素构成要进行闭运算的子集。

structurearray_like, optional

用于闭运算的结构元素。非零元素被视为 True。如果未提供结构元素,则会生成一个连通性为一的正方形元素(即,只有最近邻居连接到中心,对角线连接的元素不被视为邻居)。

iterationsint, optional

闭运算的膨胀步骤和腐蚀步骤各重复 iterations 次(默认为一次)。如果 iterations 小于 1,则每个操作会重复执行直到结果不再改变。只接受整数迭代次数。

outputndarray, optional

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

originint or tuple of ints, optional

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

maskarray_like, optional

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

版本 1.1.0 新增。

border_valueint (转换为 0 或 1), optional

输出数组中边界处的值。

版本 1.1.0 新增。

brute_forceboolean, optional

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

版本 1.1.0 新增。

axestuple of int or None

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

返回:
binary_closingndarray of bools

输入图像经过结构元素的闭运算结果。

备注

闭运算 [1] 是一种数学形态学操作 [2],它包括对输入图像使用相同的结构元素进行膨胀和腐蚀的连续操作。因此,闭运算可以填充比结构元素小的孔洞。

开运算 (binary_opening) 一起,闭运算可用于噪声去除。

参考文献

示例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5,5), dtype=int)
>>> a[1:-1, 1:-1] = 1; a[2,2] = 0
>>> a
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 0, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])
>>> # Closing removes small holes
>>> ndimage.binary_closing(a).astype(int)
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]])
>>> # Closing is the erosion of the dilation of the input
>>> ndimage.binary_dilation(a).astype(int)
array([[0, 1, 1, 1, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0]])
>>> ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int)
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]])
>>> a = np.zeros((7,7), dtype=int)
>>> a[1:6, 2:5] = 1; a[1:3,3] = 0
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> # In addition to removing holes, closing can also
>>> # coarsen boundaries with fine hollows.
>>> ndimage.binary_closing(a).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])