scipy.ndimage.
morphological_gradient#
- scipy.ndimage.morphological_gradient(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)[source]#
多维形态学梯度。
形态学梯度被计算为输入图像经膨胀操作与腐蚀操作后的差值,并使用给定的结构元素。
- 参数:
- input类数组
用于计算形态学梯度的数组。
- size整数元组
用于数学形态学操作的平坦且完整的结构元素的形状。如果提供了 footprint 或 structure,则此参数可选。更大的 size 会产生更模糊的梯度。
- footprint整数数组,可选
用于形态学操作的平坦结构元素的非无穷大元素的位置。更大的 `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 元组,其长度必须与轴的数量匹配。
- 返回:
- morphological_gradientndarray
input 的形态学梯度。
备注
对于平坦的结构元素,在给定点计算的形态学梯度对应于以该点为中心的结构元素所覆盖的输入元素之间的最大差异。
参考
示例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((7,7), dtype=int) >>> a[2:5, 2:5] = 1 >>> ndimage.morphological_gradient(a, size=(3,3)) array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> # The morphological gradient is computed as the difference >>> # between a dilation and an erosion >>> ndimage.grey_dilation(a, size=(3,3)) -\ ... ndimage.grey_erosion(a, size=(3,3)) array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> a = np.zeros((7,7), dtype=int) >>> a[2:5, 2:5] = 1 >>> a[4,4] = 2; a[2,3] = 3 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 3, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> ndimage.morphological_gradient(a, size=(3,3)) array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 3, 3, 3, 1, 0], [0, 1, 3, 3, 3, 1, 0], [0, 1, 3, 2, 3, 2, 0], [0, 1, 1, 2, 2, 2, 0], [0, 1, 1, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0, 0]])