Nodes/Comfyui_LG_Tools/🎈LG_合并批次遮罩
ComfyUI Node

🎈LG_合并批次遮罩

Turn a pile of masks into one usable mask

By LAOGOU-666·Created 2 years ago·Updated 7 months ago· 369
🎈LG_合并批次遮罩
  • masks
  • merged_mask
merge_modemax

Masks have a way of multiplying. A face-detector gives you one mask, a hands detector another, a body segmenter a third. A video processor hands you a mask per frame. Inpainting and compositing, though, want one mask - a single clean region that says "work here." Nobody wants to hand-fuse a dozen masks by hand.

LG_MaskBatchMerge (合并批次遮罩 = "merge batch masks") collapses a whole batch of masks into a single mask, with four merge modes so you can choose exactly how the regions combine.

How it works

The node takes a mask batch shaped (B, H, W) - B masks, all the same height and width - and reduces it along the batch dimension into one (1, H, W) mask. The merge_mode dropdown decides the math:

  • max - pixel-wise maximum. The union: anywhere any mask covers, the result covers. The default, and the one you'll want for combining complementary detections (face + hands → "protect or edit all of it").
  • min - pixel-wise minimum. The intersection: only where every mask overlaps. Good for "only the region all detectors agree on."
  • average - the mean across masks. Produces soft blended values.
  • sum - the total, clamped to 0–1. Useful when you want overlapping coverage to read as stronger, up to full.

Inputs:

  • masks - the MASK batch.
  • merge_mode - max, min, average, or sum.

Output:

  • merged_mask - the single merged MASK.

When it earns its keep

  • Inpainting - stack the masks from several segmenters, merge with max, and feed one solid mask to your inpaint path (VAE Encode for Inpaint, Set Mask, etc.) instead of a stack of overlapping regions. The KB's inpainting doc is all about the mask being the contract with the model - this is the node that makes a composite contract.
  • Video - collapse per-frame masks from AnimateDiff-frame segmentation into one aggregate mask, so a single mask describes "where the subject was, ever."
  • Compositing - union multiple selections before you cut something out.

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 right-click → 🎈LAOGOU → Utils.

Troubleshooting

  • "Input mask batch is empty" - the node raises if B is zero; wire an actual batch, not an empty one.
  • Shape mismatch error - all masks in the batch must share H and W. If your detectors returned different resolutions, resize them before merging.
  • Average looks thin - with average, every empty mask drags the value down. If one mask covers a region and nine empties don't, the average over ten is 0.1, and downstream may treat that as "basically nothing." Use max for union semantics or sum if you want overlap to count.
  • Sum saturates - overlapping regions clamp at 1.0, so you can't get "double strength" out of it, just "full."

For the common case - several complementary masks, one inpaint region - it's a two-click fix. Just pick max and stop thinking about it.

Category🎈LAOGOU/Utils

Inputs (2)

NameTypeDefaultDescription
masksMASK
merge_modeCOMBOmax4 options: max, min, average, sum

Outputs (1)

NameTypeDescription
merged_maskMASK