Segment

class celltk.segment.Segment(output='mask', **kwargs)
Parameters

output (str, default: 'mask') –

adaptive_thres(image, relative_thres=0.1, sigma=50, connectivity=2)

Applies Gaussian blur to the image and marks pixels that are brighter than the blurred image by a specified threshold.

Parameters
  • image (Image) –

  • relative_thres (float, default: 0.1) –

  • sigma (float, default: 50) –

  • connectivity (int, default: 2) –

Return type

Mask[uint8]

Returns

agglomeration_segmentation(image, seeds=None, agglom_min=0.7, agglom_max=None, compact=100, seed_thres=0.975, seed_min_size=12, steps=50, connectivity=2)

Starts from a seed mask determined by a constant threshold. Then incrementally uses watershed to connect neighboring pixels to each seed.

Parameters
  • image (Image) –

  • seeds (Optional[Mask], default: None) –

  • agglom_min (float, default: 0.7) –

  • agglom_max (Optional[float], default: None) –

  • compact (float, default: 100) –

  • seed_thres (float, default: 0.975) –

  • seed_min_size (float, default: 12) –

  • steps (int, default: 50) –

  • connectivity (int, default: 2) –

Return type

Mask

Returns

binary_fill_holes(mask, fill_border=True, iterations=False, kernel_radius=4, max_length=45, in_place=True, method='ndi', **kwargs)

Fills holes in a binary image. Two algorithms are available, one from SimpleITK and one from scipy. Neither algorithm fills holes on the border of the image (due to ambiguity in defining a hole). This function can optionally fill holes on the border by guessing which holes are holes in a larger object.

Parameters
  • mask (Mask) –

  • fill_border (bool, default: True) – If True, attempts to fill holes on boundary of image. Note, there is ambiguity in defining a hole on the boundary.

  • iterations (Union[bool, int], default: False) –

  • max_length (int, default: 45) –

  • in_place (bool, default: True) –

  • method (str, default: 'ndi') –

  • kwargs

Parma kernel_radius

Return type

Mask

Returns

Parameters

kernel_radius (int, default: 4) –

canny_edge_segmentation(image, sigma=1.0, low_thres=None, high_thres=None, use_quantiles=False, fill_holes=False)

Uses a Canny filter to find edges in the image.

Parameters
  • image (Image) –

  • sigma (float, default: 1.0) –

  • low_thresh

  • high_thres (Optional[float], default: None) –

  • use_quantiles (bool, default: False) –

  • fill_holes (bool, default: False) –

  • low_thres (Optional[float], default: None) –

Return type

Mask

Returns

chan_vese_dense_levelset(image, seeds, iterations=70, smoothing=0, curve_weight=1, area_weight=1, lambda1=1, lambda2=1, epsilon=1)

Calculates the Chan-Vese level set from initial seeds. Similar to Segment.morphological_acwe, but more customizable.

Parameters
  • image (Image) –

  • mask

  • iterations (int, default: 70) –

  • smoothing (float, default: 0) –

  • curve_weight (float, default: 1) –

  • area_weight (float, default: 1) –

  • lambda1 (float, default: 1) –

  • lambda2 (float, default: 1) –

  • epsilon (float, default: 1) –

  • seeds (Mask) –

Return type

Mask

Returns

clean_labels(mask, min_radius=3, max_radius=20, open_size=3, clear_border=True, relabel=False, sequential=False, connectivity=2)

Applies light cleaning intended for relatively small mammalian nuclei. Fills holes, removes small, large, and border-connected objectes, and then applies morphological opening.

