Nodes/MTB Nodes/Bbox From Mask (mtb)
ComfyUI Node Runs on cloud

Bbox From Mask (mtb)

Get the tight crop box around a mask

By melMass·Created 3 years ago·Updated about a month ago· 721
Bbox From Mask (mtb)
  • mask
  • image
  • bbox
  • image (optional)
invertfalse

You've got a mask - a subject, a face, a region - and you want the tight rectangle that just contains the white part of it. That's this node. It scans the mask, finds the smallest box that wraps the masked area, and hands you back that bounding box (plus, optionally, your image already cropped to it).

This is plumbing for crop-and-process workflows, and it's a useful pattern: instead of running an expensive operation on a whole 2048px canvas when your subject occupies one corner, you find the box around the subject, crop to it, do the work at full detail on just that region, then paste it back. Detailers and inpaint helpers do this internally; Bbox From Mask lets you build it yourself with MTB's crop nodes.

How it works

Pure geometry. It reads the mask as pixels, finds the leftmost/rightmost/topmost/bottommost lit pixels, and that rectangle is your box. No model, instant.

The inputs that matter

  • mask - the mask to measure. Whatever produced it - a background remover, a segmenter, a hand-painted mask - feeds in here.
  • invert (default off) - flip the mask first. If your subject is black on a white field (the opposite of the usual convention), the box would otherwise wrap the background; turn invert on and it measures the right region.
  • image (optional) - if you also pass an image, the node crops it to the box and returns it, saving you a separate crop node.

Two outputs: bbox (a BBOX) is the coordinates, and image (optional) is your image cropped to that box if you supplied one. The BBOX is MTB's own bounding-box type - feed it into MTB's Crop From Bbox / Bbox From Mask family and, crucially, its Uncrop node, which is how you paste the processed region back into the original frame.

How to install it

ComfyUI Manager: search MTB Nodes (comfy_mtb), install, restart. Or manually: cd ComfyUI/custom_nodes && git clone https://github.com/melMass/comfy_mtb and restart. Pure Python, nothing to download.

Common issues

The one that actually bites: an empty mask. If nothing in the mask is lit (or it's fully lit), there's no meaningful box to compute, and you'll get a degenerate result - a zero-size box or the whole frame. If your crops are coming out wrong, check that the upstream mask actually contains what you think it does; a silently empty mask is the usual culprit.

The second thing to know is that BBOX here is MTB-specific. Other packs - Impact Pack, for instance - have their own bounding-box formats, and they aren't interchangeable. Keep the box inside the MTB crop nodes and you'll be fine; try to wire it into another pack's crop node and it won't match. If you need a box you defined by hand rather than derived from a mask, its sibling node Bbox (mtb) builds one from raw x/y/width/height values.

Categorymtb/crop

Inputs (3)

NameTypeDefaultDescription
maskMASK
invertBOOLEANfalse
imageoptIMAGEOptional image

Outputs (2)

NameTypeDescription
bboxBBOX
image (optional)IMAGE