Filter Mask By Solidity
Keep the solid blobs, drop the fragments — filter masks by how convex they are
- mask
- filtered_mask
Imagine your mask has one nice, complete subject blob and a scattering of broken fragments around it. Area-based filters won't help much - sometimes the fragment is huge. Filter Mask By Solidity ranks blobs by a different property: solidity, which is area divided by convex-hull area. A solid, intact shape scores near 1.0; a ragged, shredded fragment scores low. Keep the top ones, drop the rest.
Solidity is exactly the metric for "is this blob whole or is it a fragment of something?" A complete shape - a head, a person, a full product - has a convex hull close to its own area. A torn remnant of a mask is all edge and concavity, so its hull is much bigger than its area and its solidity tanks. That's why this node beats a plain "keep the biggest N" when your noise isn't small: it's filtering for integrity, not size.
How it works
Per mask in the batch, it finds external contours with OpenCV, drops any below min_area (default 1000 - in pixels, so tune for resolution), computes solidity per surviving contour, sorts descending, keeps the keep_top_n most solid, and redraws those filled onto a blank mask. Defaults are keep_top_n=1, min_area=1000 - i.e. "give me the single most intact blob."
The inputs that matter
keep_top_n(1–10, default 1) - how many blobs to keep.min_area(0–99999, default 1000) - the floor before solidity even gets considered.
Between those two dials you can go from "just the one clean subject" to "the three most intact regions." The output is filtered_mask, a single mask of the same dimensions. It does process the batch frame-by-frame, so multiple input frames each get their own filtering.
Install
Part of ComfyUI-YCNodes. ComfyUI Manager → search "ComfyUI-YCNodes", or:
cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes
Restart ComfyUI. Standard deps, no downloads. Under "YCNode/Mask".
Gotchas
Solidity cares about convexity, not size - a small clean circle beats a huge jagged blob, which is usually what you want but worth knowing when it surprises you. min_area is absolute pixels, so the same settings behave differently at 512px vs 4K. And the output is hard-edged: it redraws contours filled, so any feathering in the source is gone. If you need soft edges after filtering, blur downstream. The sibling node Mask Top-N Filter does the same "keep N blobs" job but by area - solidity's the one that understands fragments.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| keep_top_n | INT | 11–10 | — |
| min_area | INT | 10000–99999 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| filtered_mask | MASK | — |