Main Content

segmentCells2D

Segment 2-D image using Cellpose

Since R2023b

Description

labels = segmentCells2D(cp,im) segments a 2-D numeric intensity image using the model and options specified by the cellpose object cp.

example

labels = segmentCells2D(cp,im,Name=Value) specifies options using one or more name-value arguments. For example, specify ImageCellDiameter=50 if the cells in im are approximately 50 pixels in diameter.

Note

This functionality requires Deep Learning Toolbox™, Computer Vision Toolbox™, and the Medical Imaging Toolbox™ Interface for Cellpose Library. You can install the Medical Imaging Toolbox Interface for Cellpose Library from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

Examples

collapse all

Load a 2-D intensity image.

img = imread("AT3_1m4_01.tif");
imshow(img)

Figure contains an axes object. The axes object contains an object of type image.

Specify the approximate diameter of the cells, in pixels.

averageCellDiameter = 56;

Create a cellpose object. By default, the object uses the cyto2 model from the Cellpose Library.

cp = cellpose(Model="cyto2");

Segment the image, specifying the average cell diameter.

labels = segmentCells2D(cp,img,ImageCellDiameter=averageCellDiameter);

Display the labels overlaid on the original image.

B = labeloverlay(img,labels);
imshow(B)

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

Cellpose model configuration, specified as a cellpose object. The cellpose object properties specify which model to use, and define options such as model ensembling and whether to use a GPU.

Intensity image to segment, specified as a numeric matrix. The Cellpose Library converts the values in im to data type single before segmenting the image.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: segmentCells2D(cp,im,ImageCellDiameter=50) specifies that the cells in im are approximately 50 pixels in diameter.

Auxiliary channel image, specified as a 2-D numeric matrix the same size and data type as im. You can optionally specify this argument if the model accepts a nuclear channel image. If you do not specify this value, the function passes a matrix of zeros the same size as im to the model. The Cellpose Library converts the auxiliary channel image to data type single. This argument corresponds to the chan2 parameter in the Cellpose Library.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Cell diameter in the input image im, specified as a positive numeric scalar, in pixels. By default, the function assumes that im contains cells the same size as in the training data set, and uses the diameter specified by the TrainingCellDiameter property of cp. This argument corresponds to the diameter parameter in the Cellpose Library.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Flow error threshold, specified as a positive numeric scalar. This argument specifies the acceptable error between the flow fields output by the network and flow fields reconstructed from the predicted label. A value between 0.1 and 3 is suitable for most images. This argument corresponds to the flow_threshold parameter in the Cellpose Library.

  • Increasing this value can cause the model to detect more cells, but can generate less accurate boundaries between cells.

  • Decreasing this value can cause the model to generate cleaner boundaries, but miss some cells.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Cell probability threshold, specified as a numeric scalar. The model applies this threshold to the network output, and includes pixels above the threshold in the label predictions. A value between -6 and 6 is suitable for most images. This argument corresponds to the cellprob_threshold parameter in the Cellpose Library.

  • Increasing this value can cause the model to generate cleaner boundaries, but miss some cells.

  • Decreasing this value can cause the model to detect more cells, but can generate less accurate boundaries between cells.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Normalize input image, specified as a numeric or logical 1 (true) or 0 (false). When set to true, the function rescales the input image and the auxiliary channel image, if specified, to the range [0, 1]. The function maps the bottom 1% of intensity values of each image to 0 and the top 1% of intensity values to 1. This argument corresponds to the normalize parameter in the Cellpose Library.

Data Types: logical

Tile input image, specified as a numeric or logical 0 (false) or 1 (true). When set to true, the function tiles the input image into 224-by-224 pixel regions. Segmenting tiled images requires less GPU memory, but takes longer. Tiling does not affect memory requirements on a CPU. This argument corresponds to the tile parameter in the Cellpose Library.

Data Types: logical

Overlap between tiles, specified as a positive numeric scalar. This argument specifies the fractional overlap between adjacent tiles. For each pixel in overlapping regions, the function averages the results across tiles to obtain the final value for the pixel. A value between 0.05 and 0.5 is suitable for most images. This argument corresponds to the tile_overlap parameter in the Cellpose Library.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Augment images, specified as a numeric or logical 0 (false) or 1 (true). When set to true, the function sets Tile to true and TileOverlap to 0.5, generating tiles with 50% overlap. The function flips multiple copies of each tile vertically and horizontally, and then averages results across orientations. Augmentation generally increases accuracy, but decreases speed. This argument corresponds to the augment parameter in the Cellpose Library.

Data Types: logical

GPU batch size, specified as a positive integer. This argument has an effect only when processing images on a GPU with Tile set to true. The batch size specifies the number of tiles per batch. Using a larger batch size increases speed, but requires more memory. This argument corresponds to the batchsize parameter in the Cellpose Library.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Postprocess image at its original size, specified as a numeric or logical 1 (true) or 0 (false). When set to true, the function uses bilinear interpolation to resize the network outputs to the size of the input image before postprocessing them. Resizing before postprocessing can generate smoother boundaries when ImageCellDiameter is larger than the detectable cell diameter of the model. When set to false, the function uses nearest neighbor interpolation to resize label images to the input size after postprocessing. This argument is inversely related to the no_resample parameter in the Cellpose Library.

Data Types: logical

Output Arguments

collapse all

Segmentation label image, returned as a numeric matrix of data type double. Pixel values map to the labels returned by the model. For example, if the model detects two cells, labels has pixel values in the range [0, 2], where background pixels have a value of 0, the first cell has pixel values of 1, and the second cell has pixel values of 2.

References

[1] Stringer, Carsen, Tim Wang, Michalis Michaelos, and Marius Pachitariu. “Cellpose: A Generalist Algorithm for Cellular Segmentation.” Nature Methods 18, no. 1 (January 2021): 100–106. https://doi.org/10.1038/s41592-020-01018-x.

[2] Pachitariu, Marius, and Carsen Stringer. “Cellpose 2.0: How to Train Your Own Model.” Nature Methods 19, no. 12 (December 2022): 1634–41. https://doi.org/10.1038/s41592-022-01663-4.

Version History

Introduced in R2023b