pyvista.ImageDataFilters.close

pyvista.ImageDataFilters.close#

ImageDataFilters.close(
kernel_size: int | VectorLike[int] = (3, 3, 3),
scalars: str | None = None,
*,
binary: bool | VectorLike[float] | None = None,
progress_bar: bool = False,
)[source]#

Perform morphological closing on continuous or binary data.

Closing is a dilation followed by an erosion. It is used to fill small holes/gaps while preserving the shape and size of larger objects.

Added in version 0.47.

Parameters:
kernel_sizeint | VectorLike[int], default: (3, 3, 3)

Determines the size of the kernel along the xyz-axes. Only non-singleton dimensions are closed, e.g. a kernel size of (3, 3, 1) and (3, 3, 3) produce the same result for 2D images.

scalarsstr, optional

Name of scalars to process. Defaults to currently active scalars.

binarybool | VectorLike[float], optional

Control if binary closing or continuous closing is used. Refer to dilate() and/or erode() for details about using this keyword.

progress_barbool, default: False

Display a progress bar to indicate progress.

Returns:
pyvista.ImageData

Dataset that has been closed.

See also

open, erode, dilate

Notes

This filter only supports point data. For inputs with cell data, consider re-meshing the cell data as point data with cells_to_points() or resampling the cell data to point data with cell_data_to_point_data().

Examples

Load a binary image: download_yinyang().

>>> from pyvista import examples
>>> im = examples.download_yinyang()

Use close with a relatively small kernel to fill the top black edge of the yinyang.

>>> closed = im.close(kernel_size=5)
>>> kwargs = dict(
...     cmap='grey',
...     lighting=False,
...     cpos='xy',
...     zoom='tight',
...     show_axes=False,
...     show_scalar_bar=False,
... )
>>> closed.plot(**kwargs)
../../../_images/pyvista-ImageDataFilters-close-43fa323110ef5cf7_00_00.png

Use a much larger kernel to also fill the small black circle.

>>> closed = im.close(kernel_size=25)
>>> closed.plot(**kwargs)
../../../_images/pyvista-ImageDataFilters-close-43fa323110ef5cf7_01_00.png

Since closing is the inverse of opening, we can alternatively use open() to fill the white foreground values instead of the black background.

>>> opened = im.open(kernel_size=25)
>>> opened.plot(**kwargs)
../../../_images/pyvista-ImageDataFilters-close-43fa323110ef5cf7_02_00.png