relabel_sequential
-
skimage.segmentation.relabel_sequential(label_field, offset=1)
[source] -
Relabel arbitrary labels to {
offset
, ...offset
+ number_of_labels}.This function also returns the forward map (mapping the original labels to the reduced labels) and the inverse map (mapping the reduced labels back to the original ones).
Parameters: label_field : numpy array of int, arbitrary shape
An array of labels.
offset : int, optional
The return labels will start at
offset
, which should be strictly positive.Returns: relabeled : numpy array of int, same shape as
label_field
The input label field with labels mapped to {offset, ..., number_of_labels + offset - 1}.
forward_map : numpy array of int, shape
(label_field.max() + 1,)
The map from the original label space to the returned label space. Can be used to re-apply the same mapping. See examples for usage.
inverse_map : 1D numpy array of int, of length offset + number of labels
The map from the new label space to the original space. This can be used to reconstruct the original label field from the relabeled one.
Notes
The label 0 is assumed to denote the background and is never remapped.
The forward map can be extremely big for some inputs, since its length is given by the maximum of the label field. However, in most situations,
label_field.max()
is much smaller thanlabel_field.size
, and in these cases the forward map is guaranteed to be smaller than either the input or output images.Examples
>>> from skimage.segmentation import relabel_sequential >>> label_field = np.array([1, 1, 5, 5, 8, 99, 42]) >>> relab, fw, inv = relabel_sequential(label_field) >>> relab array([1, 1, 2, 2, 3, 5, 4]) >>> fw array([0, 1, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]) >>> inv array([ 0, 1, 5, 8, 42, 99]) >>> (fw[label_field] == relab).all() True >>> (inv[relab] == label_field).all() True >>> relab, fw, inv = relabel_sequential(label_field, offset=5) >>> relab array([5, 5, 6, 6, 7, 9, 8])
Please login to continue.