scipy.ndimage.
binary_erosion#
- scipy.ndimage.binary_erosion(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False, *, axes=None)[source]#
使用给定结构元素进行多维二值腐蚀。
二值腐蚀是一种用于图像处理的数学形态学操作。
- 参数:
- inputarray_like
待腐蚀的二值图像。非零(True)元素构成要腐蚀的子集。
- structurearray_like, optional
用于腐蚀的结构元素。非零元素被视为 True。如果未提供结构元素,则生成一个方形连接性等于一的元素。
- iterationsint, optional
腐蚀重复 iterations 次(默认为一次)。如果 iterations 小于 1,则腐蚀会重复进行,直到结果不再改变。
- maskarray_like, optional
如果给定掩码,则每次迭代中只修改掩码中对应元素为 True 的元素。
- outputndarray, optional
与 input 形状相同的数组,用于放置输出。默认情况下,会创建一个新数组。
- border_valueint (cast to 0 or 1), optional
输出数组边界处的值。
- originint or tuple of ints, optional
滤波器的位置,默认为 0。
- brute_forceboolean, optional
内存条件:如果为 False,则只跟踪上次迭代中值发生变化的像素作为当前迭代中要更新(腐蚀)的候选像素;如果为 True,则所有像素都被视为腐蚀的候选像素,无论上一次迭代中发生了什么。默认为 False。
- axestuple of int or None
应用滤波器的轴。如果为 None,则对 input 的所有轴进行滤波。如果提供了 origin 元组,其长度必须与轴的数量匹配。
- 返回:
- binary_erosionndarray of bools
输入图像通过结构元素进行腐蚀的结果。
注释
腐蚀 [1] 是一种数学形态学操作 [2],它使用结构元素来收缩图像中的形状。图像通过结构元素进行二值腐蚀的结果是,以该点为中心的结构元素叠加后完全包含在图像非零元素集合中的点的轨迹。
参考文献
示例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((7,7), dtype=int) >>> a[1:6, 2:5] = 1 >>> a 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]]) >>> ndimage.binary_erosion(a).astype(a.dtype) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> #Erosion removes objects smaller than the structure >>> ndimage.binary_erosion(a, structure=np.ones((5,5))).astype(a.dtype) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])