Parameters
  • mask (Mask) – Mask of uniquely labeled cell nuclei

  • min_radius (float, default: 3) – Objects smaller than a circle with radius min_radius are removed.

  • max_radius (float, default: 20) – Objects larger than a circle with radius max_radius are removed.

  • open_size (int, default: 3) – Side length of the footprint used for the morphological opening operation.

  • clear_border (Union[bool, int], default: True) – If True or int, adny object connected to the border is removed. If an integer is given, objects that many pixels from the border are also removed.

  • relabel (bool, default: False) – If True, objects are relabeled after cleaning. This can help separate objects that were connected before cleaning, but are no longer connected.

  • sequential (bool, default: False) – If True, objects are relabeled with sequential labels.

  • connectivity (int, default: 2) – Determines the local neighborhood around a pixel for defining connected objects. Connectivity is defined as the number of orthogonal steps needed to reach a pixel.

Return type

Mask

Returns

Mask with cleaned labels

confidence_connected(image, seeds, radius=1, multiplier=4.5, iterations=10)

Uses mean and variance to connect pixels that are within a specified window (multiplier) of the values in the seed region.

Parameters
  • image (Image) –

  • seeds (Mask) –

  • radius (int, default: 1) –

  • multiplier (float, default: 4.5) –

  • iterations (int, default: 10) –

Return type

Mask

Returns

constant_thres(image, thres=1000, negative=False, connectivity=2, relative=False)

Labels pixels above or below a threshold value.

Parameters
  • image (Image) –

  • thres (Union[int, float], default: 1000) – Threshold value to use

  • negative (bool, default: False) – If True, labels pixels below thres

  • connectivity (int, default: 2) –

  • relative (bool, default: False) – If True, threshold is set relative to maximum pixel value in image.

Return type

Mask[uint8]

Returns

Labeled binary mask

convex_hull_object(mask, connectivity=2)

Computes the convex hull of each object found in a binary mask. If mask is not binary already, will be binarized.

Parameters
  • mask (Mask) –

  • connectivity (int, default: 2) –

Return type

Mask

Returns

expand_to_cytoring(labels, image=None, distance=1, margin=0, thres=None, relative=True, mask=None)

Expands labels in the given mask by a fixed distance.

Parameters
  • labels (Mask) –

  • distance (float, default: 1) –

  • margin (int, default: 0) –

  • thres (Optional[float], default: None) –

  • relative (bool, default: True) –

  • mask (Optional[Mask], default: None) – If given, only expands labels into indices that are True in mask.

  • image (Optional[Image], default: None) –

Return type

Mask

Returns

fast_marching_level_set(image, seeds, n_points=0)

Computes the distance from the seeds based on a fast marching algorithm.

Parameters
  • image (Image) –

  • seeds (Mask) –

  • n_points (int, default: 0) –

Return type

Mask

Returns

filter_objects_by_props(mask, properties, limits, image=None)

Removes objects from a labeled image based on shape properties. Accepted properties are ones available for skimage.measure.regionprops.

Parameters
  • mask (Mask) –

  • image (Optional[Image], default: None) –

  • properties (List[str]) –

  • limits (Collection[Tuple[float]]) –

Return type

Mask

Returns

find_boundaries(mask, connectivity=2, mode='thick', keep_labels=True)

Returns the outlines of the objects in the mask, optionally preserving the labels.

Parameters
  • mask (Mask) –

  • connectivity (int, default: 2) –

  • mode (str, default: 'thick') –

  • keep_labels (bool, default: True) –

Return type

Mask

Returns

label(mask, connectivity=2)

Uniquely labels connected pixels.

Parameters
  • mask (Mask) – Mask of objects to be labeled. Can be binary or greyscale.

  • connectivity (int, default: 2) – Determines the local neighborhood around a pixel. Defined as the number of orthogonal steps needed to reach a pixel.

Return type

Mask

Returns

Labeled mask.

label_by_voting(masks, label_undecided=0)

Applies a voting algorithm to determine the value of each pixel. Can be used to get the combined result from multiple masks.

Parameters
  • masks (Mask) – List of masks to use in voting.

  • label_undecided (int, default: 0) –

Return type

Mask

Returns

level_set_mask(image, levelset='checkerboard', size=None, center=None, label=False)

Returns a binary level set of various shapes.

