ComfyUI Node

Detect Mask Sub-Masses

Split it at the neck and find each center

By 9nate-drake·Created 11 months ago·Updated 11 months ago· 6
Detect Mask Sub-Masses
  • mask
  • segs
  • coordinates
  • x
  • y
  • debug
separation_strength5

Here's the situation that made this node exist: your segmentation came back with two people hugging, or a person and their reflection, or an object joined to its own shadow. The mask is one connected blob, but the thing you care about is two. Run a plain center-of-mass and you get a point floating in the bridge between them - useless, because it lands right back in the empty space where they touch. Detect Mask Sub-Masses is the fix: it severs the narrow connections holding the blob together, finds each dense part, and reports a center for each.

It's the specialist sibling in this pack. The other node, Mask to Center Point, splits masks that are already physically disconnected. This one handles the harder case where they're touching. The pack's README labels it WIP, and the author is upfront about it - expect rough edges and don't build a production pipeline around it until you've watched its debug output do its thing a few times.

How it works

The mechanism is morphological opening from scipy, which is a fancy way of saying "erode the mask, then dilate it back." The erosion chews away thin bits first, so narrow bridges vanish while the dense blobs survive. What's left gets re-labeled, and then the smart part happens: it computes each center of mass on the original mask pixels inside each detected region, not the eroded version. So the centers stay weighted by the true shape of the blob instead of drifting toward wherever the erosion bit hardest. One separation_strength slider (default 5, range 1–50) controls how aggressive the severing is - it's the radius of the disk-shaped structuring element doing the opening.

That's the whole input story, plus the same mask / segs pair the rest of the pack takes (each SEGS segment gets analyzed individually, and it handles mask batches frame-by-frame). All CPU math via numpy and scipy, no VRAM, no models.

Outputs

Same four outputs as its sibling: coordinates (JSON string of every sub-mass center), x and y (integers of the first one found only), and debug (a text log of how many sub-masses it found per input and their areas). When in doubt, always look at debug - it's your only way to see what the opening actually did to your mask.

Tuning it

Start at the default 5 and read the debug output. If you're still getting one mass when you expected two, the bridge is thicker than the tool thinks - raise it. If you start seeing coordinates appear inside a single blob you know is one object, you've gone too far and it's splitting dense shapes that have concavities. The trade-off is real and it's the entire skill of this node: high enough to cut the neck, low enough to not dismember the body.

Installing it

ComfyUI Manager, search "ComfyUI Mask to Center Point Nodes", or:

cd ComfyUI/custom_nodes
git clone https://github.com/9nate-drake/ComfyUI-MaskCenter
cd ComfyUI-MaskCenter
pip install -r requirements.txt

Restart after. The whole pack's dependency list is just numpy and scipy - no model files, no heavy installs, and you probably already have both installed.

The honest verdict

For the specific case of "one mask, multiple dense regions, narrow necks," this is the only thing in the pack that does it, and it does it cheaply. The catch is the same one the author flags: it's WIP, and you're relying on a slider plus your own eyeballs via debug to get it right. If your blobs are already disconnected, you don't need this - Mask to Center Point in Separate Regions mode does the same job with less to tune. Reach for this one when the mask is stubbornly one piece and you know it shouldn't be.

CategoryMaskCenter

Inputs (3)

NameTypeDefaultDescription
separation_strengthINT51–50
maskoptMASK
segsoptSEGS

Outputs (4)

NameTypeDescription
coordinatesSTRING
xINT
yINT
debugSTRING