back home next
Ex 4.4: Semi-automatic segmentation using SAM • Ex 4.6: Area of signal per slide

4. Tissue segmentation and classification¶

4.5 Segmentation quality control¶

So far, we tried different ways to segment tissues. But saying that a result is "good" or "bad" is subjective unless we define objective measurements.

In this exercise, we focus on segmentation quality control by comparing reference annotations (ground truth) and predictions.

4.5.1 Segmentation metrics (reference vs prediction)¶

Goals:¶

  • The goal of this exercise is to evaluate one segmentation workflow among Ex 4.1, Ex 4.2, Ex 4.3 and Ex 4.4.
  • To do so, you will need to:
    • Build an evaluation set with GT annotations (GT_*) and predictions (Pred_*) from images you already segmented.
    • Create Inter_Target (GT ∩ Pred) and Union_Target (GT ∪ Pred) using a Groovy script.
    • Compute IoU, Dice, Precision and Recall automatically for each image.
    • Export one image-level TSV file for comparison between methods.

Required data:¶

Folder Description Location License
Same dataset as your chosen workflow Images already segmented in Ex 4.1 / 4.2 / 4.3 / 4.4, with annotation files readable by unpack-annotations.groovy Same source as Ex 4.1 / Ex 4.2 / Ex 4.3 / Ex 4.4 Same license as the selected dataset

Note

Workshop mode: you can use the images you just segmented in the previous exercise (no need to redo segmentation).

For fair method comparison, keep the exact same images and GT annotations for all methods.

A. Prepare the project for evaluation in QuPath¶

  • Start from a copy of the project you want to evaluate (Ex 4.1, Ex 4.2, Ex 4.3 or Ex 4.4).
  • For a quick workshop flow, keep the images you just segmented in the previous exercise.
  • Save your project (Kenney-Ctrl+Kenney-S).
  • Keep/create one prediction class for your chosen target: Pred_Target.
  • Open the top-bar menu "Automate" > "Show script editor".
  • In the script editor, open scripts/tissue-segmentation-classification/unpack-annotations.groovy.
  • Click "Run" for one image to verify that GT annotations are imported correctly.
  • Use QP more option "more options" next to "Run" and choose "Run for project" to import GT on all selected images.
  • Keep the imported GT class name (for example Glomerulus) and reuse this exact name as gt_class in steps B and C.
  • If one structure is split across several polygons, merge these polygons before continuing.
  • Save your project again.

image

In [ ]:
 

Warning

For a quick practical session, you can evaluate on the same images used during setup/tuning. Just keep in mind that the reported metrics will usually be optimistic.

unpack-annotations.groovy requires the expected JSON and what.txt files in the dataset structure.

B. Create Inter_Target and Union_Target¶

a. For one image¶

  • Open scripts/tissue-segmentation-classification/build-intersection-union.groovy in the script editor.
  • Edit only the four class names at the top:
def gt_class = "Glomerulus"
def pred_class = "Pred_Target"
def inter_class = "Inter_Target"
def union_class = "Union_Target"
  • Click "Run" for the current image.
  • Check in the annotations list that Inter_Target and Union_Target were created.

b. For the whole project¶

  • Use QP more option "more options" next to "Run" and choose "Run for project".
  • Save your project at the end.

image

Tip

If you frequently reuse these scripts, place them in your QuPath user scripts directory ("Automate" > "User scripts..." > "Open script directory") so they are directly accessible from the menu.

C. Compute segmentation metrics automatically¶

a. For one image¶

  • Open scripts/tissue-segmentation-classification/compute-segmentation-metrics.groovy.
  • Edit class names with the same target as step B:
def gt_class = "Glomerulus"
def pred_class = "Pred_Target"
def inter_class = "Inter_Target"
def union_class = "Union_Target"
def measurement_prefix = "SegQC "
  • Click "Run" for the current image.
  • The script writes image-level measurements: SegQC A_GT, SegQC A_Pred, SegQC A_Inter, SegQC A_Union, SegQC IoU, SegQC Dice, SegQC Precision, SegQC Recall.

image

b. For the whole project¶

  • Use QP more option "more options" next to "Run" and choose "Run for project".
  • Save your project at the end.

D. Export your measurements¶

  • Save your project before exporting.
  • In the top-bar menu, go to "Measure" > "Export measurements...".
  • Transfer all selected images to the right column.
  • Choose a path for your TSV file (for example segmentation-metrics.tsv).
  • Set "Export type" to Image because metrics were stored as image-level measurements.
  • In your spread sheet software, keep columns starting with SegQC for method comparison.

image

E. Which metric to use, step by step¶

No single metric is sufficient in all situations, so use a combination.

Practical decision workflow¶

  1. Rank candidate workflows by median IoU on the same selected images.
  2. Reject methods with unacceptable Precision if false positives are costly.
  3. Reject methods with unacceptable Recall if missed tissue is costly.
  4. Use median Dice to break ties (especially for small or fragmented targets).
  5. Report all four metrics (IoU, Dice, Precision, Recall) in your final comparison.

Typical priorities by use case¶

Use case Priority metrics
Balanced segmentation benchmark IoU + Dice
Conservative segmentation (avoid false positives) Precision + IoU
Sensitive segmentation (avoid missed tissue) Recall + Dice
Small objects / fragmented masks Dice + Recall

Note

In this chapter, we only cover segmentation quality control. Classification quality control is covered in the object classification chapter.