MedIC: Efficient and Generalizable Training-free In-Context Medical Image Segmentation

MedIC overview teaser figure

Efficient and generalizable training-free in-context medical image segmentation across 22 in-distribution (ID) and out-of-distribution (OOD) datasets spanning 10 modalities using a single annotated reference and a frozen DINOv3 encoder pre-trained exclusively on natural images.

Abstract

Medical image segmentation models face recurring domain shifts that often require costly additional expert annotations and model retraining. Thus, a technique that enables a single annotated reference image to guide medical segmentation across datasets without further training is highly desirable. However, such an approach remains challenging because biomedical structures exhibit substantial intra-class feature heterogeneity and inter-class ambiguity, making typical single-prototype representations and foreground-only matching approaches insufficient. Additionally, sparse reference annotations and variations in imaging protocols further complicate this process. To address these, we introduce MedIC, a training-free framework built on the insight that reliable reference-to-target matching requires preserving diverse intra-class features while contrasting competing foreground and background evidence. Using a DINOv3 encoder pre-trained on natural images, MedIC constructs diverse foreground and background reference prototypes, contrasts their affinities to target image patches, consolidates target image features through self-similarity, and determines segmentation using target-adaptive thresholds and prototypes. Across 22 datasets spanning 10 imaging modalities and 10 segmentation tasks, MedIC achieves the highest mean Dice among evaluated methods in all tested reference annotation types (sparse, partial & full). On 11 in-distribution datasets, partial masks retaining labels for 20% of foreground patches yield 80.52% mean Dice, approaching 80.97% with full masks, while a single fully annotated in-distribution reference achieves 81.93% across 11 unseen, out-of-distribution datasets, all without medical training or adaptation. These results reveal the potential of pre-trained visual representations for efficient and generalizable medical image segmentation.

Method

  • Reference Multi-Prototype Bank: Clusters explicitly labeled foreground and background reference patches into multiple normalized prototypes per class, preserving intra-class heterogeneity without corrupting background representations.
  • Foreground–Background Contrast: Compares target patch affinities against both foreground and background prototype banks via log-sum-exp scoring, canceling shared tissue similarity offsets to sharpen ambiguous boundaries.
  • Graph-Based Feature Aggregation: Performs nonlocal message passing over a symmetrized nearest-neighbor cosine-similarity graph on target patches to consolidate target-image evidence under sparse annotations.
  • Adaptive Label Selection: Optimizes target-specific dual thresholds to form confident target seed prototypes and resolve uncertain candidate regions under distribution shift.
Overall pipeline of MedIC

Overall MedIC pipeline. Given a frozen DINOv3 encoder and one annotated reference image, MedIC builds class-wise reference multi-prototype banks, propagates target patch features on a similarity graph, computes foreground–background contrast, and predicts the target mask via adaptive label selection.

PCA visualization of DINOv3 feature variation within medical foreground regions

Feature variation within medical foreground regions. PCA visualizations of DINOv3 features reveal different patterns within each annotated structure, motivating multiple reference prototypes rather than a single average feature.

Foreground-background contrast at ambiguous medical boundaries

Foreground–background contrast at ambiguous medical boundaries. DINOv3 foreground and background affinity maps (top and middle rows) illustrate how their difference (bottom row) suppresses shared responses (green: ground-truth contours; magenta: similarity-derived contours).

Results

We evaluate MedIC across 22 datasets spanning 10 imaging modalities and 10 segmentation tasks (11 in-distribution datasets and 11 unseen out-of-distribution datasets) averaged over three fixed reference seeds using one reference image per dataset and a frozen DINOv3 ViT-L/16 encoder. Results below report Dice Similarity Coefficient (DSC (%)), where † denotes baselines equipped with the DINOv3 ViT-L/16 visual backbone.

Sparse and Dense Reference In-Context Segmentation

Mean DSC (%) across 11 in-distribution (ID) datasets with one reference image under each annotation type. Best results are in bold and second-best results are underlined.

Method Points Boxes Scribbles Partial Mask Full Mask
Supervised In-context Segmentation
UniverSeg13.6132.365.0821.9139.57
MultiverSeg15.7550.160.067.2863.75
Tyche5.4543.790.919.5955.03
Painter36.7038.1836.9337.7337.32
SegGPT6.7545.880.471.7266.81
SegIC39.8136.0541.8740.9140.50
SINE43.6841.9548.5644.7844.54
DiffewS47.0842.6749.5849.6653.67
SAM-based In-context Segmentation
GF-SAM66.2753.5365.1968.5667.72
GF-SAM†66.1152.9362.8767.5867.00
Per-SAM39.8737.0941.7040.3840.73
ProtoSAM34.4457.4325.8439.5463.44
ICL-SAM20.8643.9411.0730.4555.67
Matcher62.5252.6363.2162.4861.93
Matcher†68.1656.6168.5168.3367.51
MAUP43.4244.5142.8146.4453.20
M2C25.2432.5012.050.4536.42
Training-free In-context Segmentation
INSID362.7054.9852.7258.6969.65
MedIC (Ours) 75.20 65.91 77.16 80.52 80.97

Domain Generalization (DSC %)

One fully annotated ID reference guides segmentation of both ID and unseen OOD target images across four modality families. With zero target-domain training or parameter updates, MedIC achieves 85.49% ID mean DSC, 81.93% OOD mean DSC across the 11 unseen OOD datasets (83.67% harmonic mean DSC), leading on 10 of the 11 OOD datasets.

Method Breast Ultrasound Chest X-ray Polyp Endoscopy Chest CT
IDOOD IDOOD IDOOD IDOOD
SegIC23.6422.2647.9845.9331.8117.6134.5735.70
SINE38.3135.6759.5754.6146.6424.5736.5841.89
DiffewS69.6167.6451.0447.7157.5628.1636.9743.74
GF-SAM64.3356.8083.7669.0166.5245.5361.7756.66
GF-SAM†69.2265.3292.4973.4275.3853.5241.0551.18
Per-SAM28.9928.2357.4451.1238.9816.3842.2243.67
ProtoSAM70.5363.1967.3561.5274.8462.5262.9861.81
ICL-SAM53.7942.1177.9277.0228.0021.5076.9182.39
Matcher52.6345.7293.0679.9163.7733.2574.8071.91
Matcher†64.8661.6593.6182.1580.0557.1073.2280.95
MAUP42.1040.7572.3970.3957.8933.1670.7676.62
M2C29.7218.0574.7266.578.444.4937.9735.19
INSID358.8554.2088.9082.6284.4766.5481.8181.75
MedIC (Ours) 81.1679.30 94.0288.32 84.5072.41 82.2785.93

Sparse Reference Annotations Under Domain Shift

Reference annotation budget under domain shift

Reference annotation budget under domain shift. ID, OOD, and HM Dice scores for (a) points and scribbles, and (b) boxes and partial masks. Partial mask context remains close to full mask performance even on unseen OOD datasets.

Inference Compute Cost

Without a segmentation decoder, MedIC runs at 0.172 s/image (5.82 FPS) on a single NVIDIA A100 (40GB), approximately 2.5×, 4.0×, and 12.7× faster than INSID3, GF-SAM†, and Matcher†, respectively.

Method FLOPs (G) Time (s) FPS
GF-SAM†7,743.50.6901.45
Matcher†7,867.22.1850.46
INSID35,068.60.4292.33
MedIC (Ours) 1,402.4 0.172 5.82

Qualitative Results

Visual comparison against Matcher† and INSID3 across Points, Boxes, Scribbles, Partial Mask, Full Mask, and ID → OOD domain generalization. MedIC adheres closely to anatomical boundaries with substantially reduced spillover into surrounding tissue under both sparse reference annotations and domain shifts.

Qualitative comparison across prompt types and domain generalization

Qualitative comparison with Matcher† and INSID3 under different reference annotations and domain generalization. Blue denotes target ground truth and orange denotes predictions.

Ablation Studies

Effectiveness of Key Components

Domain generalization ablation with one fully annotated ID reference. Removing foreground–background contrast produces the largest harmonic-mean drop (83.67% to 54.52%), highlighting its role in suppressing ambiguous background affinities.

Method Domain Generalization
ID DSC (%) OOD DSC (%) HM DSC (%)
MedIC (Ours) 85.49 81.93 83.67
Component Removal
w/o Multiple Reference Prototypes82.31(−3.18)↓81.78(−0.15)↓82.05(−1.62)↓
w/o Foreground–Background Contrast55.52(−29.97)↓53.56(−28.37)↓54.52(−29.15)↓
w/o Graph-Based Feature Aggregation84.36(−1.13)↓80.77(−1.16)↓82.53(−1.14)↓
w/o Adaptive Label Selection82.18(−3.31)↓76.92(−5.01)↓79.45(−4.22)↓
w/o Multiple Reference Prototypes & Graph Aggregation81.13(−4.36)↓80.99(−0.94)↓81.06(−2.61)↓
Graph-Based Feature Aggregation Alternatives
→ Local Spatial Diffusion84.42(−1.07)↓80.87(−1.06)↓82.60(−1.07)↓
→ Hard Label Clustering83.91(−1.58)↓79.41(−2.52)↓81.60(−2.07)↓
Foreground–Background Contrast Alternatives
→ kNN Reference Matching83.76(−1.73)↓80.60(−1.33)↓82.15(−1.52)↓
→ Adaptive von Mises–Fisher Modeling81.69(−3.80)↓79.21(−2.72)↓80.43(−3.24)↓
→ Optimal Transport Matching83.66(−1.83)↓79.82(−2.11)↓81.70(−1.97)↓

Effect of Reference Annotation Budget

Effect of reference annotation budget across 11 ID datasets

Reference annotation budget. Mean DSC across 11 ID datasets. Retaining even 10% of foreground patches yields 80.32% DSC (vs. 80.97% for full masks).

Prototype Bank Size and DINO Backbone

Effect of Prototype Bank Size (Kmax)

Kmax ID DSC (%) OOD DSC (%) HM DSC (%) Runtime (ms/img)
182.3181.7882.05159.0
284.3381.4382.86168.9
485.4981.9383.67171.8
885.2881.7783.49201.2
1685.2782.1383.67219.8

Effect of DINO Backbone

Model Backbone ID DSC (%) OOD DSC (%) HM DSC (%)
DINOv2ViT-S/1473.6969.1771.36
DINOv2ViT-B/1475.2769.9972.53
DINOv2ViT-L/1474.5670.0972.25
DINOv3ViT-S/1680.6876.5878.57
DINOv3ViT-B/1680.0074.9477.38
DINOv3ViT-L/1685.4981.9383.67

BibTeX

@article{koleilat2026medic,
  title   = {MedIC: Efficient and Generalizable Training-free In-Context Medical Image Segmentation},
  author  = {Koleilat, Taha and Asgariandehkordi, Hojat and Nejati Manzari, Omid and Abdelwahed, Omar and Rivaz, Hassan and Xiao, Yiming},
  year    = {2026}
}