prtools.binary_opening#

binary_opening(input, structure=None, iterations=1, mask=None, output=None, border_value=0)[source]#

Binary opening using the given structure element.

The opening of an input image is the dilation of the erosion of the image by the structure element.

Parameters:
  • input (array_like) – Array to be opened. Nonzero elements form the subset to be opened.

  • structure (array_like, optional) – Structure element used for opening. Nonzero elements are considered True. If None (default), a structure element with a square connectivity equal to one is used.

  • iterations (int, optional) – Number of times to repeat the opening. Default is 1.

  • mask (array_like, optional) – Mask applied to the inputs where a True value indicates the corresponding input element should be included in the opening.

  • output (ndarray, optional) –

    Array location into which the result is stored. If None (default), a freshly-allocated array is returned.

    Note

    output must be None if input is a JAX array.

  • border_value (int (cast to 0 or 1), optional) – Value at the border of the output array.

Returns:

binary_opening – Opening of the input by the structure element.

Return type:

ndarray of bools

Notes

This function is designed to be compatible with JAX jit and grad operations.

Examples

>>> a = np.zeros((5,5), dtype=int)
>>> a[1:4, 1:4] = 1; a[4, 4] = 1
>>> a
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 1]])

>>> # Opening removes small objects
>>> prtools.binary_opening(a, structure=np.ones((3,3))).astype(int)
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])

>>> # Opening can also smooth corners
>>> prtools.binary_opening(a).astype(int)
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]])