Nodes/ComfyUI-UtilsCollection/Mask to Bounding Box
ComfyUI Node

Mask to Bounding Box

Turn your mask into a crop, a box, or an anchor

By silveroxides·Created 3 months ago·Updated about 17 hours ago· 30
Mask to Bounding Box
  • mask
  • image
  • bounding box
  • cropped image
invertfalse

Masks are the honest currency of local editing - they tell you exactly which pixels matter. But a lot of the pipeline around them wants a rectangle, not a silhouette. Detailers want a box to crop, regional-prompting schemes want boxes to address regions, and inpainting wants a tight area to spend its resolution budget on. That translation from mask to box is this node's entire reason to exist: it finds the smallest bounding box containing all nonzero mask pixels, and if you hand it an image, it crops that too.

It's a small node that slots into bigger patterns. The detail-loop one from the KB: detect → mask → re-render, where the re-render happens on a crop of just the masked region at full detail and gets stitched back. This node is the "mask to crop" step in that pipeline, done in a way that also hands you the box as a first-class value.

How it works

Give it a mask, and it scans for the smallest axis-aligned rectangle that contains every nonzero pixel. That's it - no inference, no model, instant and deterministic. The outputs are:

  • bounding box - the rectangle as a BOUNDING_BOX value, ready to wire into whatever consumes boxes (crop nodes, regional prompters, other UC utilities like UC_Ideogram4BoundingBoxCrop).
  • cropped image - only produced when you connect the optional image input. It crops that image to the detected box, which is the "only masked region at full detail" trick from inpainting, ready to sample.

The one behavioral switch is invert (default off). When on, the node finds the box around the inverted mask - i.e. everything that isn't masked. That's the outpainting case: mask the empty border, invert, and you've boxed the content you want to keep.

Wiring it up

The bare minimum is a mask in. If you only need the coordinates, leave image disconnected and read the bounding box. If you're building a crop-and-stitch flow, connect the source image too and pipe cropped image into your sampling branch, then composite back with the original mask later.

The classic gotcha is a mask with nothing in it - all zeros. There's no nonzero pixel to bound, so the box comes back degenerate (or empty), and the crop is garbage. If your mask came out blank, fix the mask first, not the node. Also remember invert flips the semantics completely: if your crop is the background instead of the subject, that's the flag you want.

That's the whole thing. No sliders to tune, no model to download - just a clean mask-to-box conversion that makes the rest of your region-based workflow possible.

Categoryutils/mask

Inputs (3)

NameTypeDefaultDescription
maskMASK
invertBOOLEANfalse
imageoptIMAGEOptional image cropped to the detected bounding box.

Outputs (2)

NameTypeDescription
bounding boxBOUNDING_BOX
cropped imageIMAGE