pyvista.DataSetFilters.pack_labels#
- DataSetFilters.pack_labels(
- sort: bool = False,
- scalars: str | None = None,
- preference: Literal['point', 'cell'] = 'point',
- output_scalars: str | None = None,
- progress_bar: bool = False,
- inplace: bool = False,
Renumber labeled data such that labels are contiguous.
This filter renumbers scalar label data of any type with
Nlabels such that the output labels are contiguous from[0, N). The output may optionally be sorted by label count.The output array
'packed_labels'is added to the output by default, and is automatically set as the active scalars.- Parameters:
- sortbool, default:
False Whether to sort the output by label count in descending order (i.e. from largest to smallest).
- scalars
str,optional Name of scalars to pack. Defaults to currently active scalars.
- preference
str, default: “point” When
scalarsis specified, this is the preferred array type to search for in the dataset. Must be either'point'or'cell'.- output_scalars
str,None Name of the packed output scalars. By default, the output is saved to
'packed_labels'.- progress_barbool, default:
False If
True, display a progress bar. Has no effect if VTK version is lower than 9.3.- inplacebool, default:
False If
True, the mesh is updated in-place.
- sortbool, default:
- Returns:
pyvista.DataSetDataset with packed labels.
See also
sort_labelsSimilar function with
sort=Trueby default.
Notes
This filter uses vtkPackLabels as the underlying method which requires VTK version 9.3 or higher. If vtkPackLabels is not available, packing is done with
NumPyinstead which may be slower. For best performance, consider upgrading VTK.Added in version 0.43.
Examples
Pack segmented image labels.
Load non-contiguous image labels
>>> from pyvista import examples >>> import numpy as np >>> image_labels = examples.load_frog_tissues()
Show range of labels
>>> image_labels.get_data_range() (np.uint8(0), np.uint8(29))
Find ‘gaps’ in the labels
>>> label_numbers = np.unique(image_labels.active_scalars) >>> label_max = np.max(label_numbers) >>> missing_labels = set(range(label_max)) - set(label_numbers) >>> len(missing_labels) 4
Pack labels to remove gaps
>>> packed_labels = image_labels.pack_labels()
Show range of packed labels
>>> packed_labels.get_data_range() (np.uint8(0), np.uint8(25))