Mask to Bounding Box
The Little Node That Feeds the Whole Bounding-Box Toolkit
- mask
- BBOX
Half of ComfyUI's segmentation outputs are masks, and half of what you want to do next needs a rectangle. MaskToBBox is the bridge: it takes any MASK and returns a BBOX - the rectangular bounding box that encloses the white (nonzero) region.
It looks trivial, and it kind of is, but it's the front door to the bbox subsystem inside Duanyll Nodepack. Mask → bbox → crop, expand, draw, or fill is a surprisingly common chain, and this node is where it starts. You'll reach for it when a face-detect or segmentation mask needs to become a crop region, or when you want a tight box around something the model already found for you.
How it works
The mechanism is exactly as simple as it looks: the mask tensor is thresholded at 0.5, the result is projected onto the x and y axes, and the min/max of the nonzero pixels become the bbox corners. The result is a BoundingBox object (with your label string attached) that flows into every other BBOX-typed node in the pack.
One behavior worth knowing: if the mask is empty, instead of erroring it returns the full-image bbox (0,0 to width,height). That's a deliberate choice that keeps workflows alive when a detection whiffs - your pipeline gets a "whole image" box rather than a crash. Whether that's what you want depends on the job; it's handy for "if nothing found, process everything" logic.
Inputs and outputs
- mask (MASK) - the tensor to box up. Must be 2D; a batch or channel dimension throws a clear error.
- label (STRING, default
object) - the tag carried on the box, which shows up later if you draw the bbox with a label or hand it to something that names regions.
Output is a single BBOX.
Where it fits
Wire the output into duanyll::BBoxCrop to cut the region out, duanyll::ExpandBBoxByRatio to loosen it before cropping, or duanyll::DrawBBox to visualize. Because it emits the pack's custom BBOX type, it only talks to this pack's own bbox nodes - there's no adapter to Impact Pack's SEGS or core Comfy types built in here (though the pack does ship a separate BBoxesToImpactPackSegs for that).
Installing it
Same story as every node in this pack:
cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/duanyll_nodepack
or search "Duanyll Nodepack" in ComfyUI Manager. No extra model downloads - this one runs on pure tensors, no weights involved.
Gotchas
Not much to trip on. Keep the mask 2D, remember the empty-mask = full-image behavior, and know that label is free text - it's how you tell one box from another when you're drawing several. If you see a "must be a 2D tensor" error, squeeze the mask or make sure you're not feeding an RGBA-style batch. That's the whole list.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| label | STRING | object | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| BBOX | BBOX | — |