scipy.ndimage.binary_closing#
- scipy.ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False)[source]#
Multidimensional binary closing with the given structuring element.
The closing of an input image by a structuring element is the erosion of the dilation of the image by the structuring element.
- Parameters:
- inputarray_like
Binary array_like to be closed. Non-zero (True) elements form the subset to be closed.
- structurearray_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).
- iterationsint, 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.
- outputndarray, optional
Array of the same shape as input, into which the output is placed. By default, a new array is created.
- originint or tuple of ints, optional
Placement of the filter, by default 0.
- maskarray_like, optional
If a mask is given, only those elements with a True value at the corresponding mask element are modified at each iteration.
New in version 1.1.0.
- border_valueint (cast to 0 or 1), optional
Value at the border in the output array.
New in version 1.1.0.
- brute_forceboolean, 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.
New in version 1.1.0.
- Returns:
- binary_closingndarray 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.References
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 array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]) >>> # Closing removes small holes >>> ndimage.binary_closing(a).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]]) >>> # Closing is the erosion of the dilation of the input >>> ndimage.binary_dilation(a).astype(int) array([[0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0, 1, 1, 1, 0]]) >>> ndimage.binary_erosion(ndimage.binary_dilation(a)).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]])
>>> a = np.zeros((7,7), dtype=int) >>> a[1:6, 2:5] = 1; a[1:3,3] = 0 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> # In addition to removing holes, closing can also >>> # coarsen boundaries with fine hollows. >>> ndimage.binary_closing(a).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]])