Nucleus segmentation

Functions used to segment nuclei.

Apply thresholding

Thresholding is the most standard and direct binary segmentation method:

bigfish.segmentation.thresholding(image, threshold)

Segment a 2-d image to discriminate object from background applying a threshold.

Parameters:
imagenp.ndarray

A 2-d image to segment with shape (y, x).

thresholdint or float

Pixel intensity threshold used to discriminate foreground from background.

Returns:
image_segmentednp.ndarray, bool

Binary 2-d image with shape (y, x).

Apply a Unet-based model (3-classes)

Load a pretrained model:

Segment nuclei:

See an example of application here.

bigfish.segmentation.unet_3_classes_nuc()

Load a pretrained Unet model to predict 3 classes from nucleus images: background, edge and foreground.

Returns:
modeltensorflow.keras.model object

Pretrained Unet model.

bigfish.segmentation.apply_unet_3_classes(model, image, target_size=None, test_time_augmentation=False)

Segment image with a 3-classes trained model.

Parameters:
modeltensorflow.keras.model object

Pretrained Unet model that predicts 3 classes from nucleus or cell images (background, edge and foreground).

imagenp.ndarray, np.uint

Original image to segment with shape (y, x).

target_sizeint

Resize image before segmentation. A squared image is resize to target_size. A rectangular image is resize such that its smaller dimension equals target_size.

test_time_augmentationbool

Apply test time augmentation or not. The image is augmented 8 times and the final segmentation is the average result over these augmentations.

Returns:
image_label_prednp.ndarray, np.int64

Labelled image. Each instance is characterized by the same pixel value.

bigfish.segmentation.from_3_classes_to_instances(label_3_classes)

Extract instance labels from 3-classes Unet output.

Parameters:
label_3_classesnp.ndarray, np.float32

Model prediction about the nucleus surface and boundaries, with shape (y, x, 3).

Returns:
labelnp.ndarray, np.int64

Labelled image. Each instance is characterized by the same pixel value.


Remove segmented nuclei

Remove segmented nucleus instances to perform a new segmentation on the residual image:

bigfish.segmentation.remove_segmented_nuc(image, nuc_mask, size_nuclei=2000)

Remove the nuclei we have already segmented in an image.

  1. We start from the segmented nuclei with a light dilation. The missed nuclei and the background are set to 0 and removed from the original image.

  2. We reconstruct the missing nuclei by small dilation. As we used the original image to set the maximum allowed value at each pixel, the background pixels remain unchanged. However, pixels from the missing nuclei are partially reconstructed by the dilation. The reconstructed image only differs from the original one where the nuclei have been missed.

  3. We subtract the reconstructed image from the original one.

  4. From the few missing nuclei kept and restored, we build a binary mask (dilation, small object removal).

  5. We apply this mask to the original image to get the original pixel intensity of the missing nuclei.

  6. We remove pixels with a too low intensity.

Parameters:
imagenp.ndarray, np.uint

Original nuclei image with shape (y, x).

nuc_masknp.ndarray,

Result of the segmentation (with instance differentiation or not).

size_nucleiint

Threshold above which we detect a nuclei.

Returns:
image_without_nucnp.ndarray

Image with shape (y, x) and the same dtype of the original image. Nuclei previously detected in the mask are removed.