Nodes/Comfyui_LG_Tools/🎈LG_填充空洞
ComfyUI Node

🎈LG_填充空洞

Fill the holes inside a mask in one step

By LAOGOU-666·Created 2 years ago·Updated 7 months ago· 369
🎈LG_填充空洞
  • mask
  • filled_mask

Automatic masks are never as clean as they look. Run a segmentation model - rembg, a BiRefNet-style remover, SAM - over a person and the subject mask comes back with holes: eyes carved out, mouth punched through, little specks where the model got confused. Those holes are invisible in a preview and obnoxious in practice. Feed a holey mask to inpainting and the model regenerates little artifacts inside your subject. Composite with it and the holes show up as gaps.

LG_MaskHoleFiller (填充空洞 = "fill holes") is a one-input cleanup node that fills every enclosed hole in a mask. Feed it the messy mask, get back the same mask with the interior solid.

How it works

It's the classic computer-vision flood-fill trick, implemented with OpenCV. In plain terms:

  1. Binarize the mask - anything above 0.5 becomes solid.
  2. Add a zero border around the edges, so the outside of the mask is one connected region.
  3. Flood-fill from the corner to mark everything connected to the outside.
  4. Whatever's left unmarked inside is a hole. Add it back to the mask.

The result: fully enclosed gaps get filled, but the outside stays outside. A mask that loops around an eye gets its eye filled; a mask that's actually two separate blobs stays two separate blobs.

Input:

  • mask - any MASK.

Output:

  • filled_mask - the same mask with interior holes filled, same batch size.

When it's worth it

  • Inpainting prep - the KB's inpainting docs make the case that the mask is the contract with the model: bit-identical unmasked pixels, and the masked region is what gets regenerated. A hole in the mask is a spot the model is allowed to rewrite that you didn't mean to touch. Filling it first stops that.
  • Background removal / compositing - a solid subject mask composites cleanly; a holey one leaks background through.
  • Before masking effects - blur, feather, or edge operations behave badly when the mask has islands in the middle.

Installing it

Ships in Comfyui_LG_Tools - Manager (search "Comfyui_LG_Tools") or:

cd ComfyUI/custom_nodes
git clone https://github.com/LAOGOU-666/Comfyui_LG_Tools.git
pip install -r requirements.txt

Restart, then find it under right-click → mask (it's one of the few nodes in the pack that isn't under 🎈LAOGOU - look in the mask category).

Troubleshooting

  • My soft/feathered mask turned harsh - this node binarizes at 0.5, always. Any soft edge you had is gone, hard. If you need feathered edges, do your blur/feather after hole-filling, not before.
  • A hole at the edge isn't filled - by design. Only holes fully enclosed by mask get filled; anything touching the border is treated as part of the outside. For most subject masks that's exactly right.
  • Small specks remain - the fill handles enclosed gaps, but stray floating specks outside the main blob are separate blobs, not holes, so they survive. Pair it with a mask clean/denoise node if you need those gone too.

It's the smallest possible cleanup step - in, out, done - but it removes a genuinely annoying class of inpainting artifact. Keep one in your mask-prep chain.

Categorymask

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (1)

NameTypeDescription
filled_maskMASK