Nodes/Comfyui_PDuse/PD:Mask Remove Small Objects
ComfyUI Node

PD:Mask Remove Small Objects

Sweep the specks and dust out of a mask

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD:Mask Remove Small Objects
  • mask
  • cleaned_mask
min_size100
connectivity2

Detector masks are never clean. That hand mask has a pixel-sized dot where a knuckle was, the person mask has a noise speck on the shoulder, and every one of those stray blobs becomes an unwanted inpaint region or a visible artifact in a composite. PD_MaskRemoveSmallObjects ("PD:Mask Remove Small Objects") sweeps them out: it keeps only the connected blobs in a mask that are bigger than a size you set, and deletes everything smaller. It's the "vacuum" of the mask-cleanup pair in the 7BEII/Comfyui_PDuse pack - run this first to kill specks, then PD_MaskFillHoles to close the resulting holes, and your mask is actually trustworthy.

How it works

Straightforward connected-component analysis via OpenCV. The mask is binarized at 0.5, then cv2.connectedComponents labels every separate blob. Each blob's pixel count is compared against min_size: blobs at or above the threshold survive, blobs below it are zeroed out. Everything that survives keeps its exact shape - this node trims, it doesn't morph.

The connectivity option (1 or 2, default 2) controls what counts as "one blob": 1 = 4-connectivity (only orthogonal neighbors join a region), 2 = 8-connectivity (diagonals count too). In practice: 8-connectivity is the usual default and treats a diagonal chain as one object; switch to 4-connectivity if you have two real objects that touch only at a corner and you want them treated separately. The output cleaned_mask is the same size as the input, with small blobs removed.

The code also prints a tidy per-image report to the console - "kept N objects, removed M small objects" - which is genuinely handy when you're tuning min_size across a batch and want to know how aggressive you're being.

The inputs that matter

  • mask - required. It expects a batch of 3D [B, H, W] masks, so a single image's mask works fine.
  • min_size - the blob-size threshold in pixels, default 100. This is the whole dial: too low and dust survives, too high and you'll delete real small subjects (a distant person, a small face in a crowd).
  • connectivity - 1 or 2, above.

Installing

ComfyUI Manager, search Comfyui_PDuse; or:

cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse
cd Comfyui_PDuse
pip install -r requirements.txt

Restart. Requires opencv-python, already in the pack requirements.

Gotchas

The classic footgun: min_size is measured in pixels, not relative area - so at 1024×1024 a min_size of 100 is nothing, while at 512×512 the same setting starts eating small legitimate regions. Scale it with your resolution. Also, because it binarizes at 0.5, a soft feather on a mask edge can cause a thin-but-real region to get counted as one skinny blob and dropped if it dips below the threshold - if your masks are feathered, either clean the feather first or set min_size low and rely on fill-holes for the rest. For hard-edged masks from YOLO-class detectors, it does exactly what the label promises.

CategoryPDuse/Mask

Inputs (3)

NameTypeDefaultDescription
maskMASK
min_sizeINT1001–50000
connectivityoptINT21–2

Outputs (1)

NameTypeDescription
cleaned_maskMASK