Nodes/comfyui_imgutils/Mask to BBox
ComfyUI Node

Mask to BBox

Turn any mask into the rectangle SAM and detailers actually want

By LK-168·Created about a year ago·Updated about a year ago· 1
Mask to BBox
  • mask
  • BBOX
threshold0.50
combinefalse
nms_threshold0.50

The mask-to-box adapter

ComfyUI is full of places that want a rectangle where you have a mask. SAM's box prompts, Impact Pack's DetailerForEach machinery, crop nodes - they all speak [x0, y0, x1, y1], not blobby white pixels. Mask to BBox is the adapter: feed it a mask and it hands you the bounding box (or boxes) that snugly fit it. It's the least glamorous node in this pack and the one that keeps showing up in real workflows, because masks are easy to generate and rectangles are easy to feed to things.

How it works

It's textbook computer vision, no models involved. The mask gets binarized at threshold (anything above it counts as foreground), then:

  • combine = true - one box around everything, computed from the min/max rows and columns that contain any mask pixel. The classic "tight crop around the whole subject" move.
  • combine = false - each connected component gets its own box via scipy.ndimage.label, then a Non-Maximum Suppression pass merges boxes whose IoU is at or above nms_threshold. That's your "five separate objects, five boxes" mode.

NMS detail worth knowing: when boxes merge, the result is the outer bounds of the group and confidence is the max of the merged boxes. Higher nms_threshold merges more aggressively.

Inputs and outputs

The three knobs are threshold (default 0.5, 0–1), combine (default false), and nms_threshold (default 0.5). Output is a single BBOX socket. Beginner move: flip combine to true until you have a reason not to. The false + NMS path is for when you've got multiple distinct regions and want one box per object.

Where it wires: this is the natural supplier for SAM Predictor's bbox input - the tooltip there literally says "Connect a MaskToBBoxNode output or similar." It also feeds BBoxFilter for filtering, and BBox to Mask to round-trip back.

Install

Part of LK-168/comfyui_imgutils. ComfyUI Manager → search "comfyui_imgutils", or:

cd ComfyUI/custom_nodes
git clone https://github.com/LK-168/comfyui_imgutils

Restart ComfyUI. Pure mask math, so no model downloads needed for this node itself; the detector/SAM siblings in the pack do download to $HF_HOME on first use.

Gotchas

  • Batch inputs. If you feed it a batch of masks it processes the first one only and prints a console warning. One mask in, one (or several) boxes out.
  • Empty mask. A fully transparent mask produces a zero-size box [0, 0, 0, 0] (or an empty list in separate mode) plus a warning. Check with Mask Info if you suspect this - a silent [0,0,0,0] downstream is a confusing thing to debug.
  • Boxes include the whole connected region. If two characters touch at the shoulders, their masks merge into one component and you get one box. That's not a bug; it's the nature of connected-component labeling.

Simple node, boring job, and it quietly unlocks half the detailing pipelines people build with this pack.

Categoryimgutils/bbox

Inputs (4)

NameTypeDefaultDescription
maskMASK
thresholdFLOAT0.500–1Threshold to binarize the mask before finding bbox
combineBOOLEANfalseIf True, combine all mask regions into one bbox. If False, output separate bboxes for each connected component.
nms_thresholdFLOAT0.500–1IoU threshold for Non-Maximum Suppression. Higher values merge more overlapping boxes.

Outputs (1)

NameTypeDescription
BBOXBBOX