MatAnyone2 SAM Refine
Point Coordinates In, a Graded Mask Out
- sam_model
- image
- prompt
- previous_logits
- mask
- preview
- logits
- score
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 pin0/1/2if a specific candidate shape is what you want for a repeatable batch.use_previous_logits+ the optionalprevious_logitsinput - 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.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| sam_model | MATANYONE_SAM | — | |
| image | IMAGE | — | |
| prompt | MATANYONE_PROMPT | — | |
| multimask_output | BOOLEAN | true | — |
| mask_choice | COMBO | best | 4 options: best, 0, 1, 2 |
| use_previous_logits | BOOLEAN | true | — |
| preview_opacity | FLOAT | 0.650–1 | — |
| previous_logitsopt | MATANYONE_SAM_LOGITS | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |
| preview | IMAGE | — |
| logits | MATANYONE_SAM_LOGITS | — |
| score | FLOAT | — |