Parameters
  • image (Image) –

  • levelset (str, default: 'checkerboard') –

  • size (Union[float, int, None], default: None) –

  • center (Optional[Tuple[int]], default: None) –

  • label (bool, default: False) –

Return type

Mask

Returns

li_thres(image, inside_val=0, outside_val=1)

Applies Li’s thresholding method.

Parameters
  • image (Image) –

  • inside_val (int, default: 0) –

  • outside_val (int, default: 1) –

Return type

Mask[uint8]

Returns

misic_predict(image, model_path=None, weight_path=None, batch=None)

Wrapper for implementation of Misic. Original paper from Panigrahi and colleagues.

Parameters
  • image (Image) –

  • model_path (Optional[str], default: None) –

  • weight_path (Optional[str], default: None) –

  • batch (Optional[int], default: None) –

Return type

Mask

Returns

morphological_acwe(image, seeds='checkerboard', iterations=10, smoothing=1, lambda1=1, lambda2=1, connectivity=1, thres=None, voronoi=False, keep_labels=True, clean_before_match=True)

Uses morphological active contours without edges (ACWE) algorithm to segment objects. Optionally, applies a vornoi mask after segmentation to separate objects.

Parameters
  • image (Image) –

  • seeds (Mask[Optional], default: 'checkerboard') –

  • iterations (int, default: 10) –

  • smoothing (int, default: 1) –

  • lambda1 (float, default: 1) –

  • lambda2 (float, default: 1) –

  • connectivity (int, default: 1) –

  • thres (Optional[float], default: None) –

  • voronoi (bool, default: False) –

  • keep_labels (bool, default: True) –

  • clean_before_match (bool, default: True) –

Return type

Mask

Returns

morphological_geodesic_active_contour(image, seeds='checkerboard', iterations=10, smoothing=1, threshold='auto', balloon=0)

Applies geodesic active contours with morphological operators to segment objects. Light wrapper for skimage.segmentation.morphological_geodesic_active_contour.

Parameters
  • image (Image) –

  • seeds (Mask[Optional], default: 'checkerboard') –

  • iterations (int, default: 10) –

  • smoothing (int, default: 1) –

  • threshold (float, default: 'auto') –

  • balloon (float, default: 0) –

Return type

Mask

Returns

morphological_watershed(image, watershed_line=True)

Runs morphological watershed on the image.

Parameters
  • image (Image) –

  • watershed_line (bool, default: True) –

Return type

Mask

Returns

multiotsu_thres(image, classes=2, roi=None, nbins=256, hist=None, binarize=False)

Applies Otsu’s thresholding with multiple classes. By default, returns a mask with all classes included, but can be limited to only returning some of the classes.

Parameters
  • image (Image) –

  • classes (int, default: 2) – Which classes to be included in final image.

  • roi (Union[int, Collection[int], None], default: None) –

  • nbins (int, default: 256) –

  • hist (Optional[ndarray], default: None) –

  • binarize (bool, default: False) – If True, a binary image is returned.

Return type

Mask[uint8]

Returns

otsu_thres(image, nbins=256, connectivity=2, buffer=0.0, fill_holes=False)

Uses Otsu’s method to determine the threshold and labels all pixels above the threshold.

Parameters
  • image (Image) –

  • nbins (int, default: 256) –

  • connectivity (int, default: 2) –

  • buffer (float, default: 0.0) – Adjust threshold value by a specific buffer.

  • fill_holes (bool, default: False) – If True, applies binary fill hole operation after thresholding

Return type

Mask[uint8]

Returns

random_walk_segmentation(image, seeds=None, seed_thres=0.99, seed_min_size=12, beta=80, tol=0.01, seg_thres=0.85)

Uses random aniostropic diffusion from given seeds to assign each pixel in the image to a specific seed.

Parameters
  • image (Image) –

  • seeds (Optional[Mask], default: None) –

  • seed_thres (float, default: 0.99) –

  • seed_min_size (float, default: 12) –

  • beta (float, default: 80) –

  • tol (float, default: 0.01) –

  • seg_thres (float, default: 0.85) –

