Nodes/MatAnyone2 Video Matting/MatAnyone2 Merge Masks
ComfyUI Node

MatAnyone2 Merge Masks

When One Cut Isn't Enough

By dreamrec·Created 6 months ago·Updated 6 months ago· 3
MatAnyone2 Merge Masks
  • mask_a
  • mask_b
  • mask_c
  • mask_d
  • mask
threshold0.50

Sometimes the thing you want cut out of a video isn't one SAM-friendly blob. A person holding a box, a car with a trailer, two objects touching - SAM is happier when you ask it for one thing at a time, and you get better masks by splitting the job. MatAnyoneMergeMasks is how you put those separate masks back together: it combines up to four masks into a single thresholded output.

It also sits at the end of the scripted path in the pack's extended demo (SAM Refine → Merge Masks → Matte), which tells you its real role: it's the bridge between "I refined several masks" and "the Matte node wants exactly one first-frame mask."

How it works

The merge is deliberately un-fancy, which is the right call for this job. mask_a is required; mask_b, mask_c, and mask_d are optional. The node resizes every mask to the same dimensions, takes the element-wise maximum across all of them (so a pixel that's foreground in any mask becomes foreground in the result - union semantics), then applies threshold (default 0.5) to produce a clean binary mask. One output: mask.

The threshold matters because SAM outputs soft float masks, not binary ones. At 0.5, any pixel with a ≥50% foreground probability survives. Lower it (say 0.3) if you're cutting delicate edges and want to keep the soft fringe; raise it if your masks are overlapping generously and you want a tighter result.

When you'd actually use it

  • Multi-object cut: person via one SAM prompt, their backpack via another, merged into one matte for the propagation. The editor's multi-target mode does this for clicks; this node is the scripted equivalent.
  • Combining the editor and the scripted path: refine a mask by hand in the editor, refine a second region with SAM Refine, merge the two. Mixing tools is fine; the Matte node doesn't care where its mask came from.

Install & gotchas

Pack install as always: ComfyUI Manager search "MatAnyone2", or clone + pip install -r requirements.txt + python install.py + restart.

Two things to know. First, the merge is a union, not a blend - overlapping regions don't average out, they stay foreground. That's what you want for matting (a subject is either in the cut or it isn't), but don't reach for this node expecting smooth alpha compositing; that's not its job. Second, all input masks get resized to the first mask's dimensions, so feeding it wildly different resolutions will stretch the smaller ones. Keep your masks from the same frame and you're fine. And the classic mistake - feeding it masks from different frames - produces a franken-mask that propagates weirdly. Masks being merged should describe the same frame.

CategoryMatAnyone2/SAM

Inputs (5)

NameTypeDefaultDescription
mask_aMASK
thresholdFLOAT0.500–1
mask_boptMASK
mask_coptMASK
mask_doptMASK

Outputs (1)

NameTypeDescription
maskMASK