bundles / scipy latest / scipy / ndimage / _measurements / label
function
scipy.ndimage._measurements:label
Signature
def label ( input , structure = None , output = None ) Summary
Label features in an array.
Parameters
input: array_likeAn array-like object to be labeled. Any non-zero values in
inputare counted as features and zero values are considered the background.structure: array_like, optionalA structuring element that defines feature connections.
structuremust be centrosymmetric (see Notes). If no structuring element is provided, one is automatically generated with a squared connectivity equal to one. That is, for a 2-Dinputarray, the default structuring element is[[0,1,0], [1,1,1], [0,1,0]]
output: (None, data-type, array_like), optionalIf
outputis a data type, it specifies the type of the resulting labeled feature array. Ifoutputis an array-like object, thenoutputwill be updated with the labeled features from this function. This function can operate in-place, by passing output=input. Note that the output must be able to store the largest label, or this function will raise an Exception.
Returns
label: ndarray or intAn integer ndarray where each unique feature in
inputhas a unique label in the returned array.num_features: intHow many objects were found.
If
outputis None, this function returns a tuple of (labeled_array, num_features).If
outputis a ndarray, then it will be updated with values inlabeled_arrayand only num_features will be returned by this function.
Notes
A centrosymmetric matrix is a matrix that is symmetric about the center. See [1] for more information.
The structure matrix must be centrosymmetric to ensure two-way connections. For instance, if the structure matrix is not centrosymmetric and is defined as
[[0,1,0], [1,1,0], [0,0,0]]
and the input is
[[1,2], [0,3]]
then the structure matrix would indicate the entry 2 in the input is connected to 1, but 1 is not connected to 2.
Array API Standard Support
label 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-arrayapifor more information.
Examples
Create an image with some features, then label it using the default (cross-shaped) structuring element:from scipy.ndimage import label, generate_binary_structure import numpy as np a = np.array([[0,0,1,1,0,0], [0,0,0,1,0,0], [1,1,0,0,1,0], [0,0,0,1,0,0]]) labeled_array, num_features = label(a)✓
num_features labeled_array✓
s = generate_binary_structure(2,2)
✓s = [[1,1,1], [1,1,1], [1,1,1]]✓
labeled_array, num_features = label(a, structure=s)
✓num_features labeled_array✓
See also
- find_objects
generate a list of slices for the labeled features (or objects); useful for finding features' position or dimensions
Aliases
-
scipy.ndimage.label