File size: 1,570 Bytes
712b117 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import numpy as np
__all__ = ["label_points"]
def label_points(coords, output_shape):
"""Assign unique integer labels to coordinates on an image mask
Parameters
----------
coords : ndarray
An array of N coordinates with dimension D
output_shape : tuple
The shape of the mask on which `coords` are labelled
Returns
-------
labels: ndarray
A mask of zeroes containing unique integer labels at the `coords`
Examples
--------
>>> import numpy as np
>>> from skimage.util._label import label_points
>>> coords = np.array([[0, 1], [2, 2]])
>>> output_shape = (5, 5)
>>> mask = label_points(coords, output_shape)
>>> mask
array([[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]], dtype=uint64)
Notes
-----
- The labels are assigned to coordinates that are converted to
integer and considered to start from 0.
- Coordinates that are out of range of the mask raise an IndexError.
- Negative coordinates raise a ValueError
"""
if coords.shape[1] != len(output_shape):
raise ValueError("Dimensionality of points should match the " "output shape")
if np.any(coords < 0):
raise ValueError("Coordinates should be positive and start from 0")
np_indices = tuple(np.transpose(np.round(coords).astype(int, copy=False)))
labels = np.zeros(output_shape, dtype=np.uint64)
labels[np_indices] = np.arange(1, coords.shape[0] + 1)
return labels
|