Usage#

merge_stardist_masks.naive_fusion.naive_fusion(dists: ndarray[Any, dtype[float64]], probs: ndarray[Any, dtype[float64]], rays: Rays_Base | None = None, prob_thresh: float = 0.5, grid: Tuple[int, ...] = (2, 2, 2), no_slicing: bool = False, max_full_overlaps: int = 2, erase_probs_at_full_overlap: bool = False, show_overlaps: bool = False, respect_probs: bool = False) ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]

Merge overlapping masks given by dists, probs, rays.

Performs a naive iterative scheme to merge the masks that a StarDist network has calculated to generate a label image. This function can perform 2D and 3D segmentation. For 3D segmentation rays has to be passed from the StarDist model.

Parameters:
  • dists (ndarray[Any, dtype[float64]]) – 3- or 4-dimensional array representing distances of each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x, n_rays), for 3D predictions it is (len_z, len_y, len_x, n_rays).

  • probs (ndarray[Any, dtype[float64]]) – 2- or 3-dimensional array representing the probabilities for each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x), for 3D predictions it is (len_z, len_y, len_x).

  • rays (Rays_Base | None) – For 3D predictions rays must be set otherwise a ValueError is raised. It should be the Rays_Base instance used by the StarDist model.

  • prob_thresh (float) – Only masks with probability above this threshold are considered.

  • grid (Tuple[int, ...]) – Should be of length 2 for 2D and of length 3 for 3D segmentation. This is the grid information about the subsampling occuring in the StarDist model.

  • no_slicing (bool) – For very big and winded objects this should be set to True. However, this might result in longer calculation times.

  • max_full_overlaps (int) – Maximum no. of full overlaps before current object is treated as finished.

  • erase_probs_at_full_overlap (bool) – If set to True probs are set to -1 whenever a full overlap is detected.

  • show_overlaps (bool) – If set to true, overlaps are set to -1.

  • respect_probs (bool) – If set to true, overlapping elements are overwritten by considering their probabilities. Only works when show_overlaps is ‘false’.

Returns:

The label image with uint16 labels. For 2D, the shape is (len_y * grid[0], len_x * grid[1]) and for 3D it is (len_z * grid[0], len_y * grid[1], len_z * grid[2]).

Raises:
  • ValueError – If rays is None and 3D inputs are given or when probs.ndim != len(grid). # noqa: DAR402 ValueError

  • NotImplementedError – If grid is anisotropic and respect_probs is set to true.

Return type:

ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]

Example

>>> from merge_stardist_masks.naive_fusion import naive_fusion
>>> from stardist.rays3d import rays_from_json
>>> probs, dists = model.predict(img)  # model is a 3D StarDist model
>>> rays = rays_from_json(model.config.rays_json)
>>> lbl = naive_fusion(dists, probs, rays, grid=model.config.grid)