ComfyUI Node

Mask To Bbox (SAM2)

Mask to bounding box without a single SAM2 call

By lihaoyun6·Created 11 months ago·Updated 11 months ago· 39
Mask To Bbox (SAM2)
  • mask
  • image
  • bboxes
  • image (optional)
invertfalse

The name is a lie, and it's a good one: Mask To Bbox (SAM2) doesn't call SAM2, doesn't load any model, and doesn't need an API key. It computes a bounding box around the white region of a mask and hands it to you as a BBOX. The "SAM2" in the name is a promise about what you'll do next - SAM2 workflows eat box prompts, and this node is the cheapest way to feed them one. Your segformer mask becomes a crop box in one hop.

How it works

Read the source and it's almost embarrassing: torch.nonzero(mask) to find every white pixel, take the min and max coordinates, and you have your [x1, y1, x2, y2]. That's the whole mechanism - no learned model, no magic, which is exactly why it's instant and works on any mask from anywhere. The invert flag flips which side counts as "the subject" before the search, so a subject-on-white mask and a white-on-black mask both give you the same box without you having to flip the mask yourself.

There are two quirks worth knowing before you build around it. First, with a batch of masks (a 3D MASK, as video gives you), it returns one bbox per frame as a list - which is the right shape for video SAM2 workflows. Second, the crop output only ever crops the first bbox; feed a batch and you'll get per-frame boxes but a crop of just frame zero. If your downstream only needs boxes, neither matters.

Inputs and outputs

  • mask - the required input; any MASK will do, segformer output or otherwise.
  • invert - boolean, default off. Flip it if your mask has the subject on black rather than white.
  • image - optional (tooltip: "Optional image"). Give it your original image and the node also returns the crop.

Two outputs: bboxes, typed BBOX, holding the [x1, y1, x2, y2] list - wire it into SAM2 box prompts, Impact Pack detector inputs, or anything that eats bounding boxes. And image (optional), the cropped region when you supplied an image. Feed a mask with no white pixels and you get an empty bbox list plus a console warning - that's the "mask is empty" case, not a hang.

Installing it

Same pack, same install. ComfyUI Manager → search "Segformer Ultra Fast" → Install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/lihaoyun6/ComfyUI-Segformer_Ultra_Fast

This is the cheapest node in the pack: no model downloads, no new dependencies beyond what the pack already pulls in (transformers, kornia, numpy, pillow, huggingface_hub). You could even lift the function into a custom script - but the node gives you the standard BBOX type, which is what ComfyUI's SAM-family tools expect.

Common issues

  • Empty bbox list - your mask is all black; check the invert flag or fix the mask upstream.
  • Wrong box - inverted mask semantics; flip invert.
  • Unexpected crop with batches - remember the crop uses only the first bbox; use the bboxes output for per-frame logic.

For the typical flow: Segformer B2 Clothes Ultra-Fast → mask → this node → SAM2 (or a regional pass). It's a three-node pipeline that replaces manual cropping entirely.

CategorySegformer Ultra-Fast/Crop

Inputs (3)

NameTypeDefaultDescription
maskMASK
invertBOOLEANfalse
imageoptIMAGEOptional image

Outputs (2)

NameTypeDescription
bboxesBBOX
image (optional)IMAGE