Nodes/ComfyUI-Info-Prompt-Toolkit/Remove Small Mask Regions
ComfyUI Node

Remove Small Mask Regions

Kill the Speckle Noise in Your Masks Without Killing the Subject

By kinorax·Created 5 months ago·Updated about a month ago· 2
Remove Small Mask Regions
  • mask
  • mask
min_image_area_percent0.004
threshold0.50

Mask generation is noisy. A SAM segmenter or a tagger-based mask will give you the subject, sure, but it also hands you a spray of tiny isolated foreground specks - a glint of light here, a single stray pixel there - and when you use that mask for inpainting or a detailer pass, every speck is a region you didn't want touched. IPT-RemoveSmallMaskRegions is the cleanup pass: it thresholds the mask, finds connected foreground islands, and deletes any island smaller than a minimum area percentage. The big connected region survives; the speckle goes.

It's the mask-cleaning complement to Mask Overlay Comparer (which shows you the mess) in this pack. Soft masks from the pack's SAM3 node are explicitly meant to be thresholded and cleaned downstream - this is one of the downstream nodes.

How it works

The input mask is thresholded first: pixels above the threshold value become foreground (default 0.5). Then each connected foreground component is measured as a percentage of the total image area, and any component smaller than min_image_area_percent (default 0.004% - tiny) is removed. What comes out is a binary mask - the input's softness is gone after this, replaced by a clean yes/no boundary. If you set min_image_area_percent to 0, it skips the island removal and just returns the thresholded mask, which is a handy way to get a binary mask out of a soft one.

Inputs and outputs

  • mask (required, MASK) - soft or binary input mask.
  • threshold (FLOAT, default 0.5) - pixels above this become foreground.
  • min_image_area_percent (FLOAT, default 0.004) - islands smaller than this percentage of the image area are removed.
  • mask (output, MASK) - the cleaned, binary result.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/kinorax/comfyui-info-prompt-toolkit.git
cd comfyui-info-prompt-toolkit
pip install -r requirements.txt

Restart, or ComfyUI Manager → "ComfyUI-Info-Prompt-Toolkit". ComfyUI 0.17.0+. No models.

Common issues

Because it binarizes, don't use it when you need to preserve soft mask edges - a fractional-alpha matting mask from something like BiRefNet wants to stay soft for its downstream use, and thresholding it throws away exactly the information that makes edges look good. That's why the pack keeps the soft-path (Remove Small Soft Mask Regions) as a sibling. The min_image_area_percent default of 0.004% is aggressive-by-design for speckle; if you're finding the subject gets carved away too, raise the threshold first (0.5 means half-bright pixels count as foreground) before touching the area slider. And for a "soft → clean binary" conversion in one step with no island removal, set the area to 0 - it's a freebie behavior worth remembering.

CategoryInfo-Prompt-Toolkit/Mask

Inputs (3)

NameTypeDefaultDescription
maskMASKSoft or binary input mask. The node thresholds it before removing islands.
min_image_area_percentFLOAT0.0040–100Connected foreground islands smaller than this image-area percentage are removed.
thresholdFLOAT0.500–1Pixels above this value are treated as foreground before filtering.

Outputs (1)

NameTypeDescription
maskMASK