ComfyUI Node

Mask + Matting

The one-node production pipeline from segmentation to alpha to despill

By Code2Collapse·Created 6 months ago·Updated a day ago· 52
Mask + Matting
  • image
  • pos_bbox
  • neg_bbox
  • normal_bbox
  • external_mask
  • external_trimap
  • holdout_mask
  • core_mask
  • mask
  • alpha
  • preview
  • trimap
  • bbox
  • bbox_json
  • score
  • info
  • despilled
  • lightwrap_rgba
  • edge_mask
  • inside_mask
  • outside_mask
  • luma_key_mask
  • problem_regions
  • severity
  • suggested_method
segmenterauto_best
mattervitmatte
model(auto)
matter_model(auto)
precisionfp16
attentionauto
offloadnone
subject_presetcustom
trimap_dilate8
trimap_erode8
edge_radius4
individual_objectsfalse
tracking_directionforward
frame_annotation0
object_id0
max_frames_to_track0
memory_size8
start_frame0
end_frame-1
auto_downloadfalse
seed0
tta_flipfalse
multiscalefalse
post_refinenone
refine_radius8
refine_iterations5
despilloff
despill_strength1.00
preserve_skintrue
lightwrap_strength0.00
lightwrap_radius8
edge_band_radius4
premultiplytrue
enable_luma_keyfalse
luma_modeauto
luma_low0.00
luma_high1.00
luma_gamma1.00
luma_falloff1.0
luma_invertfalse
luma_mixhint_only
enable_advanced_trimapfalse
trimap_inner_scale1.0
trimap_outer_scale1.5
trimap_smooth0.0
trimap_threshold0.50
auto_qualitytrue
auto_disambiguatetrue
quality_modebalanced
enable_diagnosetrue
diag_ring_width5
diag_blur_threshold50
diag_brightness_threshold0.15
robust_propagationfalse
robust_confidence_threshold0.65
robust_reanchor_methodblend
robust_blend_alpha0.70
positive_coords
negative_coords
text_prompt

This is the flagship. Mask Ops (MEC) is a full production matting pipeline in one node - segmenter, alpha matter, auto-quality pre-processing, despill, light wrap, video propagation, and diagnostics - with 17 outputs and enough knobs to scare anyone. The good news: the defaults are so aggressively automatic that you can ignore most of them and still get a better cutout than a hand-rolled chain.

The pipeline: a coarse mask from a segmenter, then an optional alpha matte, then polish. In this pack's terms it's MaskOpsMEC = the "Mask + Matting" node - the one-stop shop where the separate SAM/ViTMatte/RVM nodes get fused.

The parts that matter

  • segmenter (default auto_best) - the coarse-mask backend: SAM 2.1, SAM 3, SAM 3.1, BiRefNet, RMBG-2.0, InSPyReNet, DIS, GroundingDINO, and more. Entries tagged [missing-deps] need an optional pip install to activate. auto_best picks what's available and best - leave it unless you know why not.
  • matter (default vitmatte) - refines the alpha: none (use the segmenter mask as-is), vitmatte, rvm, bgmattingv2, matanyone, birefnet. This is where hair and fur actually get good.
  • model / matter_model - pick specific weight files, or (auto) and let each backend choose.
  • auto_quality (default ON) - the killer feature. It detects motion blur, low light, low contrast, speckle noise, and similar fg/bg color, then applies just-enough preprocessing (CLAHE, unsharp, NL-means, chroma stretch) before segmentation and a light guided-filter polish after. No knobs. On hard images this is the difference between a decent mask and a good one.
  • auto_disambiguate (default ON) - feed positive_coords (e.g. on the face) and negative_coords (e.g. on the neck) and it scores SAM's three candidates by pos-coverage minus neg-coverage, so you get the face, not the whole person.

Then the pro stuff: despill with preserve_skin (decontaminate green/blue/whatever backing), lightwrap_strength for blending over a new background, holdout_mask (force pixels to alpha 0 - boom mics, rigs), core_mask (force pixels to alpha 1), post_refine (guided or DenseCRF), tta_flip/multiscale for quality, and robust_propagation for video - a confidence-aware re-anchor loop that re-prompts SAM when a frame's mask drifts (flow warp / DINOv2 / blend strategies).

Outputs worth wiring

The full list is long; the ones you'll actually use: alpha (the production matte), mask (coarse), preview (premultiplied), bbox/bbox_json, score (0–1 quality), info (what actually ran), and the compositing aids - despilled, lightwrap_rgba, edge_mask, inside_mask/outside_mask. problem_regions/severity/suggested_method come from the built-in failure explainer.

