遮罩筛选 / Mask Selector
Pick one mask out of a pile and keep the leftovers
- masks
- 选中遮罩 / Selected
- 剩余遮罩 / Remaining
- 信息 / Info
Detectors have a habit of finding more than you wanted. You run SAM or a detection model and you get back a batch of masks - every object in the frame - when what you actually wanted was just the biggest one, or the one on the left. Mask Selector is the node that untangles that pile. It sorts the masks you give it, hands you whichever one you asked for, and also hands back the merged remainder, so nothing you didn't pick is lost.
That second output is the part people overlook, and it's the reason this node is more useful than a plain "index into a batch." You can separate the subject from everything else in one step: select the largest mask as your subject, and the Remaining output is already the background mask. That's a two-node foreground/background split, which sounds trivial and turns out to be surprisingly handy in compositing and inpainting graphs.
How it works
Give it a MASK batch (a single mask works too - it gets padded to a batch of one). The node thresholds at 0.5, computes each mask's pixel area and centroid, then sorts according to sort_method:
按面积排序 / By Area- largest first. This is the one to reach for when you want "the main subject."从左到右 / Left to Right- by centroid X position.从上到下 / Top to Bottom- by centroid Y position.
Empty masks get sorted to the end so they don't randomly win by area. Then index picks which one you want, 1-based. Here's the forgiving bit: if your index is out of range, it clamps to the valid range instead of erroring - and the 信息 / Info output tells you it did (index_clamped: true), along with the total count, the sort method, and the selected mask's area and center coordinates as JSON.
The three outputs:
选中遮罩 / Selected- the single mask you picked.剩余遮罩 / Remaining- every other mask merged into one.信息 / Info- JSON diagnostics.
Installing it
Part of the RUI-Nodes pack ("Rui-Node🐶"). Install once via ComfyUI Manager (search "RUI-Nodes") or:
cd ComfyUI/custom_nodes
git clone https://github.com/rui40000/RUI-Nodes
cd RUI-Nodes
pip install -r requirements.txt
Restart after. It's pure torch - no extra dependencies beyond the pack's base torch/numpy.
Where people get burned
The main gotcha is sorting by area on noisy masks - a detector that over-segments can produce dozens of tiny fragments that all sort before or after what you think of as "the" object, so your index lands on the wrong thing. The fix is to check the Info JSON: it tells you the selected area and center, so a wrong pick is diagnosable instead of mysterious. And if your masks are touching - one object's mask bleeding into another's - the "largest by area" logic sees one merged blob, not two. That's a segmentation-quality problem, not a node bug, but knowing it's the cause saves you an hour.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| masks | MASK | 待筛选的遮罩批次(N,H,W)。单张遮罩也可以,会自动补成批次。 | |
| sort_method | COMBO | 按面积排序 / By Area | 先按此规则排序,再用下面的编号取第几个。 按面积:从大到小,取主体用它最稳 从左到右 / 从上到下:按遮罩质心的坐标排 空遮罩一律排到末尾。 |
| index | INT | 1 | 取排序后的第几个遮罩,从 1 开始。 超出总数会自动夹到最后一个,并在 info 里标记 index_clamped。 |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| 选中遮罩 / Selected | MASK | — |
| 剩余遮罩 / Remaining | MASK | — |
| 信息 / Info | STRING | — |