Nodes/ComfyUI-ZML-Image/ZML_限制遮罩形状
ComfyUI Node

ZML_限制遮罩形状

Turn any mask into one clean rectangle

By zml-w·Created about a year ago·Updated 2 months ago· 218
ZML_限制遮罩形状
  • 遮罩
  • 形状遮罩
形状方形
膨胀系数1.0
最小面积比例0.010

Masks from detectors are messy. They're blobby, they're fragmented - a YOLO face detector gives you a wobbly polygon, an auto-segment gives you seventeen disconnected splinters, and half of them are below the size where they'd ever matter. ZML_LimitMaskShape (ZML_限制遮罩形状, "limit mask shape") takes that mess and hands you back exactly one thing: a clean square or rectangle covering the region that matters. It's the "sanitize the mask" node, and it's quietly indispensable once you're doing region-based work.

It's from ComfyUI-ZML-Image, zml-w's 160+ node pack, added in a September 2025 update, and it lives in the pack's mask subcategory.

How it works

The pipeline is four steps, all under the hood:

  1. Binarize the input mask - anything above a fixed threshold becomes white, everything else black.
  2. Drop small noise - connected components (blobs) below the 最小面积比例 (minimum area ratio, 0–1, default 0.01) get discarded. The parameter is a fraction of the whole image: at 0.01, a blob must cover at least 1% of the image to survive. This kills the "a few stray pixels" problem in one step.
  3. Bounding box - the surviving region is wrapped in a box, either 方形 (square - side = the larger dimension) or 矩形 (rectangle - the tight axis-aligned bounds).
  4. Scale by 膨胀系数 (expansion factor, 0.1–5.0, default 1.0) - the box grows (factor > 1) or shrinks (< 1) by that multiplier, giving you breathing room or a tighter fit.

Output: a single clean 形状遮罩 (shape mask) - a MASK of that one box.

What it's actually for

This is the mask hygiene step in detailer/region workflows. Feed it a messy mask and get a single clean box you can hand to an inpaint node, a region sampler, or a crop-based detailer - the kind of thing the detection/masking layer of the ecosystem builds entire pipelines around. It's especially good when you want one box for one job: a hand region for a detailer-style re-render, a face box for a face-restore pass, a single canvas region for a mask-conditioned sampler.

The 膨胀系数 is the control you'll actually play with: 1.3–1.5 of padding around a face gives a detailer room to work; below 1.0 tightens toward the subject.

Installing it

Pack install:

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

restart ComfyUI, or ComfyUI Manager → search "ComfyUI-ZML-Image". This one needs OpenCV (it uses connected components), which the pack's requirements install for you. No models.

Where people get caught

It's a bounding box, not a hull - a subject's shape is discarded, and anything outside the box (a second person, a prop) gets ignored. If you need to preserve shape, this is the wrong node. Also watch 最小面积比例: crank it too high and it deletes your actual subject as "noise," returning a black mask; too low and a speck of dust becomes a full-canvas box. Default 0.01 is a sane starting point - tune from there. And remember the fixed binarization threshold: soft, feathered masks get snapped to hard edges, which is usually what you want before a box but a surprise if you expected gradients to survive.

Categoryimage/ZML_图像/遮罩

Inputs (4)

NameTypeDefaultDescription
遮罩MASK
形状COMBO方形2 options: 方形, 矩形
膨胀系数FLOAT1.00.1–5
最小面积比例FLOAT0.0100–1

Outputs (1)

NameTypeDescription
形状遮罩MASK