Nodes/ComfyUI-YCNodes/Mask Split Filter
ComfyUI Node

Mask Split Filter

Explode a multi-object mask into one mask per object

By yichengup·Created 2 years ago·Updated 4 months ago· 48
Mask Split Filter
  • mask
  • segmented_masks

You have one mask with five objects in it, and you want to edit them one at a time. Mask Split Filter turns that one mask into a batch of masks - one per connected component - so each object becomes its own selectable, addressable region. It's the "explode" step that makes per-object processing possible without a SAM model.

The output is a batched tensor, sorted by each component's top-left corner (roughly reading order, left-to-right then top-to-bottom). So if your mask has three blobs, you get a batch of three masks, each isolating one blob, in a stable order you can index with a Mask From Batch node or feed into a batch-aware loop. Downstream, one batch-of-masks means one pass: apply the same edit to every object, or slice out individual ones.

How it works

It runs cv2.findContours in RETR_TREE mode, which captures nesting - and it does something slightly clever: when a contour has child contours (a hole or an inner object), it subtracts the children from the parent before emitting the mask. So a donut stays a donut rather than being filled in, and an object with a separate inner blob keeps them distinct. Each component is drawn filled onto its own mask, converted to a tensor, and concatenated into the batch.

It only takes the first frame of the input mask - feed it one mask at a time.

The input and output

One input, mask (MASK). One output, segmented_masks (MASK) - a batch where each item is one component. Feed the batch into something batch-aware, or use Mask From Batch (same pack, "YCNode/Mask/Batch") to pull out a specific index by number.

Install

From ComfyUI-YCNodes. ComfyUI Manager → search "ComfyUI-YCNodes", or:

cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes

Restart ComfyUI. Standard deps (torch, numpy, pillow, opencv-python, scipy), no model downloads. Under "YCNode/Mask".

Gotchas

The order is top-left based, which is stable but not always what you'd intuitively call "object 1" - if your workflow depends on which index is which, verify once visually (the pack's MaskPreview_YC is good for that). Nesting is respected, but remember it's a mask operation: overlapping blobs that are fused into one connected region won't split here. That's what Mask Smart Valley Split from the same pack is for - it cuts fused blobs at the pinch first, then this splits the results into individual masks. Two splitters, two jobs, and they chain cleanly.

CategoryYCNode/Mask

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (1)

NameTypeDescription
segmented_masksMASK