Cell segmentation
Functions used to segment cells.
Apply watershed algorithm
Main function to segment cells with a watershed algorithm:
Our segmentation using watershed algorithm can also be perform with two separated steps:
- bigfish.segmentation.cell_watershed(image, nuc_label, threshold, alpha=0.8)
Apply watershed algorithm to segment cell instances.
In a watershed algorithm we consider cells as watershed to be flooded. The watershed relief is inversely proportional to both the pixel intensity and the closeness to nuclei. Pixels with a high intensity or close to labelled nuclei have a low watershed relief value. They will be flooded in priority. Flooding the watersheds allows to propagate nuclei labels through potential cytoplasm areas. The lines separating watershed are the final segmentation of the cells.
- Parameters:
- imagenp.ndarray, np.uint
Cells image with shape (y, x).
- nuc_labelnp.ndarray, np.int64
Result of the nuclei segmentation with shape (y, x) and nuclei instances labelled.
- thresholdint or float
Threshold to discriminate cells surfaces from background.
- alphafloat or int
Weight of the pixel intensity values to compute the watershed relief.
- Returns:
- cell_labelnp.ndarray, np.int64
Segmentation of cells with shape (y, x).
- bigfish.segmentation.get_watershed_relief(image, nuc_label, alpha)
Build a representation of cells as watershed.
In a watershed algorithm we consider cells as watershed to be flooded. The watershed relief is inversely proportional to both the pixel intensity and the closeness to nuclei. Pixels with a high intensity or close to labelled nuclei have a low watershed relief value. They will be flooded in priority. Flooding the watersheds allows to propagate nuclei labels through potential cytoplasm areas. The lines separating watershed are the final segmentation of the cells.
- Parameters:
- imagenp.ndarray, np.uint
Cells image with shape (z, y, x) or (y, x).
- nuc_labelnp.ndarray, np.int64
Result of the nuclei segmentation with shape (y, x) and nuclei instances labelled.
- alphafloat or int
Weight of the pixel intensity values to compute the relief.
- Returns:
- watershed_reliefnp.ndarray, np.uint16
Watershed representation of cells with shape (y, x).
- bigfish.segmentation.apply_watershed(watershed_relief, nuc_label, cell_mask)
Apply watershed algorithm to segment cell instances.
In a watershed algorithm we consider cells as watershed to be flooded. The watershed relief is inversely proportional to both the pixel intensity and the closeness to nuclei. Pixels with a high intensity or close to labelled nuclei have a low watershed relief value. They will be flooded in priority. Flooding the watersheds allows to propagate nuclei labels through potential cytoplasm areas. The lines separating watershed are the final segmentation of the cells.
- Parameters:
- watershed_reliefnp.ndarray, np.uint or np.int
Watershed representation of cells with shape (y, x).
- nuc_labelnp.ndarray, np.int64
Result of the nuclei segmentation with shape (y, x) and nuclei instances labelled.
- cell_masknp.ndarray, bool
Binary image of cells surface with shape (y, x).
- Returns:
- cell_labelnp.ndarray, np.int64
Segmentation of cells with shape (y, x).
Apply a Unet-based model (distance map)
Load a pretrained model:
Segment cells:
See an example of application here.
- bigfish.segmentation.unet_distance_edge_double()
Load a pretrained Unet model to predict foreground and a distance map to edge from nucleus and cell images.
- Returns:
- model
tensorflow.keras.model
object Pretrained Unet model.
- model
- bigfish.segmentation.apply_unet_distance_double(model, nuc, cell, nuc_label, target_size=None, test_time_augmentation=False)
Segment cell with a pretrained model to predict distance map and use it with a watershed algorithm.
- Parameters:
- model
tensorflow.keras.model
object Pretrained Unet model that predict distance to edges and cell surface.
- nucnp.ndarray, np.uint
Original nucleus image with shape (y, x).
- cellnp.ndarray, np.uint
Original cell image to segment with shape (y, x).
- nuc_labelnp.ndarray, np.int64
Labelled nucleus image. Each nucleus is characterized by the same pixel value.
- 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.
- model
- Returns:
- cell_label_prednp.ndarray, np.int64
Labelled cell image. Each cell is characterized by the same pixel value.
- bigfish.segmentation.from_distance_to_instances(label_x_nuc, label_2_cell, label_distance, nuc_3_classes=False, compute_nuc_label=False)
Extract instance labels from a distance map and a binary surface prediction with a watershed algorithm.
- Parameters:
- label_x_nucnp.ndarray, np.float32
Model prediction about the nucleus surface (and boundaries), with shape (y, x, 1) or (y, x, 3).
- label_2_cellnp.ndarray, np.float32
Model prediction about cell surface, with shape (y, x, 1).
- label_distancenp.ndarray, np.uint16
Model prediction about the distance to edges, with shape (y, x, 1).
- nuc_3_classesbool
Nucleus image input is an output from a 3-classes Unet.
- compute_nuc_labelbool
Extract nucleus instance labels.
- Returns:
- nuc_labelnp.ndarray, np.int64
Labelled nucleus image. Each nucleus is characterized by the same pixel value.
- cell_labelnp.ndarray, np.int64
Labelled cell image. Each cell is characterized by the same pixel value.