_felzenszwalb.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import numpy as np
  2. from ._felzenszwalb_cy import _felzenszwalb_cython
  3. from .._shared import utils
  4. @utils.channel_as_last_axis(multichannel_output=False)
  5. def felzenszwalb(image, scale=1, sigma=0.8, min_size=20, *, channel_axis=-1):
  6. """Computes Felsenszwalb's efficient graph based image segmentation.
  7. Produces an oversegmentation of a multichannel (i.e. RGB) image
  8. using a fast, minimum spanning tree based clustering on the image grid.
  9. The parameter ``scale`` sets an observation level. Higher scale means
  10. less and larger segments. ``sigma`` is the diameter of a Gaussian kernel,
  11. used for smoothing the image prior to segmentation.
  12. The number of produced segments as well as their size can only be
  13. controlled indirectly through ``scale``. Segment size within an image can
  14. vary greatly depending on local contrast.
  15. For RGB images, the algorithm uses the euclidean distance between pixels in
  16. color space.
  17. Parameters
  18. ----------
  19. image : (M, N[, 3]) ndarray
  20. Input image.
  21. scale : float
  22. Free parameter. Higher means larger clusters.
  23. sigma : float
  24. Width (standard deviation) of Gaussian kernel used in preprocessing.
  25. min_size : int
  26. Minimum component size. Enforced using postprocessing.
  27. channel_axis : int or None, optional
  28. If None, the image is assumed to be a grayscale (single channel) image.
  29. Otherwise, this parameter indicates which axis of the array corresponds
  30. to channels.
  31. .. versionadded:: 0.19
  32. ``channel_axis`` was added in 0.19.
  33. Returns
  34. -------
  35. segment_mask : (M, N) ndarray
  36. Integer mask indicating segment labels.
  37. References
  38. ----------
  39. .. [1] Efficient graph-based image segmentation, Felzenszwalb, P.F. and
  40. Huttenlocher, D.P. International Journal of Computer Vision, 2004
  41. Notes
  42. -----
  43. The `k` parameter used in the original paper renamed to `scale` here.
  44. Examples
  45. --------
  46. >>> from skimage.segmentation import felzenszwalb
  47. >>> from skimage.data import coffee
  48. >>> img = coffee()
  49. >>> segments = felzenszwalb(img, scale=3.0, sigma=0.95, min_size=5)
  50. """
  51. if channel_axis is None and image.ndim > 2:
  52. raise ValueError(
  53. "This algorithm works only on single or " "multi-channel 2d images. "
  54. )
  55. image = np.atleast_3d(image)
  56. return _felzenszwalb_cython(image, scale=scale, sigma=sigma, min_size=min_size)