Watershed¶
Watershed segmentation module.
The watershed segmentation method is useful to segment tissue like intensity images. It often requires pre-processing (filtering) and requires prior computation of seeds.
- timagetk.algorithms.watershed.seeds_detection(image, h_min, **kwargs)[source]¶
Seed detection algorithm by local minima detection and labelling.
- Parameters:
image (timagetk.SpatialImage) – The intensity image to use for local minima detection.
h_min (int) – Height-minima value to use for local minima detection.
connectivity (int in {4, 6, 8, 10, 18, 26}) – The connectivity of the structuring elements,
18
by default.
See also
- Returns:
LabelledImage – The seeds image
Examples
>>> from timagetk import TissueImage3D >>> from timagetk.io.util import shared_dataset >>> from timagetk.io import imread >>> from timagetk.algorithms.watershed import seeds_detection >>> from timagetk.visu.stack import stack_browser >>> img_path = shared_dataset("p58")[0] >>> image = imread(img_path) >>> seeds = seeds_detection(image, h_min=15)
- timagetk.algorithms.watershed.watershed(image, seeds, **kwargs)[source]¶
Seeded watershed algorithm.
- Parameters:
image (timagetk.SpatialImage or timagetk.MultiChannelImage) – Intensity image to segment.
seeds (timagetk.SpatialImage) – Seeds image, each marker have an unique value
channel (str) – If a
MultiChannelImage
is used as input image, select the channel to use with this algorithm.labelchoice (str in {"first", "min", "most"}) – How to deal with “conflicts”, i.e. where several labels meet. See the “Notes” section for a detailled explanations.
max_iterations (int) – Use this to set a maximal number of iterations, stop the algorithm before convergence (by default).
params (str, optional) – CLI parameter string used by
vt.watershed
method.
- Returns:
LabelledImage – The segmented image.
- Raises:
TypeError – If
image
is not aSpatialImage
. Ifseeds
is not aLabelledImage
.ValueError – If the image returned by
vt.watershed
isNone
.
See also
timagetk.third_party.vt_parser.LABELCHOICE_METHODS
,timagetk.third_party.vt_parser.DEF_LABELCHOICE
,timagetk.third_party.vt_parser.watershed_kwargs
,timagetk.third_party.vt_parser.general_kwargs
,timagetk.third_party.vt_parser.parallel_kwargs
Notes
Explanations of available
labelchoice
keyword argument:first: the first label wins (default);
min: the less represented label wins;
most: the most represented label wins;
Example
>>> from timagetk.io.util import shared_dataset >>> from timagetk.io import imread >>> from timagetk.algorithms.linearfilter import gaussian_filter >>> from timagetk.algorithms.regionalext import regional_extrema >>> from timagetk.algorithms.connexe import connected_components >>> from timagetk.algorithms.watershed import watershed >>> from timagetk.visu.mplt import grayscale_imshow >>> img_path = shared_dataset("p58")[2] >>> image = imread(img_path)
>>> # EXAMPLE #1 - 2D segmentation >>> zslice = 25 >>> zslice_img = image.get_slice(zslice, 'z') >>> # Smooth the 2D slice to avoid too many local minimas: >>> sigma = 1.2 >>> smooth_img = gaussian_filter(zslice_img, sigma=sigma) >>> # Search for local minimas: >>> hmin = 50 >>> regext_img = regional_extrema(smooth_img,height=hmin,method='minima') >>> # Label detected local minima based on connectivity: >>> conn_img = connected_components(regext_img,connectivity=8,low_threshold=1,high_threshold=hmin) >>> # Segment the 2D slice with smoothed image and detected seeds: >>> labelled_img = watershed(smooth_img, conn_img) >>> # Display each steps: >>> images = [zslice_img, smooth_img, regext_img, conn_img, labelled_img] >>> titles = [f"Original (z-slice: {zslice})", f"Smoothed (sigma={sigma})", f"Local minima (height={hmin})", "Connexe components labelling", "Watershed"] >>> grayscale_imshow(images, title=titles, val_range=['type', 'type', 'auto', 'auto', 'auto'], cmap=['gray', 'gray', 'viridis', 'glasbey', 'glasbey'], max_per_line=5)
>>> # EXAMPLE #2 - 3D segmentation >>> # Smooth the 2D slice to avoid too many local minimas: >>> sigma = 1.2 >>> smooth_img = gaussian_filter(image, sigma=sigma) >>> # Search for local minimas: >>> hmin = 5 >>> regext_img = regional_extrema(smooth_img,height=hmin,method='minima') >>> # Label detected local minima based on connectivity: >>> conn_img = connected_components(regext_img,connectivity=26,low_threshold=1,high_threshold=hmin) >>> # Segment the 2D slice with smoothed image and detected seeds: >>> labelled_img = watershed(smooth_img, conn_img) >>> # Display each steps: >>> zslice = 25 >>> images = [image.get_slice(zslice, 'z'), smooth_img.get_slice(zslice, 'z'), regext_img.get_slice(zslice, 'z'), conn_img.get_slice(zslice, 'z'), labelled_img.get_slice(zslice, 'z')] >>> titles = [f"Original (z-slice: {zslice})", f"Smoothed (sigma={sigma})", f"Local minima (height={hmin})", "Connexe components labelling", "Watershed"] >>> grayscale_imshow(images, title=titles, val_range=['type', 'type', 'auto', 'auto', 'auto'], cmap=['gray', 'gray', 'viridis', 'glasbey', 'glasbey'], max_per_line=5)