Skip to content
Snippets Groups Projects
Commit ecfd7c08 authored by Christian Kento Rasmussen's avatar Christian Kento Rasmussen
Browse files

Add watershed segmentation algorithm

parent 7f812a6f
No related branches found
No related tags found
No related merge requests found
import matplotlib.pyplot as plt
import numpy as np
import torch
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
def watershed_segment(volume):
""" Apply watershed algorithm to a 3D volume.
Args:
volume (np.array | torch.Tensor): A 3D volume.
Returns:
np.array: Segmented watershed Connected Components.
"""
volume = volume > 1
distance = ndi.distance_transform_edt(volume)
coords = peak_local_max(distance, footprint=np.ones((3,)*volume.ndim), labels=volume)
mask = np.zeros(distance.shape, dtype=bool)
mask[tuple(coords.T)] = True
markers, _ = ndi.label(mask)
labels = watershed(-distance, markers, mask=volume)
return labels
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment