SAM + ViTMatte Pipeline — Full Quality
From SAM coarse mask to compositing-grade alpha in one node
- sam_model
- image
- bbox
- existing_mask
- trimap
- refined_mask
- coarse_mask
- edge_mask
- preview
- detected_bbox
- score
- info
A SAM mask is a selection. A matte is a fractional coverage map - the kind of alpha that survives compositing a flyaway strand of hair without a hard white fringe. Those are different problems, and the gap between them is where "good enough for a mask" dies on a green-screen comp. SAM + ViTMatte Pipeline (MEC) is the pack's end-to-end answer: SAM segments the object, iterative refinement tightens it, and a neural matting stage converts the hard mask into soft, edge-accurate alpha. If you've got a single image that needs a genuinely compositing-grade cutout, this is the node the pack built for it.
The four stages
The pipeline is four stages, all inside one node. Stage one: SAM runs from your points_json / bbox_json prompts (or existing_mask if you already have a start) and produces a coarse mask. Stage two: sam_iterations (default 2, 2–3 ideal per the tooltip) re-runs SAM with mask-derived prompts to tighten boundaries. Stage three is the matting itself - refine_method picks the backend: vitmatte (HuggingFace's neural matting, the best edge quality; needs transformers), multi_scale_guided (guided filter at three scales, best non-neural), color_aware (LAB-space), laplacian_blend, or guided_filter. auto picks the best available in that order. Stage four is cleanup: fill_holes_enabled, min_region_size (default 64) for specks, and edge_contrast (default 1.0) to sharpen boundaries under harsh lighting.
The subject presets
The genuinely thoughtful part is subject_type. Set it to custom (default) and all the widgets are honored verbatim. Set it to hair, fur, cloth, skin_face, hard_edge, or soft_glow, and it auto-tunes the trimap width and matting parameters to the subject's boundary character - hair wants a wide trimap and high detail, hard_edge (vehicles, props) wants a minimal trimap and a near-binary feel. That's the sort of preset that saves you ten minutes of knob-fiddling on every portrait.
The knobs with real teeth
The knobs with real teeth: edge_radius (default 12) is how many pixels around edges get refined - larger, softer transitions; detail_preservation (0.85) balances keeping fine detail (hair, lace) against smoothing. trimap_dilate / trimap_erode (0 = auto from edge_radius) and an optional trimap MASK let you override the auto-generated trimap for manual control. batch_mode processes every frame in the batch (off = first frame only - and for video, the SeC + MatAnyone2 pipeline is the better tool anyway).
Outputs
Outputs are rich: refined_mask (the final alpha), coarse_mask (before matting - great for before/after), edge_mask (where matting actually changed the boundary), a preview (input + alpha overlay side by side), detected_bbox, score, and info (JSON of stages, params, timings).
Install
Install: clone Code2Collapse/ComfyUI-CustomNodePacks into custom_nodes (or Manager → "CustomNodePacks"), the SAM 2.1/3 weights plus pip install git+https://github.com/facebookresearch/sam2.git, and - for the flagship vitmatte backend - pip install transformers pillow, with the model (~400 MB) auto-downloading from HuggingFace on first use. The community's standing warning applies: ViTMatte is the quality ceiling but the edge-quality gains are about the boundary, not the interior - feed it a bad coarse mask and it will politely matte your mistake.
Inputs (21)
| Name | Type | Default | Description |
|---|---|---|---|
| sam_model | SAM_MODEL | Loaded SAM model from SAM Model Loader | |
| image | IMAGE | Input image / batch to segment + matte | |
| subject_type | COMBO | custom | Auto-tune trimap & matting params based on subject boundary character. custom : honor manual widgets verbatim (default). hair : SAM → ViTMatte, wide trimap, high detail (portraits). fur : SAM → ViTMatte, very wide trimap (animals). cloth : tighter trimap, structural edges preserved. skin_face : multi-scale guided, soft skin boundary. hard_edge : minimal trimap, binary feel (vehicles, props). soft_glow : laplacian blend, very wide soft band. |
| points_json | STRING | [] | JSON array: [{"x":100,"y":200,"label":1}, ...] |
| bbox_json | STRING | Bounding box: [x1,y1,x2,y2]. Leave empty for points-only. | |
| sam_iterations | INT | 21–5 | Number of SAM refinement iterations. Each pass uses the previous mask to generate better prompts. 2-3 is ideal. |
| refine_method | COMBO | auto | Edge refinement backend. auto: best available (vitmatte → multi_scale_guided → guided_filter) vitmatte: HuggingFace ViTMatte neural matting guided_filter: fast image-guided alpha multi_scale_guided: guided filter at 3 scales (best non-neural) color_aware: LAB-space color-sensitive edge refinement laplacian_blend: Laplacian pyramid frequency blending |
| edge_radius | INT | 121–200 | Pixels around edges to refine (larger = softer transitions) |
| detail_preservation | FLOAT | 0.850–1 | How much fine detail (hair, fur, lace) to preserve. 0=smooth, 1=maximum detail. |
| edge_contrast | FLOAT | 1.00–3 | Boost edge contrast for challenging lighting. >1 sharpens boundaries. |
| fill_holes_enabled | BOOLEAN | true | Fill interior holes in the mask |
| min_region_size | INT | 640–10000 | Remove isolated mask regions smaller than N pixels (0=disabled) |
| multimask_output | BOOLEAN | true | Return 3 candidate masks from SAM (vs single best) |
| mask_index | INT | 00–2 | Which SAM candidate mask to keep when multimask_output is True |
| score_threshold | FLOAT | 0.000–1 | Discard SAM masks below this confidence score |
| bboxopt | BBOX | Bounding box from BBox node (overrides bbox_json) | |
| existing_maskopt | MASK | Use as initial mask instead of SAM first pass | |
| trimapopt | MASK | Custom trimap for ViTMatte (overrides auto-generated) | |
| trimap_dilateopt | INT | 00–200 | Outer trimap radius (0 = use edge_radius * 1.5). |
| trimap_erodeopt | INT | 00–200 | Inner trimap erosion radius (0 = use edge_radius * 1.0). |
| batch_modeopt | BOOLEAN | false | Process every frame in the input batch (off = first frame only). |
Outputs (7)
| Name | Type | Description |
|---|---|---|
| refined_mask | MASK | Final compositing-grade alpha matte after SAM + matting refinement. |
| coarse_mask | MASK | SAM coarse mask before edge refinement and cleanup. |
| edge_mask | MASK | Edge-band mask highlighting where matting changed the boundary. |
| preview | IMAGE | Side-by-side preview of input image and refined mask overlay. |
| detected_bbox | BBOX | Bounding box derived from the refined mask. |
| score | FLOAT | Best SAM confidence score from iterative refinement. |
| info | STRING | JSON summary of stages, parameters, and timings. |