Nodes/MatAnyone2 Video Matting/MatAnyone2 SAM Refine
ComfyUI Node

MatAnyone2 SAM Refine

Point Coordinates In, a Graded Mask Out

By dreamrec·Created 6 months ago·Updated 6 months ago· 3
MatAnyone2 SAM Refine
  • sam_model
  • image
  • prompt
  • previous_logits
  • mask
  • preview
  • logits
  • score
multimask_outputtrue
mask_choicebest
use_previous_logitstrue
preview_opacity0.65

If you want the mechanical version of what the editor does, here it is. MatAnyoneSAMRefine takes a frame, a MATANYONE_PROMPT built from explicit point coordinates, and runs Segment Anything to produce a mask - plus something the clicky editor never shows you: a confidence score. That score is the whole reason you'd choose the scripted path.

The scripted flow lives in the pack's extended demo: SAM Loader → Prompt Start → Add Point → SAM Refine → Merge Masks → Matte. No editor, no clicking, fully reproducible - paste the workflow, change three coordinates, re-run. For anything automated or batched, this is your workhorse.

How it works

Under the hood it's a standard SamPredictor: set_image on your frame, then predict with your point coordinates and labels (1 = foreground, 0 = background). The two knobs that matter:

  • multimask_output (default on) - SAM returns up to three candidate masks for the same points, because one set of clicks can plausibly mean several things. Each gets a score.
  • mask_choice - how you pick from those candidates. best (default) takes the highest score; or pin 0/1/2 if a specific candidate shape is what you want for a repeatable batch.
  • use_previous_logits + the optional previous_logits input - feed the logits from a prior SAM Refine pass back in to keep refining the same mask rather than starting fresh. This is the "iterative" SAM pattern: first pass guesses, second pass you correct with one more point, and the low-res mask carries the context forward.

Outputs: mask (MASK), preview (IMAGE overlay at preview_opacity, default 0.65), logits (MATANYONE_SAM_LOGITS - wire back into previous_logits), and score (FLOAT, the confidence of your chosen candidate).

The inputs that matter

sam_model comes from SAM Loader, image from Select Frame, prompt from the Prompt chain. Then honestly it's just mask_choice and the logits loop. The score output is your feedback channel: if it's low, your points are ambiguous - add another via Add Point and refine again.

Install

The pack, once more: ComfyUI Manager search "MatAnyone2", or clone, pip install -r requirements.txt, python install.py, restart. No extra dependencies beyond the pack's, since SAM lives in vendor/.

Where it bites

Two traps. First, coordinates are in pixel space of the frame you feed it - a 512×320 default point means nothing if your frame is 1920×1080. Get them from where your frame is, or your point lands in the wrong place and your score tells you. Second, this is a per-frame operation: it refines one frame's mask. It does not track the subject across the clip - that's the Matte node's job. People wire SAM Refine straight to a video output and wonder why only the first frame is cut. Don't be that person; the whole architecture is refine-one-frame, then propagate.

If multimask_output is on and you see the "wrong" object selected, best isn't always right - SAM's top score can pick a larger, more confident region than the thing you clicked. That's what mask_choice 0/1/2 exists for.

CategoryMatAnyone2/SAM

Inputs (8)

NameTypeDefaultDescription
sam_modelMATANYONE_SAM
imageIMAGE
promptMATANYONE_PROMPT
multimask_outputBOOLEANtrue
mask_choiceCOMBObest4 options: best, 0, 1, 2
use_previous_logitsBOOLEANtrue
preview_opacityFLOAT0.650–1
previous_logitsoptMATANYONE_SAM_LOGITS

Outputs (4)

NameTypeDescription
maskMASK
previewIMAGE
logitsMATANYONE_SAM_LOGITS
scoreFLOAT