scipy.ndimage.

binary_hit_or_miss#

scipy.ndimage.binary_hit_or_miss(input, structure1=None, structure2=None, output=None, origin1=0, origin2=None, *, axes=None)[source]#

多维二值击中或不击中变换。

击中或不击中变换用于在输入图像中查找给定模式的位置。

参数:
input类数组(转换为布尔值)

用于检测模式的二值图像。

structure1类数组(转换为布尔值),可选

结构元素的一部分,用于匹配 input 的前景(非零元素)。如果未提供值,则选择连接性为 1 的方形结构。

structure2类数组(转换为布尔值),可选

结构元素的第二部分,必须完全避开前景。如果未提供值,则采用 structure1 的补集。

outputndarray,可选

与输入具有相同形状的数组,用于存放输出。默认情况下,会创建一个新数组。

origin1int 或 int 元组,可选

结构元素 structure1 第一部分的放置位置,默认情况下,居中结构为 0。

origin2int 或 int 元组,可选

结构元素 structure2 第二部分的放置位置,默认情况下,居中结构为 0。如果为 origin1 提供了值而未为 origin2 提供值,则 origin2 将设置为 origin1

axesint 元组或 None

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

返回:
binary_hit_or_missndarray

使用给定的结构元素(structure1, structure2)对 input 进行击中或不击中变换。

另请参阅

binary_erosion

参考资料

示例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[1, 1] = 1; a[2:4, 2:4] = 1; a[4:6, 4:6] = 1
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0],
       [0, 0, 0, 0, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> structure1 = np.array([[1, 0, 0], [0, 1, 1], [0, 1, 1]])
>>> structure1
array([[1, 0, 0],
       [0, 1, 1],
       [0, 1, 1]])
>>> # Find the matches of structure1 in the array a
>>> ndimage.binary_hit_or_miss(a, structure1=structure1).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> # Change the origin of the filter
>>> # origin1=1 is equivalent to origin1=(1,1) here
>>> ndimage.binary_hit_or_miss(a, structure1=structure1,\
... origin1=1).astype(int)
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, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0]])