Split Mask by Contours
Split Mask by Contours
- mask_prior
- mask_alternative
- mask
- mask
Segmentation and detection nodes often hand you back one mask that actually covers two or more separate regions - two people standing apart in a frame, a subject plus a disconnected accessory, two detected objects lumped into a single mask output. SplitMask takes that one blob of a mask and splits it into two, based on the area of each disconnected region (its "contour," in the CV sense) rather than anything spatial like left/right position. Per the README: "this node splits one mask into two masks of the same size according to the area of the submasks. If there are more than two areas, it will select the two largest submasks."
That last sentence is the part to actually internalize: this isn't a general N-way mask splitter. If your source mask has three or more disconnected regions - say a segmentation pass that picked up some noise along with your two real subjects - this node keeps only the two biggest by area and discards the rest. If you need every region preserved individually, this specific node won't do that; it's built for the common two-subject case, not an arbitrary-count one.
Inputs: mask_prior is required - the combined mask you're splitting. mask_alternative is optional, and its exact role isn't detailed further than the README's description above, so treat it as a fallback or secondary input to experiment with rather than assume a specific behavior; if your masking pipeline has a primary and backup mask source, this is presumably where the second one goes.
Outputs: two MASK outputs, both simply labeled mask in the node's output list - since they're not otherwise distinguished, you'll want to preview both after wiring them up the first time to confirm which one corresponds to which region for your particular input, rather than assuming a fixed left/right or first/second ordering without checking.
Install: search "ComfyUI-utils-nodes" in ComfyUI Manager, or:
cd ComfyUI/custom_nodes
git clone https://github.com/zhangp365/ComfyUI-utils-nodes
Restart ComfyUI. No models or API keys - this is pure mask math running on CPU.
Where this trips people up:
- Splitting is area-based, not identity- or position-based. If you're expecting "left region goes to output one, right region goes to output two" consistently across different inputs, that's not the guarantee here - it's ranked by size, so which physical region lands on which output can shift between runs depending on which happens to be larger.
- A mask with two regions of nearly identical size is the fragile case - a tiny difference in area can flip which one gets treated as "first" between two otherwise similar inputs, so don't build logic downstream that assumes a stable mapping unless you've verified it holds for your specific source material.
- If your upstream mask only has one contiguous region to begin with (nothing to split), don't expect a meaningful second output - feed this node only after confirming your mask actually contains multiple disconnected areas, typically from a segmentation or detection node that can return multi-subject results.
- For genuinely noisy masks with small stray regions, this node's area-based two-largest selection is doing double duty as a de-noiser - which is convenient when that's what you want, and a silent data-loss trap when one of the "smaller" discarded regions was actually something you needed.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask_prior | MASK | — | |
| mask_alternativeopt | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |
| mask | MASK | — |