Mask to BBox Mask
Per-frame bounding boxes in one node
- mask
- image
- bbox_mask
- images
- bboxes
A segmentation mask tells you where a subject is, pixel by pixel. Sometimes you don't need that granularity - you need a rectangle. Mask to BBox Mask is the bridge: for every frame, it finds the bounding box of whatever's above your threshold in the mask, and hands you back a rectangular mask, an optional overlay visualization, and a proper BOUNDING_BOX structure. It's the "detector output" half of a detailing or region workflow, minus the detector - you've already got the mask, you just want it simplified.
The three outputs, and why you'd want each
- bbox_mask - a rectangular MASK that hugs the mask's extent, per frame. Empty frames yield an empty (all-zero) box instead of a crash, which matters when you batch across a whole video and some frames simply have nothing masked. Wire this into any mask consumer.
- images - the bbox drawn over an image, if you connect the optional
imageinput. Filled by default; switchfilloff and it's an outline atoutline_thickness(default 3) pixels. This is your visual check that the box is landing where you think. - bboxes - a
BOUNDING_BOXoutput:x/y/width/heightdicts per frame. This is the compatibility hook. It speaks the same type as comfy-core's bbox nodes and Draw Gray BBox, so you can feed tracked rectangles into crop, paste, and region plumbing without a converter.
Inputs
- mask - the input mask.
- threshold - 0.5 default; pixels above this count as "present."
- padding - grows the box by this many pixels on all sides. Handy when you want the sampler to have context beyond the tight mask.
- fill / outline_thickness - control the overlay rendering.
- image - optional; connects the overlay output and also defines the output resolution when present.
Where it fits
This is the automation backbone of the masking/detailing loop: detect → crop to region → re-render → paste back. When your "detector" is actually a SAM mask or a color key or a latent-mask threshold, this node is what turns it into crop coordinates. And because it's per-frame and batch-aware, it works across a video batch, tracking a moving subject's box from frame to frame - which is exactly the shape of a video detailer pass.
Install
Part of TrentNodes:
cd ComfyUI/custom_nodes
git clone https://github.com/TrentHunter82/TrentNodes.git
cd TrentNodes
pip install -r requirements.txt
Restart, look under Trent/Masks. Pure torch, no models.
Gotchas
Two things to know. First, "bounding box" here is axis-aligned - if your mask is a diagonal sword or a U shape, the box will include a lot of dead space. That's the nature of the beast; if you need tight regions, you want the polygon, not the box. Second, when you connect an image input that's a different resolution than the mask, the output mask adopts the image's size - the bbox is computed in mask coordinates then placed at image scale. That's usually what you want (overlay matches the picture), but it can surprise you if you're mixing 512px masks with 1024px images.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| threshold | FLOAT | 0.500–1 | — |
| padding | INT | 00–4096 | — |
| fill | BOOLEAN | true | — |
| outline_thickness | INT | 31–256 | — |
| imageopt | IMAGE | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| bbox_mask | MASK | — |
| images | IMAGE | — |
| bboxes | BOUNDING_BOX | — |