scipy.ndimage.
grey_erosion#
- scipy.ndimage.grey_erosion(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)[源代码]#
计算灰度腐蚀,可以使用结构元素或对应于平坦结构元素的足迹。
灰度腐蚀是一种数学形态学操作。对于完整且平坦结构元素的简单情况,它可以被视为滑动窗口上的最小值滤波器。
- 参数:
- input类数组
要计算灰度腐蚀的数组。
- size整数元组
用于灰度腐蚀的平坦完整结构元素的形状。如果提供了 footprint 或 structure,则此参数可选。
- footprint整数数组,可选
用于灰度腐蚀的平坦结构元素的非无穷大元素的位置。非零值表示选择最小值的中心像素的邻域集合。
- structure整数数组,可选
用于灰度腐蚀的结构元素。structure 可以是非平坦结构元素。structure 数组对邻域中的每个像素应用一个减法偏移。
- output数组,可选
可以提供一个用于存储腐蚀结果的数组。
- mode{‘reflect’,’constant’,’nearest’,’mirror’, ‘wrap’},可选
参数 mode 决定了数组边界的处理方式,其中 cval 是当 mode 等于 'constant' 时的值。默认为 'reflect'。
- cval标量,可选
如果 mode 为 'constant',用于填充输入边界以外的值。默认值为 0.0。
- origin标量,可选
参数 origin 控制滤波器的位置。默认值为 0。
- axes整数元组或 None
应用滤波器的轴。如果为 None,则 input 沿所有轴进行滤波。如果提供了 origin 元组,其长度必须与轴的数量匹配。
- 返回值:
- outputndarray
input 的灰度腐蚀结果。
另请参阅
备注
图像输入通过在域 E 上定义的结构元素 s 进行灰度腐蚀的计算公式为
(input+s)(x) = min {input(y) - s(x-y),对于 y 属于 E}
特别是,对于定义为 s(y) = 0(y 属于 E)的结构元素,灰度腐蚀计算输入图像在由 E 定义的滑动窗口内的最小值。
参考文献
示例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((7,7), dtype=int) >>> a[1:6, 1:6] = 3 >>> a[4,4] = 2; a[2,3] = 1 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 3, 3, 0], [0, 3, 3, 1, 3, 3, 0], [0, 3, 3, 3, 3, 3, 0], [0, 3, 3, 3, 2, 3, 0], [0, 3, 3, 3, 3, 3, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> ndimage.grey_erosion(a, size=(3,3)) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 3, 2, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> footprint = ndimage.generate_binary_structure(2, 1) >>> footprint array([[False, True, False], [ True, True, True], [False, True, False]], dtype=bool) >>> # Diagonally-connected elements are not considered neighbors >>> ndimage.grey_erosion(a, footprint=footprint) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 3, 1, 2, 0, 0], [0, 0, 3, 2, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])