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]#

多维二进制命中或失误变换。

命中或失误变换在输入图像中查找给定模式的位置。

参数:
inputarray_like (转换为布尔值)

要在其中检测模式的二进制图像。

structure1array_like (转换为布尔值), 可选

要与 input 的前景色(非零元素)拟合的结构元素的一部分。 如果未提供值,则选择连接性为 1 的方形结构。

structure2array_like (转换为布尔值), 可选

必须完全错过前景的结构元素的第二部分。 如果未提供值,则取 structure1 的补集。

outputndarray,可选

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

origin1int 或 int 的元组, 可选

结构元素 structure1 的第一部分的放置位置,默认为 0 以获得居中的结构。

origin2int 或 int 的元组, 可选

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

axes整数元组或 None

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

返回:
binary_hit_or_missndarray

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

另请参阅

二进制腐蚀

附注

数组 API 标准支持

binary_hit_or_miss 对 Python Array API Standard 兼容的后端具有实验性支持,除了 NumPy 之外。 请考虑通过设置环境变量 SCIPY_ARRAY_API=1 并提供 CuPy、PyTorch、JAX 或 Dask 数组作为数组参数来测试这些功能。 支持以下后端和设备(或其他功能)的组合。

CPU

GPU

NumPy

不适用

CuPy

不适用

PyTorch

JAX

⚠️ 无 JIT

Dask

⚠️ 计算图

不适用

有关更多信息,请参阅 对数组 API 标准的支持

参考文献

示例

>>> 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]])