Select Mask by Area or Position
SAM found four people, now pick the right one
- masks
- selected_mask
- selection
You described "man in gray shirt" to SAM 3 and it came back with four candidates: the guy you wanted, the guy behind him, a reflection, and something on a poster. Every downstream node in a character-swap or person-removal chain assumes one subject. Select Mask by Area or Position is the node that turns a batch of detections into one mask.
It's nearly trivial. It's also the node that, when it's wrong, makes you re-render a forty-minute sequence.
How it picks
Four rules, applied to a batch of masks:
largest- biggest nonzero area. The default, and right more often than you'd expect for a close-up subject.leftmost/rightmost- sorts valid candidates by the mean x-coordinate of their pixels. Deterministic and stupid in a way that's genuinely useful in two-shots where one subject is consistently on one side.index- takes the Nth valid candidate in detection order. Use this only when you've inspected the output and know the ordering is stable.
Any mask with an area of four pixels or fewer is discarded first, which is how you avoid picking a speck of noise over a person. If nothing survives that filter you get SAM found no subject. Adjust the subject text and inspect the mask preview. - which is a prompt problem, not a selection problem, and the error is telling you so.
Note the index semantics: it's an index into the valid list, not into the raw batch. If you're also using Recover Empty SAM Frames, keep that in mind, because it re-selects on individual frames and the candidate counts won't necessarily match.
Inputs and outputs
Required: masks (a MASK batch, typically straight from SAM 3's detect or track-to-mask output), selection (the enum above) and index (ignored unless you chose index). There are no optional inputs, no thresholds, no widget soup.
Two outputs. selected_mask is the single mask - one mask, and it's what you wire into tracking, into Recover Empty SAM Frames as the initial mask, or into the expansion stage. selection is an H3_SUBJECT_SELECTION, an opaque record of the rule you chose; it exists specifically so Recover Empty SAM Frames can re-apply the same rule on frames where tracking dropped out, instead of guessing. If you skip this output and hand-recover frames later, you'll get mismatched subjects on the recovered frames - the one failure mode of this whole approach.
Install
ComfyUI Manager → search H3 Relay → install → restart, or:
cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/akatz-ai/h3-relay.git
This node is pure tensor arithmetic - no models, no ffmpeg, no SAM. You'll want SAM 3 anyway, since it's the source of the masks: the pack's graphs expect checkpoints/sam3.1_multiplex_fp16.safetensors from the Comfy-Org SAM 3.1 repo, used through ComfyUI's native SAM 3 nodes. ComfyUI 0.37.0+.
If you're using these masks for H3, remember the weights' licence territory excludes the US, EU, UK and Korea - outputs included.
Where people get burned
Masks are one-per-frame when they come from video tracking, and this node does not aggregate over time - it picks one mask out of a batch. So the honest order of operations is: run native SAM detection on a good frame, select once, then track that single mask forward. Selection after tracking is a diagnostic to see which subject the tracker latched onto, not a fix.
And the boring one that catches everybody: leftmost/rightmost are computed per mask, not per frame, so if your two subjects cross over mid-clip, a positional rule silently swaps identity at the crossing. For anything with movement, select on frame zero and track; don't re-select continuously and hope.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| masks | MASK | — | |
| selection | COMBO | 4 options: largest, leftmost, rightmost, index | |
| index | INT | 0 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| selected_mask | MASK | — |
| selection | H3_SUBJECT_SELECTION | — |