SciPy

scipy.ndimage.binary_hit_or_miss

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

Multi-dimensional binary hit-or-miss transform.

The hit-or-miss transform finds the locations of a given pattern inside the input image.

Parameters:
input : array_like (cast to booleans)

Binary image where a pattern is to be detected.

structure1 : array_like (cast to booleans), optional

Part of the structuring element to be fitted to the foreground (non-zero elements) of input. If no value is provided, a structure of square connectivity 1 is chosen.

structure2 : array_like (cast to booleans), optional

Second part of the structuring element that has to miss completely the foreground. If no value is provided, the complementary of structure1 is taken.

output : ndarray, optional

Array of the same shape as input, into which the output is placed. By default, a new array is created.

origin1 : int or tuple of ints, optional

Placement of the first part of the structuring element structure1, by default 0 for a centered structure.

origin2 : int or tuple of ints, optional

Placement of the second part of the structuring element structure2, by default 0 for a centered structure. If a value is provided for origin1 and not for origin2, then origin2 is set to origin1.

Returns:
binary_hit_or_miss : ndarray

Hit-or-miss transform of input with the given structuring element (structure1, structure2).

See also

ndimage.morphology, binary_erosion

References

[1]https://en.wikipedia.org/wiki/Hit-or-miss_transform

Examples

>>> from scipy import ndimage
>>> 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]])

Previous topic

scipy.ndimage.binary_fill_holes

Next topic

scipy.ndimage.binary_opening