Return type

Mask

Returns

regional_extrema(image, find='minima', fully_connected=True, thres=None, min_size=12)

Finds regional minima/maxima within flat areas. Generally requires an image with little noise.

Parameters
  • image (Image) –

  • find (str, default: 'minima') –

  • fully_connected (bool, default: True) –

  • thres (Optional[float], default: None) –

  • min_size (int, default: 12) –

Return type

Mask

Returns

Mask with regional extrema labeled

remove_nuc_from_cyto(cyto_mask, nuc_mask, val_match=False, erosion=False)

Removes nuclei from a cytoplasmic mask, optionally eroding the final mask.

Parameters
  • cyto_mask (Mask) –

  • nuc_mask (Mask) –

  • val_match (bool, default: False) –

  • erosion (bool, default: False) –

Return type

Mask

Returns

shape_detection_level_set(edge_potential, initial_level_set, curvature=1, propagation=1, iterations=1000)

Propagates an initial level set to edges found in an edge potential image. This function will likely not work well on an unmodified input image. Use the edge detection functions in Process to create an edge potential image.

Parameters
  • edge_potential (Image) –

  • initial_level_set (Mask) –

  • curvature (float, default: 1) –

  • propagation (float, default: 1) –

  • iterations (int, default: 1000) –

Return type

Mask

Returns

sitk_filter_pipeline(mask, filters, kwargs=[])

Applies arbitrary filters from the SimpleITK package. Any image filter in SimpleITK, and most arguments for those filters, can be used.

Parameters
  • mask (Mask) –

  • filters (Collection[str]) – List of filters from SimpleITK to apply.

  • kwargs (Collection[dict], default: []) – List of dictionaries containing the kwargs for the selected filters. Length must equal length of filters.

Return type

Mask

Returns

sitk_label(mask, min_size=None)

Uniquely labels connected pixels. Also removes connected objects below a specified size.

Parameters
  • mask (Mask) – Mask of objects to be labeled. Can be binary or greyscale.

  • min_size (Optional[float], default: None) – If given, all objects smaller than min_size are removed.

Return type

Mask

Returns

Labeled mask

threshold_level_set(image, initial_level_set, lower_thres=None, upper_thres=None, propagation=1, curvature=1, iterations=1000, max_rms_error=0.02)

Level set segmentation method based on intensity values.

Parameters
  • image (Image) –

  • initial_level_set (Mask) –

  • lower_thres (Optional[float], default: None) –

  • upper_thres (Optional[float], default: None) –

  • propagation (float, default: 1) –

  • curvature (float, default: 1) –

  • iterations (int, default: 1000) –

  • max_rms_error (float, default: 0.02) –

Return type

Mask

Returns

unet_predict(image, weight_path, roi=2, batch=None, classes=3)

Uses a UNet-based neural net to predict the label of each pixel in the input image. This function returns the probability of a specific region of interest, not a labeled mask.

Parameters
  • image (Image) –

  • weight_path (str) –

  • roi (Union[int, str], default: 2) –

  • batch (Optional[int], default: None) –

  • classes (int, default: 3) –

Return type

Image

Returns

watershed_from_markers(image, mask, watershed_line=True, remove_large=True)

Runs morphological watershed from given seeds.

Parameters
  • image (Image) –

  • mask (Mask) –

  • watershed_line (bool, default: True) –

  • remove_large (bool, default: True) –

Return type

Mask

Returns

watershed_ift_segmentation(image, seeds=None, seed_thres=0.975, seed_min_size=12, connectivity=2)

Applies watershed from the given seeds using the image foresting transform algorithm.

Parameters
  • image (Image) –

  • seeds (Optional[Mask], default: None) –

  • seed_thres (float, default: 0.975) –

  • seed_min_size (float, default: 12) –

  • connectivity (int, default: 2) –

Return type

Mask

Returns