Nodes/tri3d-comfyui-nodes/Clear small patches v5.1.0
ComfyUI Node

Clear small patches v5.1.0

Drop masks too small to matter, and get a flag saying so

By TRI3D-LC·Created 3 years ago·Updated about a year ago· 27
Clear small patches v5.1.0
  • masks
  • mask
  • cleaned
threshold1.00

Segmentation noise is a fact of life: a stray pixel blob from background removal, a speck of skin tagged as garment, a mask that's technically there but covers 0.3% of the frame. tri3d-clean_mask is the filter that throws those out. Feed it a MASK batch and a threshold percentage, and it zeroes out any mask whose white area is below that threshold - then tells you whether anything actually got cleaned.

What it does

Two inputs: masks (a MASK batch) and threshold, a float from 0 to 100, default 1. The threshold is a percentage of the image area. For each mask it binarizes (anything below 127 goes to 0, the rest to 255), measures how much of the frame is white, and:

  • if the white area exceeds threshold percent, the mask passes through unchanged
  • if not, the mask is replaced with all-zeros

Two outputs: mask (the filtered batch) and cleaned, a BOOLEAN that's True when the masks were small enough to be wiped (i.e. "this run did clean something"), False when the masks survived. That flag is the sneaky-useful part - you can wire it into a logic gate to skip downstream work when there's nothing left to process.

One thing the docstring overpromises

The class docstring says it removes "all patches in the mask with area smaller than threshold" - small islands within a bigger mask. The active code doesn't do that: the per-patch connected-component logic is commented out, and it actually checks the total white area of the whole mask. So a large mask with a few tiny speckles is passed through intact, not cleaned. If you specifically need speckle removal inside a mask, this isn't the node - you want a morphological open (erode + dilate) elsewhere. Good to know before you assume the noise is gone.

Where it fits

In the pack's VTON pipeline it's a guard: after tri3d-extract-masks-batch, a mask can come back near-empty when a part wasn't detected, and feeding that downstream pollutes the result. This node turns "near-empty" into "definitely empty" plus a flag. It's also handy for cleaning up noisy background-removal masks on batch runs.

Installing

ComfyUI Manager (search "tri3d"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes

Restart, install requirements (pip install -r custom_nodes/tri3d-comfyui-nodes/requirements.txt). No model downloads - pure threshold math.

Troubleshooting

  • Everything survives - threshold is below the area of your smallest real mask; raise it.
  • Everything gets wiped - threshold too high for your content, or your masks aren't actually white-on-black (check polarity; an inverted mask has huge white area).
  • The cleaned flag reads wrong - remember it's "were these small enough to clean", not "did the node do good work".

Small, honest, and the flag output is the reason to reach for it over a two-node workaround.

CategoryTRI3D

Inputs (2)

NameTypeDefaultDescription
masksMASK
thresholdFLOAT1.000–100

Outputs (2)

NameTypeDescription
maskMASK
cleanedBOOL