{ } Raw JSON

bundles / scipy latest / scipy / ndimage / _morphology / binary_closing

function

scipy.ndimage._morphology:binary_closing

source: /scipy/ndimage/_morphology.py :676

Signature

def   binary_closing ( input structure = None iterations = 1 output = None origin = 0 mask = None border_value = 0 brute_force = False * axes = None )

Summary

Multidimensional binary closing with the given structuring element.

Extended Summary

The closing of an input image by a structuring element is the erosion of the dilation of the image by the structuring element.

Parameters

input : array_like

Binary array_like to be closed. Non-zero (True) elements form the subset to be closed.

structure : array_like, optional

Structuring element used for the closing. Non-zero elements are considered True. If no structuring element is provided an element is generated with a square connectivity equal to one (i.e., only nearest neighbors are connected to the center, diagonally-connected elements are not considered neighbors).

iterations : int, optional

The dilation step of the closing, then the erosion step are each repeated iterations times (one, by default). If iterations is less than 1, each operations is repeated until the result does not change anymore. Only an integer of iterations is accepted.

output : ndarray, optional

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

origin : int or tuple of ints, optional

Placement of the filter, by default 0.

mask : array_like, optional

If a mask is given, only those elements with a True value at the corresponding mask element are modified at each iteration.

border_value : int (cast to 0 or 1), optional

Value at the border in the output array.

brute_force : boolean, optional

Memory condition: if False, only the pixels whose value was changed in the last iteration are tracked as candidates to be updated in the current iteration; if true al pixels are considered as candidates for update, regardless of what happened in the previous iteration. False by default.

axes : tuple of int or None

The axes over which to apply the filter. If None, input is filtered along all axes. If an origin tuple is provided, its length must match the number of axes.

Returns

binary_closing : ndarray of bools

Closing of the input by the structuring element.

Notes

Closing [1] is a mathematical morphology operation [2] that consists in the succession of a dilation and an erosion of the input with the same structuring element. Closing therefore fills holes smaller than the structuring element.

Together with opening (binary_opening), closing can be used for noise removal.

Array API Standard Support

binary_closing has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ⛔                   
JAX                   ⚠️ no JIT
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

from scipy import ndimage
import numpy as np
a = np.zeros((5,5), dtype=int)
a[1:-1, 1:-1] = 1; a[2,2] = 0
a
ndimage.binary_closing(a).astype(int)
ndimage.binary_dilation(a).astype(int)
ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int)
a = np.zeros((7,7), dtype=int)
a[1:6, 2:5] = 1; a[1:3,3] = 0
a
ndimage.binary_closing(a).astype(int)
ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int)

See also

binary_dilation
binary_erosion
binary_opening
generate_binary_structure
grey_closing

Aliases

  • scipy.ndimage.binary_closing

Referenced by

This package