Nodes/ComfyUI-WJNodes/Accurate mask clipping
ComfyUI Node

Accurate mask clipping

Find the exact bounding box of a mask, and optionally crop to it

By 807502278·Created 2 years ago·Updated 11 months ago· 20
Accurate mask clipping
  • mask
  • mask
  • min_y
  • max_y
  • min_x
  • max_x
cropfalse
offset0
threshold0.10

Accurate mask clipping does one job precisely: it finds the tight bounding box around whatever's actually non-zero in your mask, and optionally crops to it. If you've got a mask that's mostly empty space with a small region of actual content - a detected face, or a hand-drawn selection - and you want to know exactly where that content sits, or crop everything down to just that region, this is the node.

The inputs and outputs that matter

mask is your only required input. threshold (default 0.1) sets how bright a pixel needs to be before it counts as "foreground" for finding the box - this matters because a mask isn't always cleanly binary; soft edges, anti-aliasing, or noise can leave faint non-zero pixels scattered outside the region you actually care about, and a threshold that's too low will let those drag your bounding box wider than it should be. offset lets you pad the found box outward (positive numbers) or pull it inward (negative), handy when you want a little breathing room around a detected region before you crop or composite it - the same instinct as "dilation" in the detector nodes elsewhere in this pack. crop, off by default, decides whether the mask actually gets cropped to that box or just measured.

That last point is worth sitting with: even with crop left off, you still get all four boundary coordinates back - min_y, max_y, min_x, max_x - alongside the (uncropped) mask. This node works as a pure measurement tool too, feeding those four numbers into a separate crop node that needs to apply the exact same boundary to both an image and its mask together, which is exactly the shape of adv_crop elsewhere in this pack. The classic detect-crop-refine loop that packs like Impact Pack use for face and hand detailing runs on precisely this idea: find the real boundary of the region you care about, then work at full resolution inside just that boundary instead of the whole frame.

Installing it

Part of ComfyUI-WJNodes, installed the same way as everything else here. ComfyUI Manager: search "ComfyUI-WJNodes," install, restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git

Find it under WJNode → ImageEdit → ImageCrop. No downloads, no special dependencies - it's straightforward mask math.

Where people get burned

A bounding box bigger than you expected almost always means threshold is too low and it's catching faint noise pixels outside your real region - nudge it up. A box smaller than expected, cutting off part of what you'd call the visible mask, means the opposite: raise it more gently, or check whether your mask has genuinely soft, low-opacity edges below whatever threshold you set. And remember offset applies after the boundary is found - if you're trying to tighten the crop rather than pad it, that number needs to go negative, not larger.

CategoryWJNode/ImageEdit/ImageCrop

Inputs (4)

NameTypeDefaultDescription
maskMASK
cropBOOLEANfalse
offsetINT0-8192–8192
thresholdFLOAT0.100–1

Outputs (5)

NameTypeDescription
maskMASK
min_yINT
max_yINT
min_xINT
max_xINT