Install and model files

Pack install, then models:

cd ComfyUI/custom_nodes
git clone https://github.com/Code2Collapse/ComfyUI-CustomNodePacks.git
pip install opencv-python>=4.7.0 scipy>=1.10.0

SAM weights go in ComfyUI/models/sams/ (SAM 3) and ComfyUI/models/sam2/ (SAM 2.1); BiRefNet/RMBG/InSPyReNet weights are auto-listed from their standard folders. auto_download (default off) can lazy-fetch missing weights from HF/torch.hub. The [missing-deps] tags tell you exactly which pip packages unlock which backends - install transformers for ViTMatte, and the extras for the video matters.

Three gotchas: (1) RMBG-2.0 is non-commercial under BRIA's license - check before shipping a product. (2) SAM 3 is not Apache like SAM/SAM2, worth knowing for commercial use. (3) With 50+ inputs, keep auto_quality on and resist touching everything - the node's own docs describe it as "no knobs to tune," and it's right.

CategoryMaskEditControl/Pipeline

Inputs (68)

NameTypeDefaultDescription
imageIMAGESource image or video frames (B,H,W,C).
segmenterCOMBOauto_bestCoarse-mask backend. Entries tagged [missing-deps] need an optional pip install to activate.
matterCOMBOvitmatteOptional alpha refinement. 'none' returns the segmenter mask as alpha.
modelCOMBO(auto)Specific weight file to use. Tag prefix selects the backend folder; '(auto)' lets each backend pick.
matter_modelCOMBO(auto)Weight file for the matter backend.
precisionCOMBOfp163 options: fp16, bf16, fp32
attentionCOMBOauto6 options: auto, sdpa, flash, sage, xformers, eager
offloadCOMBOnone3 options: none, cpu, sequential
subject_presetCOMBOcustomOverride trimap_dilate/erode/edge with subject-tuned values.
trimap_dilateINT80–128
trimap_erodeINT80–128
edge_radiusINT40–64
individual_objectsBOOLEANfalseIf supported by the backend, return one mask per detected object.
tracking_directionCOMBOforward3 options: forward, backward, bidirectional
frame_annotationINT00–100000Frame index (in clip) where prompts are anchored.
object_idINT00–1024
max_frames_to_trackINT00–1000000 = no cap.
memory_sizeINT81–256
start_frameINT00–100000
end_frameINT-1-1–100000-1 = last frame.
auto_downloadBOOLEANfalseAllow lazy auto-download from HF/torch.hub when a weight is missing.
seedINT00–18446744073709550000
tta_flipBOOLEANfalseTest-time augmentation: run segmenter on the H-flipped image and average. Slower but cleaner.
multiscaleBOOLEANfalseRun the segmenter at 0.75x / 1.0x / 1.25x and fuse. Helps small / thin subjects.
post_refineCOMBOnoneFinal alpha refinement. 'guided' = guided filter (fast, torch-only). 'crf' = DenseCRF (requires pydensecrf, sharpest edges).
refine_radiusINT81–64Spatial radius for guided / CRF refinement.
refine_iterationsINT51–30CRF inference iterations.
despillCOMBOoffColour decontamination on the named backing. 'auto' estimates the colour from image corners.
despill_strengthFLOAT1.000–2How aggressively to subtract the spill (0 = off).
preserve_skinBOOLEANtrueKeep warm pixels (R>G>B) untouched during despill.
lightwrap_strengthFLOAT0.000–2Light-wrap intensity. 0 = off; ~0.3-0.6 = natural blend over the new BG.
lightwrap_radiusINT81–64Light-wrap halo radius in pixels.
edge_band_radiusINT41–64Width of the soft edge band when splitting edge/inside/outside masks.
premultiplyBOOLEANtruePremultiply preview by alpha. Disable for straight-alpha outputs.
enable_luma_keyBOOLEANfalseRun a luminance keyer on the source image BEFORE segmentation and use it as a hint / external_mask.
luma_modeCOMBOauto5 options: auto, highlights, midtones, shadows, custom
luma_lowFLOAT0.000–1
luma_highFLOAT1.000–1
luma_gammaFLOAT1.000.01–10
luma_falloffFLOAT1.00–10
luma_invertBOOLEANfalse
luma_mixCOMBOhint_onlyHow to combine the luma-key mask with the segmenter result. 'hint_only' = use as external_mask hint; 'intersect/union/replace' = combine with the final alpha.
enable_advanced_trimapBOOLEANfalseUse the edge-aware trimap generator (asymmetric inner/outer scaling, image-edge snapping, smoothing) instead of the simple dilate/erode trimap.
trimap_inner_scaleFLOAT1.00.1–3
trimap_outer_scaleFLOAT1.50.5–5
trimap_smoothFLOAT0.00–20
trimap_thresholdFLOAT0.500–1
auto_qualityBOOLEANtrueAUTOMATIC robustness for hard images. Detects motion blur, low light, low contrast, speckle noise and low bg/fg colour separation, then applies just-enough pre-processing (CLAHE, unsharp, NL-means, chroma stretch) BEFORE the segmenter, and a light guided-filter+edge-snap polish on the alpha. No knobs.
auto_disambiguateBOOLEANtrueWhen BOTH positive and negative points are supplied, score SAM's 3 candidate masks by (pos-coverage − neg-coverage − size-penalty) instead of raw score. This is what makes `pos=face, neg=neck` return just the face, not the whole person.
quality_modeCOMBObalancedStrength of auto_quality pre/post processing. fast = mild, balanced = default, max_fidelity = NL-means denoise + larger guided filter.
enable_diagnoseBOOLEANtrueRun automatic mask-failure diagnostics (severity score + suggested method).
diag_ring_widthINT51–50
diag_blur_thresholdFLOAT500–1000
diag_brightness_thresholdFLOAT0.150–1
robust_propagationBOOLEANfalseConfidence-aware re-anchor loop for video. After SAM2 propagation, every frame's mask is scored vs the last good mask (IoU × size-ratio). When confidence drops below the threshold, the chosen re-anchor strategy fires (flow warp / DINOv2 region search / convex blend). Robust against motion blur + lighting drift. Single-image inputs skip this stage.
robust_confidence_thresholdFLOAT0.650–1Below this confidence the re-anchor fires.
robust_reanchor_methodCOMBOblendflow=Farneback optical-flow warp of last good mask. dino=DINOv2 patch-feature region search + SAM2 re-prompt. blend=convex mix of current+warped. none=accept drifted output (debug).
robust_blend_alphaFLOAT0.700–1Blend weight for current SAM2 mask when method=blend.
positive_coordsoptSTRINGJSON list of positive points [[x,y],...] from PointsBBoxMaskEditor (positive_coords output).
negative_coordsoptSTRINGJSON list of negative points [[x,y],...] from PointsBBoxMaskEditor (negative_coords output).
pos_bboxoptBBOXSingle positive bbox [x0,y0,x1,y1].
neg_bboxoptBBOXOptional negative bbox (excluded region).
normal_bboxoptBBOXGeneric bbox if you don't care about polarity.
text_promptoptSTRINGOpen-vocabulary text prompt (SAM3 / GroundingDINO / VideoMaMa). Wire from any STRING source.
external_maskoptMASKOptional mask used as a hint or overridden when input_mode='auto' falls through.
external_trimapoptMASKOptional pre-computed trimap that bypasses internal trimap generation.
holdout_maskoptMASKGarbage / holdout matte. Pixels where this is >0 are FORCED to alpha=0 (used to chop out boom mics, rigs, etc).
core_maskoptMASKCore / inside matte. Pixels where this is >0 are FORCED to alpha=1 (used to lock down opaque interiors).

