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)[源代码][源代码]#

使用给定的结构元素进行多维二进制闭合。

通过结构元素对输入图像的 闭合 是图像通过该结构元素的 膨胀腐蚀

参数:
输入array_like

要闭合的二进制类数组。非零(True)元素形成要闭合的子集。

结构类似数组, 可选

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

迭代int, 可选

闭合的膨胀步骤,然后是腐蚀步骤,每个步骤都重复 iterations 次(默认一次)。如果 iterations 小于 1,则每个操作将重复进行,直到结果不再变化为止。只接受整数形式的 iterations。

输出ndarray,可选

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

起源int 或 int 的元组,可选

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

掩码类似数组, 可选

如果给定了掩码,则每次迭代时仅修改掩码元素对应位置为 True 的那些元素。

Added in version 1.1.0.

border_valueint (转换为 0 或 1), 可选

输出数组边界处的值。

Added in version 1.1.0.

brute_force布尔值,可选

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

Added in version 1.1.0.

返回:
binary_closing布尔类型的 ndarray

通过结构元素关闭输入。

注释

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

开启 (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]])