File size: 3,514 Bytes
9a713a9
 
 
 
 
 
 
 
 
c843d82
 
9a713a9
 
c843d82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: Cellpose+SAM DRG Segmentation
license: mit
sdk: streamlit
emoji: 🏃
colorFrom: indigo
colorTo: purple
app_file: streamlit_app.py
---
# Spinal Cord Segmentation Pipeline



Automated, end‑to‑end processing and segmentation of spinal‑cord microscopy images with [Cellpose](https://cellpose.readthedocs.io/).

## Overview

This repository provides a **turn‑key workflow** for turning raw histological slides of the spinal cord (TIFF) into high‑quality, full‑resolution segmentation masks—in a *single command*.

## Key Features

| Stage | Purpose |
|-------|---------|
| **TIFF → PNG conversion** | Converts raw `.tiff` slides to compressed `.png`, with optional down‑scaling to speed up processing. |
| **Smart tiling** | Splits very large images into manageable tiles that fit comfortably in GPU/CPU memory. |
| **Cellpose inference** | Runs the *cyto3* (default) or any other Cellpose model on every tile. |
| **Mask stitching** | Re‑assembles the individual tile masks into a single, full‑resolution segmentation mask. |

## Requirements

* Python **3.9+**
* GPU‑enabled PyTorch build (optional but recommended)
* Dependencies (installed automatically via `requirements.txt`):
  * `cellpose==3.1.1.1`
  * `opencv‑python`
  * `numpy`
  * `pillow`
  * `tifffile`

## Installation

```bash
# Clone the repository
git clone https://github.com/your‑username/spinal‑cord‑segmentation.git
cd spinal‑cord‑segmentation

# Create / activate a virtualenv (optional but recommended)
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# Install Python dependencies
pip install -r requirements.txt
```

## Quick Start

1. **Place** your raw `.tiff` images in `data/input/` (or adjust the paths in `bin/constants.py`).
2. **Run** the pipeline:

   ```bash
   python main.py
   ```
3. **Collect** your results:
   * PNG conversions → `data/png/`
   * Split tiles → `data/tiles/`
   * Cellpose masks → `data/masks/`
   * Stitched masks → `data/output/`

## Detailed Workflow

```mermaid
flowchart LR
    A[TIFF images] --> B[generate_pngs.py]:::step
    classDef step fill:#fafafa,stroke:#333,stroke-width:1px;
    B --> C[generate_split_images.py]:::step
    C --> D[run_cellpose.py]:::step
    D --> E[generate_masks.py]:::step
    E --> F[Final segmentation]:::step
```

*All paths, tile overlap, and Cellpose parameters are configurable in* **`bin/constants.py`**.

## Project Layout

```
.
├── main.py              # Orchestrates the full pipeline
├── bin/
│   ├── constants.py     # Centralised paths & tunables
│   ├── generate_pngs.py # TIFF → PNG converter
│   ├── generate_split_images.py
│   └── generate_masks.py
├── model/
│   └── run_cellpose.py  # Wrapper around Cellpose API
├── requirements.txt
└── LICENSE
```

## License

Distributed under the terms of the **MIT License**.  See `LICENSE` for full text.

## Contributing

Contributions, issues and feature requests are welcome!  Please open an issue or submit a pull request — and ensure your code passes `flake8`/`black` checks and includes appropriate tests.

## Citation

If you use this pipeline in your research, please cite *Cellpose* **and** this repository:

```text
@article{stringer_cellpose_2021,
  title   = {Cellpose: a generalist algorithm for cellular segmentation},
  author  = {Stringer, Carsen and Pachitariu, Marius},
  journal = {Nature Methods},
  year    = {2021}
}
```