Outputs (17)

NameTypeDescription
maskMASKCoarse mask from the segmenter (B,H,W).
alphaMASKRefined alpha after matter + refinement (production output).
previewIMAGEimage * alpha premultiplied preview.
trimapMASKTrimap (0/0.5/1) used by the matter.
bboxBBOXTight bbox around the alpha as [x0,y0,x1,y1].
bbox_jsonSTRINGSame bbox as JSON {'x','y','w','h'}.
scoreFLOATOverall production-quality score in [0,1] (boundary + coherence + size + smoothness).
infoSTRINGJSON: backends, modes, per-frame quality breakdown, refine stages run, settings used.
despilledIMAGEImage with backing-colour spill suppressed (when `despill_strength`>0).
lightwrap_rgbaIMAGERGBA light-wrap layer to ADD over the new background.
edge_maskMASKSoft edge band where matting actually matters.
inside_maskMASKSolid-fg interior mask (safe to colour-grade).
outside_maskMASKSolid-bg exterior mask (safe to defocus / replace).
luma_key_maskMASKLuminance keyer output (empty if `enable_luma_key` is off).
problem_regionsMASKDiagnostic problem-region heatmap from the failure explainer.
severityFLOATSeverity score [0,1] from the failure explainer.
suggested_methodSTRINGSuggested next masking method (string) from the failure explainer.