Nodes/ComfyTV/Mask Cleanup (ComfyTV)
ComfyUI Node

Mask Cleanup (ComfyTV)

Delete the specks your segmentation mask left behind

By jtydhr88·Created 3 months ago·Updated about 16 hours ago· 725
Mask Cleanup (ComfyTV)
  • mask
  • mask
min_region_frac0.010

Mask Cleanup is the tidying-up node you reach for right after any automatic segmentation. SAM and its cousins are great at finding a subject, but they also hand back masks that look like they were drawn with a dry brush - a dozen disconnected islands where the background texture caught the model's eye, holes punched through the subject, a few stray specks orbiting it. This node sweeps all that: it removes islands smaller than a threshold and fills holes, leaving one clean region you can actually key or composite with.

This one is also the odd node out in this pack, and worth calling out: Mask Cleanup is not a ComfyTV stage. It's a plain ComfyUI node (is_output_node false) that works in any ordinary ComfyUI graph, takes a standard MASK, and outputs a standard MASK. You don't need the whole ComfyTV canvas app to use it - it's a free utility hiding in the pack, in the ComfyTV/Utils category.

How it works. The node labels connected regions in the mask (via scipy's ndimage), finds the largest one, and removes every island whose area is smaller than min_region_frac of that largest region - with an absolute floor of 64 pixels so a tiny subject doesn't get deleted by a loose threshold. Holes get filled the same way. The default min_region_frac of 0.01 is conservative: anything smaller than 1% of your main region is gone. If scipy isn't installed, it logs a warning and passes the mask through untouched rather than crashing - a thoughtful failure mode.

The input that matters. Just two, and one of them is a one-time tune:

  • mask - the MASK to clean (required).
  • min_region_frac - 0–0.2, default 0.01. The minimum island/hole size as a fraction of the largest region. Raise it (say 0.05) for aggressive cleanup on noisy masks; keep it low if your mask legitimately has several real parts.

Output is the cleaned mask (still a MASK), ready to feed an inpaint, a detailer, or any downstream mask consumer.

Install. Install the pack once:

cd ComfyUI/custom_nodes
git clone https://github.com/jtydhr88/ComfyTV

Restart ComfyUI (full restart, not a browser refresh) and it appears under ComfyTV → Utils. ComfyUI Manager - search "ComfyTV" - works too. Zero Python dependencies in the pack itself, but note this node wants scipy for the region labeling - it degrades to passthrough without it, so if you see the "scipy unavailable" warning in the log, pip install scipy into your ComfyUI environment. No model files needed. Desktop/multi-install users: clone into the running instance's path from the startup log or the node won't register.

Where people get burned. The threshold is relative, not absolute - and there's a 64px floor precisely so that "1% of the biggest region" doesn't accidentally delete a legitimate small part on a big mask. If the cleanup is eating parts you want, the fix is a lower min_region_frac, not bigger min_region... and if it's leaving specks you hate, raise it. Also: this node takes a MASK, so if you're pulling the alpha out of a ComfyTV stage (which hands you COMFYTV_IMAGE-shaped mattes), you may need to route through a mask conversion first. And remember it's per-image - for video mattes you'll want the Mask Propagate stage instead, which is built to track masks through time.

CategoryComfyTV/Utils

Inputs (2)

NameTypeDefaultDescription
maskMASK
min_region_fracFLOAT0.0100–0.2Islands/holes smaller than this fraction of the mask's largest region are removed/filled (absolute floor: 64 px).

Outputs (1)

NameTypeDescription
maskMASK