Nodes/ComfyUI-ZML-Image/ZML_遮罩闭合填充
ComfyUI Node

ZML_遮罩闭合填充

Close the gaps in a broken mask

By zml-w·Created about a year ago·Updated 2 months ago· 218
ZML_遮罩闭合填充
  • 遮罩
  • 填充后遮罩
闭合强度0

Masks from detectors are rarely clean. A face detector can leave gaps in the middle of the face, a scribble mask can have a speckle of unselected pixels inside what should be a solid region, and a line-art mask can be a tangle of dashes instead of a closed shape. ZML_MaskFillHoles (ZML_遮罩闭合填充) is the ZML pack's cleanup node for exactly this: it closes and fills masks so what comes out is a solid, usable region.

The single input that matters is 闭合强度 (closing strength, 0–64, default 0), and the author's tooltip says it plainly: the larger the value, the better it can connect broken lines to form a closed region; if your mask has gaps, increase this. Mechanism-wise, the node does two operations from scipy.ndimage. First, if the strength is above 0, it runs a morphological closing - dilate then erode with a disk-shaped structuring element of that radius - which stitches nearby blobs and bridges thin gaps. Second, unconditionally, it runs binary_fill_holes, which fills any black region fully enclosed by white. The result is a solid mask where holes and open edges have been healed.

That two-step split is the design worth understanding. binary_fill_holes is free and always on - it fills internal holes with no parameters at all. The 闭合强度 knob exists only for external gaps: the open breaks along the mask's edge or between separate fragments that would never be "enclosed." So a mask with a hole in the middle gets fixed at strength 0; a mask with an open edge or a dashed outline needs strength up. Start low (1–5) and nudge up until the shape closes - too high and you'll weld unrelated nearby blobs into one, which is usually wrong.

Inputs are just 遮罩 (MASK) plus the strength; output is 填充后遮罩 (the cleaned MASK), same size, single channel. It handles batches, processing each mask independently.

Where this fits in a real workflow: it's the classic pre-detailer or pre-inpaint cleanup - you feed your YOLO/SAM/scribble mask through it before a FaceDetailer-style pass or an inpaint, so the region is contiguous and the re-render doesn't leave unhealed speckles around the edges. It's also genuinely useful on hand-painted masks, where a fast scribble always has interior gaps. The KB's masking doc's "get that mask in SO many ways" point applies here: this is the polish step that makes a sloppy mask usable.

The dependency note matters for installs: it imports scipy.ndimage, so the pack's scipy requirement is doing real work here, not just sitting there. Manager handles it; on a manual clone run pip install -r requirements.txt or you'll get an ImportError the first time this node runs.

Install:

cd ComfyUI/custom_nodes
git clone https://github.com/zml-w/ComfyUI-ZML-Image

or Manager search "ComfyUI-ZML-Image", restart, and look under ZML 图像 → 遮罩. A 300-word node, no model, no network - but it's one of those "wait, that fixed my whole mask" utilities you'll keep.

Categoryimage/ZML_图像/遮罩

Inputs (2)

NameTypeDefaultDescription
遮罩MASK
闭合强度INT00–64数值越大,越能连接断开的线条以形成闭合区域。如果遮罩本身有缺口,增加此值。

Outputs (1)

NameTypeDescription
填充后遮罩MASK