Nodes/KJNodes for ComfyUI/Separate Masks
ComfyUI Node Runs on cloud

Separate Masks

Split one mask into per-object masks

By kijai·Created 3 years ago·Updated a day ago· 3,011
Separate Masks
  • mask
  • mask
size_threshold_width256
size_threshold_height256
mode
max_poly_points8

You segmented an image and got back one mask with three blobs in it - two people and a chair, say - but you want to treat each one separately. That's what Separate Masks is for. It takes a single mask and splits it into multiple masks along its connected components, so each distinct blob becomes its own mask in the output batch. It's the missing step between "I have a segmentation" and "I want to process each object on its own."

It's part of KJNodes, kijai's utility pack. This one leans on OpenCV-style connected-component logic, so it's a real geometric split, not a hack.

How it works

A connected component is just a group of touching mask pixels. The node finds each one, throws away the ones smaller than your size thresholds (so specks and stray noise don't become their own masks), and emits the survivors as separate masks. The clever part is the mode, which decides how each component gets represented - as its exact pixels, as a bounding box, or as a simplified polygon.

The inputs that matter

  • mask - the mask to split.
  • size_threshold_width / size_threshold_height - the minimum size a blob has to be to count. Both default to 256. Raise them to ignore small fragments; lower them if the node is dropping regions you actually wanted.
  • mode - the representation:
    • area keeps each component's real pixel shape (most faithful).
    • box returns each component's bounding rectangle - handy when the next step is a crop.
    • convex_polygons wraps each blob in a convex hull, smoothing jagged edges.
  • max_poly_points - only relevant in convex_polygons mode; caps how many points the hull uses (3–32). Fewer points = blockier, simpler shapes.

The output is a mask batch - one mask per surviving component. This is a preview/output node, so it'll also show you the split result on the node itself.

Installing it

ComfyUI Manager → search KJNodes for ComfyUI → install → restart. Or cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-KJNodes, then pip install -r ComfyUI-KJNodes/requirements.txt, and restart.

Where people get burned

  • The output is a batch, not one mask. Everything downstream has to handle a mask batch - you'll iterate over it or index into it. Wire it into a node that expects a single mask and you'll process only the first component (or error).
  • Thresholds too high. Set the size thresholds above your smallest real object and it vanishes from the output. If a region you wanted is missing, drop the thresholds.
  • Wrong mode for the job. box is great for feeding a crop but useless if you needed the exact silhouette - use area for shape-faithful masks and convex_polygons only when you specifically want the edges simplified.
CategoryKJNodes/masking

Inputs (5)

NameTypeDefaultDescription
maskMASK
size_threshold_widthINT2560–4096
size_threshold_heightINT2560–4096
modeCOMBO3 options: convex_polygons, area, box
max_poly_pointsINT83–32

Outputs (1)

NameTypeDescription
maskMASK