_label.py 1.5 KB

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