Nodes/comfyui-timesaver/TS Matting (ViTMatte)
ComfyUI Node

TS Matting (ViTMatte)

Guided matting via Hugging Face ViTMatte. Builds an auto-trimap from a coarse binary MASK (erode for confident foreground, dilate for the unknown band), then refines the alpha with hair / fur / semi-transparency. Drop-in for TS Remove Background when you already have a SAM3 mask.

By AlexYez·Created 2 years ago·Updated 3 days ago· 11
TS Matting (ViTMatte)
  • image
  • mask
  • background_color
  • IMAGE
  • MASK
  • MASK_IMAGE
modelvitmatte-base-composition-1k
trimap_erode_px10
trimap_dilate_px20
max_resolution2048
auto_crop_by_masktrue
crop_padding_pct15
precisionauto
temporal_smoothmedian3
ema_alpha0.50
mask_blur0
mask_offset0
invert_outputfalse
backgroundAlpha
CategoryTS/Image/Cutout

Inputs (16)

NameTypeDefaultDescription
imageIMAGESource image batch [B, H, W, 3].
maskMASKCoarse binary or soft MASK [B, H, W] (e.g. from SAM3 Detect or SAM3 Video Track). A single-frame mask is broadcast across the image batch.
modelCOMBOvitmatte-base-composition-1kViTMatte variant. 'base' = ~370 MB (recommended for best edge quality), 'small' = ~96 MB (faster, lower detail). 'composition-1k' is the standard matting benchmark; 'distinctions-646' is more diverse.
trimap_erode_pxINT100–128Erosion radius for the confident foreground core. Higher = thinner certain-foreground, more soft edges (better for hair).
trimap_dilate_pxINT200–128Dilation radius for the unknown band beyond the mask. Higher = ViTMatte gets more context around the object (helps fly-away hair / fur). 20 is a good default; bump to 32+ if the source mask is tight around hair/fur.
max_resolutionoptINT20480–4096Long-edge cap (px) for ViTMatte inference. ViTMatte uses global self-attention in some ViT blocks; on 4K input that single attention matrix needs ~25 GB of VRAM. The frame is downscaled before inference and the resulting alpha is upscaled back to native size. 0 = use the native resolution (only safe for small images / lots of VRAM).
auto_crop_by_maskoptBOOLEANtrueCrop the frame to the mask's bounding box (plus padding) before running ViTMatte. Saves enormous amounts of VRAM and time when the object is small compared to the frame. Has no effect when the mask covers the whole frame.
crop_padding_pctoptINT150–100Padding around the mask bbox in percent of bbox size when ``auto_crop_by_mask`` is on. 15 = 15% context margin.
precisionoptCOMBOautoInference precision on CUDA. 'auto' picks bf16 on Ampere+ (more numerically robust, no fp16 inf/nan on extreme alpha) and fp16 elsewhere. Force 'fp32' for diagnostics. CPU always runs fp32.
temporal_smoothoptCOMBOmedian3Smooth alpha across frames to reduce 'boiling' edges in video. 'median3' (default) kills random 1-frame flicker with minimal overhead and is a no-op for single images. 'median5' for stronger flicker (2-frame lag at clip boundaries). 'ema_causal' is causal exponential averaging — no lag, but can blur fast motion. 'off' disables. Peak RAM grows by ~N*H*W*4 bytes during the pass.
ema_alphaoptFLOAT0.500–0.99Strength of the causal EMA when temporal_smooth = 'ema_causal'. Higher = more smoothing (lag for moving objects); lower = closer to raw per-frame alpha.
mask_bluroptINT00–64Final Gaussian blur on the alpha output (px).
mask_offsetoptINT0-20–20Shrink (-) / expand (+) the final alpha mask.
invert_outputoptBOOLEANfalseInvert the alpha output.
backgroundoptCOMBOAlphaOutput background mode.
background_coloroptCOLOR#ffffffSolid background colour when 'background' = 'Color'.

Outputs (3)

NameTypeDescription
IMAGEIMAGECutout image (RGBA or composited on the chosen background).
MASKMASKRefined alpha matte.
MASK_IMAGEIMAGEAlpha matte as a grayscale image for preview.