|
1 | 1 | # Segmentation Methods |
2 | 2 |
|
3 | | -<!-- TODO: Add comprehensive segmentation guide --> |
| 3 | +SPIMquant provides two built-in segmentation methods for detecting pathology signals in SPIM data, plus support for intensity correction pre-processing. The method and correction are configured independently per stain via the `snakebids.yml` config file. |
4 | 4 |
|
5 | | -Learn about different segmentation methods in SPIMquant. |
| 5 | +## Overview |
6 | 6 |
|
7 | | -## Available Methods |
| 7 | +```mermaid |
| 8 | +flowchart LR |
| 9 | + A[Raw SPIM] --> B[Intensity Correction] |
| 10 | + B --> C{seg_method} |
| 11 | + C -->|threshold| D[Threshold] |
| 12 | + C -->|otsu+k3i2| E[Multi-Otsu + k-means] |
| 13 | + D --> F[Binary Mask] |
| 14 | + E --> F |
| 15 | + F --> G[Clean: remove edge / small objects] |
| 16 | + G --> H[Segmentation Output] |
| 17 | +``` |
8 | 18 |
|
9 | | -### Threshold |
| 19 | +After segmentation the binary mask is used to compute [field fraction](../reference/outputs.md#field-fraction-map-seg), [object count](../reference/outputs.md#object-count-map-seg), and [per-object region properties](../reference/outputs.md#region-properties-statistics-table-tabular). |
10 | 20 |
|
11 | | -Simple intensity thresholding. |
| 21 | +--- |
12 | 22 |
|
13 | | -<!-- TODO: Add threshold method details --> |
| 23 | +## Intensity Correction |
14 | 24 |
|
15 | | -### Otsu + K-means (otsu+k3i2) |
| 25 | +Bias-field correction is applied before segmentation to compensate for the spatially varying illumination typical of lightsheet microscopy. |
16 | 26 |
|
17 | | -Otsu thresholding combined with k-means clustering. |
| 27 | +### Gaussian (`correction_method: gaussian`) |
18 | 28 |
|
19 | | -<!-- TODO: Add otsu+k3i2 method details --> |
| 29 | +A Gaussian blur is applied to the raw image at a coarse downsampled level to estimate the low-frequency illumination profile, which is then divided out. This is fast and requires no extra memory, making it suitable for quick iteration or large datasets. |
20 | 30 |
|
21 | | -## Intensity Correction |
| 31 | +**Config key:** `correction_method: gaussian` |
| 32 | + |
| 33 | +### N4 (`correction_method: n4`) |
| 34 | + |
| 35 | +ANTs N4BiasFieldCorrection is applied. N4 fits a smooth B-spline model of the bias field and is more accurate than Gaussian correction, especially for thicker sections or strong illumination gradients. |
| 36 | + |
| 37 | +**Config key:** `correction_method: n4` |
| 38 | + |
| 39 | +!!! tip |
| 40 | + Use `n4` when high quantitative accuracy is required. Use `gaussian` for faster exploratory runs or when the illumination gradient is mild. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Segmentation Methods |
| 45 | + |
| 46 | +### Threshold (`seg_method: threshold`) |
| 47 | + |
| 48 | +A fixed intensity threshold is applied to the (corrected) image. Voxels above the threshold are classified as positive; all others are negative. |
| 49 | + |
| 50 | +The threshold value is configured per-stain: |
| 51 | + |
| 52 | +```yaml |
| 53 | +stain_defaults: |
| 54 | + abeta: |
| 55 | + seg_method: threshold |
| 56 | + seg_threshold: 500 # intensity value; adjust to your data |
| 57 | +``` |
| 58 | +
|
| 59 | +**When to use:** Works well when the pathology signal is clearly brighter than background and the intensity scale is stable across subjects. Simple to interpret and debug. |
| 60 | +
|
| 61 | +**Limitations:** Sensitive to residual intensity non-uniformities and to between-subject intensity variation. May require manual threshold tuning per dataset. |
| 62 | +
|
| 63 | +### Multi-Otsu (`seg_method: otsu+k3i2`) |
| 64 | + |
| 65 | +An unsupervised thresholding method that adapts to the intensity distribution of each image. |
| 66 | + |
| 67 | +The method string encodes two parameters: `k` — the number of Otsu classes, and `i` — the threshold index to use for the binary classification. For `otsu+k3i2`: |
| 68 | + |
| 69 | +1. **Multi-Otsu thresholding** — Otsu's method is extended to find `k-1` thresholds that minimise within-class variance, splitting the histogram into `k` classes. With `k=3` the image is divided into background, low-signal, and high-signal classes (2 thresholds). |
| 70 | +2. **Binary classification** — the threshold at index `i` (1-based) is applied to produce the final binary mask. With `i=2` this selects the second (higher) threshold, classifying only the brightest voxels as positive. |
| 71 | + |
| 72 | +**Config key:** |
| 73 | + |
| 74 | +```yaml |
| 75 | +stain_defaults: |
| 76 | + abeta: |
| 77 | + seg_method: otsu+k3i2 |
| 78 | +``` |
| 79 | + |
| 80 | +**When to use:** Preferred when the staining intensity varies across subjects or imaging sessions, because the threshold adapts automatically to each image. Also more robust to residual illumination gradients. |
| 81 | + |
| 82 | +**Limitations:** Can fail on images with unusual histograms (e.g. very sparse pathology that does not form a distinct peak) or when the background is very noisy. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Post-Segmentation Cleaning |
| 87 | + |
| 88 | +After the initial binary mask is produced, a cleaning step removes two classes of artefact: |
| 89 | + |
| 90 | +1. **Edge objects** — connected components that touch the border of the field of view are removed, as these are typically cut-off tissue or slide artefacts rather than true pathology. |
| 91 | +2. **Small objects** — objects below a minimum volume threshold (configured via `regionprop_filters`) are discarded, eliminating small noise specks. |
| 92 | + |
| 93 | +!!! note "Scale convention" |
| 94 | + The output mask is stored on a **0–100 scale** (not 0–1). This is deliberate: when the mask is spatially downsampled to compute field fraction, the resulting values are directly interpretable as a percentage (0–100 %). |
22 | 95 |
|
23 | | -### Gaussian |
| 96 | +--- |
24 | 97 |
|
25 | | -<!-- TODO: Add Gaussian correction details --> |
| 98 | +## Per-Stain Configuration |
26 | 99 |
|
27 | | -### N4 |
| 100 | +Each stain is configured independently. A typical `snakebids.yml` block looks like: |
28 | 101 |
|
29 | | -<!-- TODO: Add N4 correction details --> |
| 102 | +```yaml |
| 103 | +stain_defaults: |
| 104 | + abeta: |
| 105 | + seg_method: otsu+k3i2 |
| 106 | + correction_method: n4 |
| 107 | + regionprop_filters: |
| 108 | + min_area: 50 # voxels; objects smaller than this are discarded |
| 109 | + Iba1: |
| 110 | + seg_method: threshold |
| 111 | + seg_threshold: 300 |
| 112 | + correction_method: gaussian |
| 113 | +``` |
30 | 114 |
|
31 | | -## Custom Segmentation |
| 115 | +Stains not listed in `stain_defaults` fall back to the top-level `seg_method` and `correction_method` keys. |
32 | 116 |
|
33 | | -<!-- TODO: Add custom segmentation guide --> |
| 117 | +--- |
34 | 118 |
|
35 | | -## Next Steps |
| 119 | +## Further Reading |
36 | 120 |
|
37 | | -- [Imaris Crops](imaris_crops.md) |
38 | | -- [Examples](../examples/workflows.md) |
| 121 | +- [Output Files Reference](../reference/outputs.md) — description of segmentation output files |
| 122 | +- [How SPIMquant Works Under the Hood](../workflow_overview.md#stage-7--segmentation) — pipeline context for segmentation |
| 123 | +- [Imaris Crops](imaris_crops.md) — export patches for visual inspection |
0 commit comments