Image Segmentation with CellPose-SAM#

Since Version 4 CellPose uses a variaton of the Segment-Anything-Model.

See also

As usual, we start with loading an example image.

import cellpose
Welcome to CellposeSAM, cellpose v
cellpose version: 	4.0.3 
platform:       	win32 
python version: 	3.11.11 
torch version:  	2.6.0! The neural network component of
CPSAM is much larger than in previous versions and CPU excution is slow. 
We encourage users to use GPU/MPS if available. 
from cellpose import models
import stackview
import numpy as np
from skimage.data import human_mitosis
from skimage.io import imread
image = human_mitosis()
stackview.insight(image)
shape(512, 512)
dtypeuint8
size256.0 kB
min7
max255

Loading a pretrained model#

CellPose-SAM comes with only a single model that generalizes for multiple images and channel variations.

model = models.CellposeModel(gpu=True)

We let the model “evaluate” the image to produce masks of segmented nuclei.

masks, flows, styles = model.eval(image, 
                                  batch_size=32, 
                                  flow_threshold=0.4, 
                                  cellprob_threshold=0.0,
                                  normalize={"tile_norm_blocksize": 0})

We convert the label image to integer type because many downstream libraries expect this.

masks = masks.astype(np.uint32)
stackview.insight(masks)
shape(512, 512)
dtypeuint32
size1024.0 kB
min0
max329
n labels329

Exercise#

Load ../../data/blobs.tif and apply Cellpose-SAM to it.

Load ../../data/membrane2d.tif and apply Cellpose-SAM to it.