Nodes/ComfyUI-CustomNodePacks/SAM + ViTMatte Pipeline — Full Quality
ComfyUI Node

SAM + ViTMatte Pipeline — Full Quality

From SAM coarse mask to compositing-grade alpha in one node

By Code2Collapse·Created 6 months ago·Updated a day ago· 52
SAM + ViTMatte Pipeline — Full Quality
  • sam_model
  • image
  • bbox
  • existing_mask
  • trimap
  • refined_mask
  • coarse_mask
  • edge_mask
  • preview
  • detected_bbox
  • score
  • info
subject_typecustom
points_json[]
bbox_json
sam_iterations2
refine_methodauto
edge_radius12
detail_preservation0.85
edge_contrast1.0
fill_holes_enabledtrue
min_region_size64
multimask_outputtrue
mask_index0
score_threshold0.00
trimap_dilate0
trimap_erode0
batch_modefalse

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.

CategoryC2C/Pipeline

Inputs (21)

NameTypeDefaultDescription
sam_modelSAM_MODELLoaded SAM model from SAM Model Loader
imageIMAGEInput image / batch to segment + matte
subject_typeCOMBOcustomAuto-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_jsonSTRING[]JSON array: [{"x":100,"y":200,"label":1}, ...]
bbox_jsonSTRINGBounding box: [x1,y1,x2,y2]. Leave empty for points-only.
sam_iterationsINT21–5Number of SAM refinement iterations. Each pass uses the previous mask to generate better prompts. 2-3 is ideal.
refine_methodCOMBOautoEdge 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_radiusINT121–200Pixels around edges to refine (larger = softer transitions)
detail_preservationFLOAT0.850–1How much fine detail (hair, fur, lace) to preserve. 0=smooth, 1=maximum detail.
edge_contrastFLOAT1.00–3Boost edge contrast for challenging lighting. >1 sharpens boundaries.
fill_holes_enabledBOOLEANtrueFill interior holes in the mask
min_region_sizeINT640–10000Remove isolated mask regions smaller than N pixels (0=disabled)
multimask_outputBOOLEANtrueReturn 3 candidate masks from SAM (vs single best)
mask_indexINT00–2Which SAM candidate mask to keep when multimask_output is True
score_thresholdFLOAT0.000–1Discard SAM masks below this confidence score
bboxoptBBOXBounding box from BBox node (overrides bbox_json)
existing_maskoptMASKUse as initial mask instead of SAM first pass
trimapoptMASKCustom trimap for ViTMatte (overrides auto-generated)
trimap_dilateoptINT00–200Outer trimap radius (0 = use edge_radius * 1.5).
trimap_erodeoptINT00–200Inner trimap erosion radius (0 = use edge_radius * 1.0).
batch_modeoptBOOLEANfalseProcess every frame in the input batch (off = first frame only).

Outputs (7)

NameTypeDescription
refined_maskMASKFinal compositing-grade alpha matte after SAM + matting refinement.
coarse_maskMASKSAM coarse mask before edge refinement and cleanup.
edge_maskMASKEdge-band mask highlighting where matting changed the boundary.
previewIMAGESide-by-side preview of input image and refined mask overlay.
detected_bboxBBOXBounding box derived from the refined mask.
scoreFLOATBest SAM confidence score from iterative refinement.
infoSTRINGJSON summary of stages, parameters, and timings.