Remove Small Mask Islands v5.1.0
Sweep the specks and flyaways out of your masks
- image
- IMAGE
Every background remover, segmentation model, and human parser leaks specks. A stray chair edge, a flyaway hair, a bit of the floor that survived the cutout - all the small disconnected white blobs that make your mask look like it's covered in static. This node exists to sweep those up. It keeps every contiguous white region bigger than a threshold area and erases everything below it.
It's the kind of tiny cleanup node you don't think about until you need it, and then you need it constantly. In a garment pipeline it's the difference between a clean garment mask and one that carries along a shadow of the person's face or a fleck of background.
How it works
Classic OpenCV connected-component cleanup. The image is converted to grayscale, thresholded to pure black/white at 127, and then findContours with RETR_EXTERNAL picks out each disconnected white region. Any contour whose area is below min_island_area is dropped; the survivors are redrawn into a clean mask. The clean mask is then ANDed against every channel of the original image, so you get the original back with the small islands erased - not just a new mask, but a cleaned image.
The invert boolean flips the logic for working on black islands instead of white ones: it inverts the image first, cleans the black regions, then inverts back. Handy if your mask is stored with the "filled" areas as black.
The inputs that matter
image- the mask (or image) to clean. It takes the first frame if you feed it a batch.min_island_area(INT, 1–10000, default 100) - the cutoff in pixels. Contiguous regions smaller than this get removed. Start at 100 and raise it if specks survive.invert(BOOLEAN, default false) - set true to treat black blobs as the islands.
Output: the cleaned IMAGE.
Installing it
Same pack, one install:
cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes
cd tri3d-comfyui-nodes
pip install -r requirements.txt
or ComfyUI Manager → "tri3d-comfyui-nodes", restart, under TRI3D.
Common issues
- It can't fix connected noise - the cleanup works on disconnected islands. A speck touching the main mask via a thin bridge survives because it's part of the same contour. If your masks have that problem, erode first, clean, then dilate back.
- Threshold surprises - the binarization happens at 127 on the first channel, so a soft/feathered mask gets hard-cut at 50% grey. Semi-transparent edges become solid or vanish depending on which side of 127 they fall.
- It processes one image - feed it a batch and only the first frame gets cleaned. Loop or split if you need batch cleanup.
Pair it with the pack's other cleanup nodes (tri3d-clean_mask does a similar job as a percentage-of-image threshold) and you can keep masks tidy without leaving the pack.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| min_island_area | INT | 1001–10000 | — |
| invert | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |