Nodes/mask2sam/πŸ“¦ Mask to BBoxes
ComfyUI Node

πŸ“¦ Mask to BBoxes

Turn any mask into SAM2 bounding-box prompts

By GlidiasΒ·Created 11 months agoΒ·Updated 3 months agoΒ· 1
πŸ“¦ Mask to BBoxes
  • mask
  • bboxes

SAM2 (Segment Anything 2) turns rough prompts into crisp masks, but it always needs a prompt to get going - a point, a box, or text. If you already have a mask, the fastest way to hand it to SAM2 is to stop thinking and just box the thing. That's the entire job of πŸ“¦ Mask to BBoxes: read a mask, emit a bounding box for every shape in it, in exactly the format Kijai's SAM2 node wants. No more typing coordinates, no more fumbling.

It comes from Glidias/mask2sam, the same tiny pack that ships πŸ“ Mask to Points. Its reason to exist is feeding kijai/ComfyUI-segment-anything-2 - that's the thing you actually want to pair it with. The pack author built it so SAM2 workflows can be driven from Krita transparency-mask layers (or any batch of masks) instead of hand-clicking points every run. It's a niche utility, not a headline node, and it knows it.

How it works

Under the hood it's skimage's measure doing the work. For each mask in the batch it runs connected-component labeling on the foreground, then reads each region's bounding box and converts it from row/column into [x1, y1, x2, y2]. You get one list per batch item, and each list holds one box per connected blob - so a mask with three separated objects yields three boxes. Empty masks produce empty lists, and Kijai's node handles that gracefully. There's no SAM2 here, no model, no GPU work; it's pure geometry on tensors you already have.

The one input that matters

  • mask - a batch of MASK tensors. The only thing you set.

Output is a single bboxes value of type BBOX, which is the type Kijai's SAM2 segmentation node accepts directly. Wire it in and you're done.

The trap: your colors are flipped

This is the thing that will bite you on the first run. Black (0) is the foreground here, white (255) is "do nothing." That's the Krita painting convention the author works in, and it's the reverse of what most ComfyUI mask tools produce (white = object). Feed in a normal white-on-black mask and you'll get boxes around everything except your subject. If your output looks like nonsense, invert the mask before it hits this node - or check what color you actually painted.

Install

ComfyUI Manager is the easy path: search "mask2sam" and install, or clone it manually:

cd ComfyUI/custom_nodes
git clone https://github.com/Glidias/mask2sam

Then restart ComfyUI. The pack's only dependencies are scikit-image and shapely - light, CPU-only, auto-installed by Manager. There are no model downloads here; the SAM2 weights live in Kijai's pack, so if you're missing a checkpoint, that's where the fix is.

When to reach for it

Two sweet spots. First, batch and automation: if a script or a Krita workflow produces masks, this turns them into SAM2 prompts with zero human input. Second, the "individual objects" workflow: since each blob gets its own box, SAM2 can treat them as separate objects rather than one merged segmentation. A box is also a more forgiving prompt than a point for SAM2, so you can draw sloppy masks and let it clean up the edges. Keep in mind it's box-precise, not pixel-precise - the mask inside each box is SAM2's job, not this node's.

One tip: a speckle of noise in your mask becomes a box in your output. If you see boxes everywhere, clean the mask (small-blob removal, or just paint tidy) before feeding it. Garbage in, twenty little rectangles out.

CategorySAM2/utils

Inputs (1)

NameTypeDefaultDescription
maskMASKβ€”

Outputs (1)

NameTypeDescription
bboxesBBOXβ€”