Connexe

Connected components detection and labelling.

WARNING: even if the documentation from vt-python says to use -label as argument for labelling connected components, it’s -labels!

timagetk.algorithms.connexe.connected_components(image, **kwargs)[source]

Connected components labeling.

Parameters:
  • image (timagetk.SpatialImage or timagetk.MultiChannelImage or timagetk.LabelledImage) – Intensity or height image to label by detecting connected components.

  • channel (str) – If a MultiChannelImage is used as input image, select the channel to use with this algorithm.

  • low_threshold (int or float) – Low threshold to binarize input image.

  • high_threshold (int or float) – High threshold to binarize input image.

  • max (bool) – If True keep the largest connected component (implies binary output). Else all detected components are kept. Equivalent to max_number_cc=1.

  • min_size_cc (int) – Minimal size, in voxels, of the connected component. Otherwise, all detected components are kept by default.

  • max_number_cc (int) – Maximal number of connected components to keep, the largest are kept first. Otherwise, all detected components are kept by default.

  • connectivity ({4, 6, 8, 10, 18, 26}) – The connectivity of the structuring elements, DEF_CONNECTIVITY by default.

  • binary_output (bool) – If True all valid connected components have the same value (1). Else each detected components have their own label (default).

  • label (bool) – If True (default) returns one label per connected component.

  • size (bool) – If True the value attributed to the points of a connected component is the size of the connected components This allows selecting w.r.t to size afterward.

  • sort_sizes (bool) – If True the labels are ordered by decreasing size of connected components, implies label=True (unless changed afterwards).

  • params (str, optional) – CLI parameter string used by vt.connexe method.

Returns:

LabelledImage – Connected component image.

Raises:
  • TypeError – If image is not a SpatialImage or a LabelledImage.

  • ValueError – If the image returned by vt.connexe is None.

Notes

Connectivity is among the 4-, 6-, 8-, 18-, 26-neighborhoods. 4 and 8 are for 2D images, the others for 3D images.

Example

>>> from timagetk.io.util import shared_dataset
>>> from timagetk.io import imread
>>> from timagetk.algorithms.regionalext import regional_extrema
>>> from timagetk.algorithms.connexe import connected_components
>>> from timagetk.visu.mplt import grayscale_imshow
>>> img_path = shared_dataset("p58")[0]
>>> image = imread(img_path)
>>> # - Example #1: Connected components labelling of threshold intensity image:
>>> labeling = connected_components(image, low_threshold=60)
>>> titles = [f"Original", "Connexe components labeling"]
>>> grayscale_imshow([image, labeling], slice_id=20, title=titles, val_range=['type', 'auto'], cmap=['gray', 'viridis'])
>>> # - Example #2: Connected components labelling of local minima image:
>>> zslice = 20
>>> subimg = image.get_slice(zslice, 'z')
>>> # - H-minima transformation for h-min threshold and 8-connexe pixels:
>>> hmin = 45
>>> extmin_img = regional_extrema(subimg, hmin,"minima", connectivity=8)
>>> # - Connected component labelling after hysteresis thresholding:
>>> seed_image = connected_components(extmin_img, connectivity=8, low_threshold=1, high_threshold=hmin)
>>> images = [subimg, extmin_img, seed_image]
>>> titles = [f"Original (z-slice: {zslice})", f"Local minima (height={hmin})", "Connexe components labelling"]
>>> grayscale_imshow(images, title=titles, val_range=['type', 'auto', 'auto'], cmap=['gray', 'viridis', 'glasbey'])
>>> # - EXAMPLE #3: Background detection (assumed to be the biggest label)
>>> zslice = 20
>>> subimg = image.get_slice(zslice, 'z')
>>> hmin = int(subimg.max() // 30)
>>> extmin_img = regional_extrema(subimg, hmin, "minima")
>>> bkgd_seed = connected_components(extmin_img, high_threshold=hmin, max=True)
>>> bkgd_seed.labels()
>>> grayscale_imshow([subimg, bkgd_seed], val_range=['type', [0, 255]], cmap=['gray', 'viridis'])