Mask Cleanup (ComfyTV)(MaskCleanup)
Mask Cleanup tidies up a MASK by removing small stray islands and filling small holes.
Mask Cleanup
Description
Mask Cleanup tidies up a MASK by removing small stray islands and filling small holes. It is a post-processing utility, useful after segmentation, matting, or any workflow that produces noisy binary masks where tiny specks or pinholes are unwanted but the main shapes should be left intact.
The node compares each connected white region and each enclosed black hole against the size of the mask's largest region. Anything below the configured fraction (subject to a minimum absolute size) is treated as noise and either removed or filled. The output is a cleaned MASK of the same shape, ready to feed into the downstream nodes that produced the original input.
Inputs
-
mask (MASK, required): The mask to clean up. It is interpreted as a set of white (foreground) regions on a black background, with holes being black areas fully enclosed by white.
-
min_region_frac (FLOAT, required): Controls how aggressive the cleanup is. Defaults to
0.01, with a range from0to0.2and a step of0.001. Islands and holes smaller than this fraction of the mask's largest region are removed or filled respectively. For example, at the default0.01, any speck or pinhole smaller than 1 percent of the biggest region's area is cleaned away. An absolute floor of 64 pixels applies, so very small fractions still remove only genuine tiny artifacts rather than nibbling at legitimate detail.
Outputs
- MASK: The cleaned mask, with small islands removed and small holes filled. The overall silhouette of the large regions is preserved, so this output can be substituted directly for the original mask in the rest of the workflow.
Usage Notes
- The threshold is relative to the mask's largest region, so the node adapts to different scales: a mask with one dominant blob and a mask with many similarly sized blobs will each be cleaned proportionally.
- Use a lower value such as
0.001to0.005when you want to preserve small but legitimate features, and a higher value closer to0.05or more when the input is very noisy and you only care about the major regions. - Setting the value to
0disables most cleanup, leaving only artifacts below the 64 px absolute floor affected. - Because holes are filled rather than removed, this node is a good choice for closing pinholes in foreground masks before using them for inpainting or compositing.
- The node works on connected regions, so it is not a substitute for smoothing or feathering; it targets isolated specks and holes rather than the edges of the main shapes.
Comments
Sign in with GitHub to join the discussion.