Mask to BBox
Turn a mask back into a box — the reverse adapter you didn't know you needed
- mask
- bbox
- x
- y
- w
- h
Mask to BBox is the exact reverse of BBox to Mask, and it exists because ComfyUI is full of nodes that output masks and only a few that output proper BBOX objects. Segment anything, BiRefNet-style cutouts, manual mask painting - they all end with a mask tensor, and the next node in your chain wants a bounding box.
The concrete case: a detector that emits a mask instead of a box. You've got a mask of where the face (or hand, or car, or whatever) is, and you want to feed a box into a crop node, a detailer, or one of the bbox-to-query converters. That's what this node is for - it measures the mask and hands you a tight box around it.
How it works
It's honest pixel math. It finds every pixel in the mask above a 0.5 threshold, takes the min/max row and column, and builds a tight [[x, y, width, height]] box in XYWH form. Because the box hugs the actual mask outline, it handles irregular shapes fine - an L-shaped or non-convex mask just gets the smallest rectangle that contains it. All coordinates are floored to integers so they wire cleanly into crop and paste nodes.
A few behaviors worth knowing:
- Batched masks (a
MASKwith a batch dimension) - it uses the first one and ignores the rest. - Empty mask - you get
[[0, 0, 0, 0]]and zeros, not an error. Debuggable, at least. - Width/height are computed inclusive of the max pixel (
x_max - x_min + 1), so a box drawn over a mask's exact boundary doesn't end up one pixel short.
Inputs and outputs
Just one input: mask (MASK). Outputs are bbox (BBOX), plus x, y, w, h as separate INTs - grab whichever shape fits what you're wiring into.
Where it shines
The pack's own example workflow chains it straight into BBox to SAM3 Query: mask → Mask to BBox → BBox to SAM3 Query → refined segmentation. You take a mask you already have, convert it to a box, and use that box as the query to re-segment the region with SAM3. It's the loop that turns "I already found it once" into "let's find it again, but better."
It also rounds out the bbox toolkit: Detection Query gives you boxes from JSON, BBoxes to Mask turns boxes into masks, and this closes the circle for anything that skips the box and goes straight to a mask.
Installing it
It's part of ComfyUI-JK-TextTools. Install via ComfyUI Manager (search "JK-TextTools") or:
cd ComfyUI/custom_nodes
git clone https://github.com/Nakamura2828/ComfyUI-JK-TextTools.git
Restart ComfyUI. No models to download, no runtime pip deps beyond what ComfyUI already needs.
The gotcha
Don't expect it to handle multi-object masks nicely - a mask containing three separate blobs gets one bounding box that spans all of them. If you need per-object boxes, split the mask into individual masks first (the pack's BBoxes to Mask and SEGs to Mask outputs are already per-object) and run each through this node.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| bbox | BBOX | — |
| x | INT | — |
| y | INT | — |
| w | INT | — |
| h | INT | — |