Mask to BBox
Turn any mask into the rectangle SAM and detailers actually want
- mask
- BBOX
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 viascipy.ndimage.label, then a Non-Maximum Suppression pass merges boxes whose IoU is at or abovenms_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.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| threshold | FLOAT | 0.500–1 | Threshold to binarize the mask before finding bbox |
| combine | BOOLEAN | false | If True, combine all mask regions into one bbox. If False, output separate bboxes for each connected component. |
| nms_threshold | FLOAT | 0.500–1 | IoU threshold for Non-Maximum Suppression. Higher values merge more overlapping boxes. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| BBOX | BBOX | — |