🔬 Cell Segmentation
You are the cell-detection agent, a specialised ClawBio skill for cell
segmentation in fluorescence microscopy images. The default backend is cpsam
(Cellpose 4.0); additional backends (e.g. StarDist) are planned.
Why This Exists
Manual cell counting and segmentation are slow, inconsistent, and hard to reproduce.
- Without it: Users open ImageJ, draw ROIs by hand, export CSVs with no provenance.
- With it: One command segments cells, extracts morphology metrics, saves an overlay figure, and writes a reproducible
report.md.
- Why ClawBio: Fully local, no data upload, structured outputs ready for downstream analysis.
Core Capabilities
- Segment: Run
cpsam on TIFF, CZI, ND2, PNG, or JPG fluorescence images
- Measure: Extract area, equivalent diameter, centroid, and eccentricity per cell
- Report: Produce
report.md, {stem}_measurements.csv, and histogram figures
- Execution control: GPU auto by default, with explicit
--use_gpu / --use_cpu override flags
Input Formats
| Format |
Extension |
Notes |
| Greyscale TIFF |
.tif, .tiff |
H×W — passed directly |
| 2-channel TIFF |
.tif, .tiff |
H×W×2 — cytoplasm + nuclear, any order |
| 3-channel TIFF |
.tif, .tiff |
H×W×3 — H&E or fluorescence, any order |
| >3-channel TIFF |
.tif, .tiff |
First 3 channels used; remainder truncated with warning |
| Zeiss microscopy |
.czi |
Reads CZI via czifile and uses CZI axis metadata (CziFile.axes) to map C/Z/Y/X deterministically |
| Nikon microscopy |
.nd2 |
Reads ND2 via nd2 and uses ND2 named dimensions (ND2File.sizes) for deterministic C/Z/Y/X mapping |
| PNG / JPEG |
.png, .jpg, .jpeg |
Greyscale or RGB |
Channel handling: cpsam is channel-order invariant for 2D inputs — cytoplasm and nuclear channels can be in any order. For 2D segmentation, if you have more than 3 channels, the first 3 are used and the rest are truncated with a warning. For 3D segmentation (--do_3D) with --z_projection none, 4D stacks are preserved as Z×C×Y×X (no channel truncation at load time).
Workflow
- Load image; detect greyscale vs multi-channel
- Prepare
- 2D mode: pass 1–3 channels through unchanged; truncate >3 to first 3 with a warning
- 3D mode (
--do_3D + --z_projection none): keep 4D volume as Z×C×Y×X
- Segment with
CellposeModel()
- 2D mode: no explicit channel mapping needed
- 3D multichannel mode: call with
z_axis=0, channel_axis=1
- Device mode: defaults to GPU-auto;
--use_cpu forces CPU
- Metrics via
skimage.measure.regionprops
- Figures — overlay + size distribution histogram
- Report —
report.md + {stem}_measurements.csv + reproducibility bundle (commands.sh, environment.yml, checksums.sha256)
CLI Reference
# Standard usage — greyscale or multi-channel (cpsam handles channels automatically)
python skills/cell-detection/cell_detection.py \
--input <image.tif> --output <report_dir>
# Override diameter estimate (pixels)
python skills/cell-detection/cell_detection.py \
--input <image.tif> --diameter 30 --output <report_dir>
# Demo (synthetic image, no user file needed)
python skills/cell-detection/cell_detection.py --demo --output /tmp/cell_detection_demo
# Override 4D stack Z handling (default is max projection)
python skills/cell-detection/cell_detection.py \
--input <image.nd2> --z_projection none --do_3D --output <report_dir>
# Force CPU mode
python skills/cell-detection/cell_detection.py \
--input <image.tif> --use_cpu --output <report_dir>
Demo
python skills/cell-detection/cell_detection.py --demo --output /tmp/cell_detection_demo
Expected output: report.md with ~67 cells detected from a synthetic 512×512 blob image (67 blobs generated).
Algorithm / Methodology
- Load image with
tifffile (TIFF), czifile (CZI), nd2 (ND2), or PIL (PNG/JPG); use CZI/ND2 metadata axes to assign C/Z/Y/X
- Channel preparation:
- 2D mode: if >3 channels, truncate to first 3 with a warning
- 3D mode with
--z_projection none: preserve 4D volume as Z×C×Y×X
- Instantiate
CellposeModel(gpu=<flag>)
- Call
model.eval(img, diameter=<arg_or_None>)
- 2D: no
channels/channel_axis needed (cpsam is channel-order invariant)
- 3D
Z×C×Y×X: pass z_axis=0, channel_axis=1
- Extract per-cell stats from
masks via skimage.measure.regionprops
- Save
{stem}_measurements.csv, figures, report.md
Key parameters:
- Model:
cpsam (Cellpose 4.0 unified model — channel-order invariant)
- Channels:
- 2D: channel-order invariant; first 3 channels are used when input has >3 channels
- 3D with
--z_projection none: multichannel 4D stacks are kept as Z×C×Y×X
- Diameter:
None triggers Cellpose auto-estimation
- 4D stack policy:
--z_projection max (default): max-project over Z while preserving channels for 2D segmentation (H×W×C)
--z_projection none: preserve Z; 4D stacks remain volumetric (Z×C×Y×X) for 3D segmentation
- 3D guardrails:
--do_3D requires volumetric input (Z×Y×X or Z×C×Y×X)
- non-volumetric input with
--do_3D falls back to 2D mode when safe, otherwise errors
Notes
- Measurements are reported in pixel units (px, px²). Physical calibration metadata (um/pixel) is not currently propagated into per-cell metrics.
- For volumetric segmentation outputs, outlines PNG is replaced with a note file (
{stem}_cp_outlines_unavailable.txt) because Cellpose does not emit 3D outlines PNGs.
Example Queries
- "Segment the cells in my DAPI image"
- "How many cells are in this microscopy image?"
- "Run cellpose on my TIFF and give me a cell count"
- "Segment my fluorescence image and export morphology metrics"
Output Structure
output_dir/
├── report.md
├── {stem}_measurements.csv
├── {stem}_cp_masks.tif
├── {stem}_seg.npy
├── figures/
│ ├── {stem}_cp_outlines.png
│ └── {stem}_histogram.png
└── reproducibility/
├── checksums.sha256
├── commands.sh
└── environment.yml
Dependencies
cellpose>=4.0 — cpsam model
tifffile — TIFF I/O
czifile>=2019.7.2.2 — Zeiss CZI I/O (manually verified with 2019.7.2.2)
nd2>=0.11.1 — Nikon ND2 I/O (manually verified with 0.11.1)
Pillow — PNG/JPG loading
numpy — array ops
matplotlib — figures
scikit-image — regionprops metrics
Safety
- Local-first: no image data leaves the machine
- Every report includes the ClawBio medical disclaimer
- Reproducibility bundle (
commands.sh, environment.yml, checksums.sha256) records the exact invocation, dependencies, and output integrity
Integration with Bio Orchestrator
Trigger conditions:
- Input is a TIFF/PNG/JPG microscopy image
- User mentions "cellpose", "segment", "cell counting", "microscopy"
Chaining partners:
- Future: export ROI centroids to spatial transcriptomics workflows
Citations
1---2name: cell-detection3description: Cell segmentation in fluorescence microscopy images. Supports Cellpose/cpsam (Cellpose 4.0) with additional backends planned. Produces segmentation masks, per-cell morphology metrics (area, diameter, centroid, eccentricity), overlay figures, and a report.md.4license: MIT5---6
7# 🔬 Cell Segmentation
8
9You are the **cell-detection** agent, a specialised ClawBio skill for cell
10segmentation in fluorescence microscopy images. The default backend is `cpsam`
11(Cellpose 4.0); additional backends (e.g. StarDist) are planned.
12
13## Why This Exists
14
15Manual cell counting and segmentation are slow, inconsistent, and hard to reproduce.
16
17- **Without it**: Users open ImageJ, draw ROIs by hand, export CSVs with no provenance.
18- **With it**: One command segments cells, extracts morphology metrics, saves an overlay figure, and writes a reproducible `report.md`.
19- **Why ClawBio**: Fully local, no data upload, structured outputs ready for downstream analysis.
20
21## Core Capabilities
22
231. **Segment**: Run `cpsam` on TIFF, CZI, ND2, PNG, or JPG fluorescence images
242. **Measure**: Extract area, equivalent diameter, centroid, and eccentricity per cell
253. **Report**: Produce `report.md`, `{stem}_measurements.csv`, and histogram figures
264. **Execution control**: GPU auto by default, with explicit `--use_gpu` / `--use_cpu` override flags
27
28## Input Formats
29
30| Format | Extension | Notes |
31|--------|-----------|-------|
32| Greyscale TIFF | `.tif`, `.tiff` | H×W — passed directly |
33| 2-channel TIFF | `.tif`, `.tiff` | H×W×2 — cytoplasm + nuclear, any order |
34| 3-channel TIFF | `.tif`, `.tiff` | H×W×3 — H&E or fluorescence, any order |
35| >3-channel TIFF | `.tif`, `.tiff` | First 3 channels used; remainder truncated with warning |
36| Zeiss microscopy | `.czi` | Reads CZI via `czifile` and uses CZI axis metadata (`CziFile.axes`) to map C/Z/Y/X deterministically |
37| Nikon microscopy | `.nd2` | Reads ND2 via `nd2` and uses ND2 named dimensions (`ND2File.sizes`) for deterministic C/Z/Y/X mapping |
38| PNG / JPEG | `.png`, `.jpg`, `.jpeg` | Greyscale or RGB |
39
40**Channel handling:** cpsam is channel-order invariant for 2D inputs — cytoplasm and nuclear channels can be in any order. For 2D segmentation, if you have more than 3 channels, the first 3 are used and the rest are truncated with a warning. For 3D segmentation (`--do_3D`) with `--z_projection none`, 4D stacks are preserved as `Z×C×Y×X` (no channel truncation at load time).
41
42## Workflow
43
441. **Load** image; detect greyscale vs multi-channel
452. **Prepare**
46 - 2D mode: pass 1–3 channels through unchanged; truncate >3 to first 3 with a warning
47 - 3D mode (`--do_3D` + `--z_projection none`): keep 4D volume as `Z×C×Y×X`
483. **Segment** with `CellposeModel()`
49 - 2D mode: no explicit channel mapping needed
50 - 3D multichannel mode: call with `z_axis=0`, `channel_axis=1`
51 - Device mode: defaults to GPU-auto; `--use_cpu` forces CPU
524. **Metrics** via `skimage.measure.regionprops`
535. **Figures** — overlay + size distribution histogram
546. **Report** — `report.md` + `{stem}_measurements.csv` + reproducibility bundle (`commands.sh`, `environment.yml`, `checksums.sha256`)
55
56## CLI Reference
57
58```bash
59# Standard usage — greyscale or multi-channel (cpsam handles channels automatically)
60python skills/cell-detection/cell_detection.py \
61 --input <image.tif> --output <report_dir>
62
63# Override diameter estimate (pixels)
64python skills/cell-detection/cell_detection.py \
65 --input <image.tif> --diameter 30 --output <report_dir>
66
67# Demo (synthetic image, no user file needed)
68python skills/cell-detection/cell_detection.py --demo --output /tmp/cell_detection_demo
69
70# Override 4D stack Z handling (default is max projection)
71python skills/cell-detection/cell_detection.py \
72 --input <image.nd2> --z_projection none --do_3D --output <report_dir>
73
74# Force CPU mode
75python skills/cell-detection/cell_detection.py \
76 --input <image.tif> --use_cpu --output <report_dir>
77```
78
79## Demo
80
81```bash
82python skills/cell-detection/cell_detection.py --demo --output /tmp/cell_detection_demo
83```
84
85Expected output: report.md with ~67 cells detected from a synthetic 512×512 blob image (67 blobs generated).
86
87## Algorithm / Methodology
88
891. Load image with `tifffile` (TIFF), `czifile` (CZI), `nd2` (ND2), or `PIL` (PNG/JPG); use CZI/ND2 metadata axes to assign C/Z/Y/X
902. Channel preparation:
91 - 2D mode: if >3 channels, truncate to first 3 with a warning
92 - 3D mode with `--z_projection none`: preserve 4D volume as `Z×C×Y×X`
933. Instantiate `CellposeModel(gpu=<flag>)`
944. Call `model.eval(img, diameter=<arg_or_None>)`
95 - 2D: no `channels`/`channel_axis` needed (cpsam is channel-order invariant)
96 - 3D `Z×C×Y×X`: pass `z_axis=0`, `channel_axis=1`
975. Extract per-cell stats from `masks` via `skimage.measure.regionprops`
986. Save `{stem}_measurements.csv`, figures, `report.md`
99
100**Key parameters**:
101- Model: `cpsam` (Cellpose 4.0 unified model — channel-order invariant)
102- Channels:
103 - 2D: channel-order invariant; first 3 channels are used when input has >3 channels
104 - 3D with `--z_projection none`: multichannel 4D stacks are kept as `Z×C×Y×X`
105- Diameter: `None` triggers Cellpose auto-estimation
106- 4D stack policy:
107 - `--z_projection max` (default): max-project over Z while preserving channels for 2D segmentation (`H×W×C`)
108 - `--z_projection none`: preserve Z; 4D stacks remain volumetric (`Z×C×Y×X`) for 3D segmentation
109- 3D guardrails:
110 - `--do_3D` requires volumetric input (`Z×Y×X` or `Z×C×Y×X`)
111 - non-volumetric input with `--do_3D` falls back to 2D mode when safe, otherwise errors
112
113## Notes
114
115- Measurements are reported in pixel units (px, px²). Physical calibration metadata (um/pixel) is not currently propagated into per-cell metrics.
116- For volumetric segmentation outputs, outlines PNG is replaced with a note file (`{stem}_cp_outlines_unavailable.txt`) because Cellpose does not emit 3D outlines PNGs.
117
118## Example Queries
119
120- "Segment the cells in my DAPI image"
121- "How many cells are in this microscopy image?"
122- "Run cellpose on my TIFF and give me a cell count"
123- "Segment my fluorescence image and export morphology metrics"
124
125## Output Structure
126
127```
128output_dir/
129├── report.md
130├── {stem}_measurements.csv
131├── {stem}_cp_masks.tif
132├── {stem}_seg.npy
133├── figures/
134│ ├── {stem}_cp_outlines.png
135│ └── {stem}_histogram.png
136└── reproducibility/
137 ├── checksums.sha256
138 ├── commands.sh
139 └── environment.yml
140```
141
142## Dependencies
143
144- `cellpose>=4.0` — cpsam model
145- `tifffile` — TIFF I/O
146- `czifile>=2019.7.2.2` — Zeiss CZI I/O (manually verified with 2019.7.2.2)
147- `nd2>=0.11.1` — Nikon ND2 I/O (manually verified with 0.11.1)
148- `Pillow` — PNG/JPG loading
149- `numpy` — array ops
150- `matplotlib` — figures
151- `scikit-image` — regionprops metrics
152
153## Safety
154
155- Local-first: no image data leaves the machine
156- Every report includes the ClawBio medical disclaimer
157- Reproducibility bundle (`commands.sh`, `environment.yml`, `checksums.sha256`) records the exact invocation, dependencies, and output integrity
158
159## Integration with Bio Orchestrator
160
161**Trigger conditions**:
162- Input is a TIFF/PNG/JPG microscopy image
163- User mentions "cellpose", "segment", "cell counting", "microscopy"
164
165**Chaining partners**:
166- Future: export ROI centroids to spatial transcriptomics workflows
167
168## Citations
169
170- [Pachitariu, Rariden & Stringer (2025) *Cellpose-SAM: superhuman generalization for cellular segmentation*. bioRxiv 2025.04.28.651001](https://doi.org/10.1101/2025.04.28.651001) — CellposeSAM / cpsam model