Skip to content
Back to skills

Cell Detection

ASecurity

Cell segmentation in fluorescence microscopy images. Supports Cellpose/cpsam (Cellpose 4.0) with additional backends

  • 17 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 4, 2026
developmentpythongobashrailsgitapibackend

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned September 4, 2026

npx -y skills add gabrielmoreira/agent-skills-mirror --skill cell-detection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cell Detection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cell Detection
[![Security: A β€” Skills Directory](https://www.skillsdirectory.com/api/skills/gabrielmoreira-cell-detection-agent-skills-mirror/badge)](https://www.skillsdirectory.com/skills/gabrielmoreira-cell-detection-agent-skills-mirror)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cell-detection
description: 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.
license: MIT
metadata:
  version: 0.1.0
  author: ClawBio
  tags:
  - microscopy
  - segmentation
  - cellpose
  - fluorescence
  - imaging
  - cell-biology
  openclaw:
    requires:
      bins:
      - python3
    always: false
    emoji: πŸ”¬
    homepage: https://github.com/ClawBio/ClawBio
    os:
    - darwin
    - linux
    install:
    - kind: pip
      package: cellpose>=4.0
    - kind: pip
      package: tifffile
    - kind: pip
      package: czifile>=2019.7.2.2
    - kind: pip
      package: nd2>=0.11.1
    - kind: pip
      package: Pillow
    - kind: pip
      package: scikit-image
    trigger_keywords:
    - cellpose
    - cpsam
    - cell segmentation
    - nucleus segmentation
    - fluorescence microscopy
    - microscopy image
    - image segmentation
    - cell counting
    - segmentation mask
---

# πŸ”¬ 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

1. **Segment**: Run `cpsam` on TIFF, CZI, ND2, PNG, or JPG fluorescence images
2. **Measure**: Extract area, equivalent diameter, centroid, and eccentricity per cell
3. **Report**: Produce `report.md`, `{stem}_measurements.csv`, and histogram figures
4. **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

1. **Load** image; detect greyscale vs multi-channel
2. **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`
3. **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
4. **Metrics** via `skimage.measure.regionprops`
5. **Figures** β€” overlay + size distribution histogram
6. **Report** β€” `report.md` + `{stem}_measurements.csv` + reproducibility bundle (`commands.sh`, `environment.yml`, `checksums.sha256`)

## CLI Reference

```bash
# 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

```bash
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

1. Load image with `tifffile` (TIFF), `czifile` (CZI), `nd2` (ND2), or `PIL` (PNG/JPG); use CZI/ND2 metadata axes to assign C/Z/Y/X
2. 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`
3. Instantiate `CellposeModel(gpu=<flag>)`
4. 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`
5. Extract per-cell stats from `masks` via `skimage.measure.regionprops`
6. 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

- [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

Files in this skill

  • SKILL.md8.3 KB
  • cell_detection.py27.1 KB
  • requirements.txt106 B
  • tests/test_cell_detection.py37 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…