ComfyUI Node

Sam2 Segment

When a Bounding Box Isn't a Mask Yet

By PozzettiAndrea·Created 10 months ago·Updated about 20 hours ago· 47
Sam2 Segment
  • sam2_model
  • image
  • bboxes
  • mask
  • mask
keep_model_loadedfalse
coordinates_positive
coordinates_negative
individual_objectsfalse
mask_threshold0.0
max_hole_area0
max_sprinkle_area0

A bounding box is a rectangle. A person is rarely a rectangle. The whole point of this pack's two-stage pipeline is that the detector gives you a box and then Sam2 Segment carves out the actual pixels inside it using Meta's SAM2. If you've ever fed a coarse detection box straight into an inpaint and watched it chew up the background around your subject, this is the node that fixes that.

It's the second half of the classic "GroundingDINO finds it, SAM masks it" pattern that's been standard in ComfyUI since the SD days - the knowledge base's background-removal essay calls targeted selection like this the right tool whenever you need a specific object, not just foreground-vs-background. ComfyUI-Grounding just bundles both halves into one install.

How it works

You feed it a sam2_model from the SAM2Model (down)Loader, an image, and one of three kinds of prompt: bboxes from a detector, positive/negative point coordinates, or an existing mask to refine. SAM2 runs its transformer-based segmentation and returns a clean binary mask. The model's segmentor (single_image, video, or automaskgenerator) was baked in at load time - and note that automaskgenerator models can't actually be used here; the node raises an error pointing you to a separate auto-mask node instead.

The inputs that matter

  • sam2_model + image - wire in the model from the loader and whatever frame you're segmenting.
  • bboxes - connect GroundingDetector's output here and you get one mask per detected object. This is the easy path.
  • coordinates_positive / coordinates_negative - JSON strings of click points, like [{'x': 100, 'y': 200}]. Powerful but fiddly; most people live on bboxes.
  • keep_model_loaded - defaults to off on this node (unlike the loaders). Each run offloads SAM2 to CPU, which is the right call when you're also holding Flux in VRAM.
  • mask_threshold - converts SAM2's raw logits to a binary mask. 0 is the default; push positive to be stricter, negative to keep more.
  • max_hole_area / max_sprinkle_area - auto-fill holes or drop stray specks, in percent of image area. Cheap cleanup that beats doing it by hand.

Output is a single mask, which plugs into anything that takes a MASK - inpainting, region cropping, compositing.

Installing it

Same story as every node in this pack: install once, reuse everywhere.

cd ComfyUI/custom_nodes/
git clone https://github.com/PozzettiAndrea/ComfyUI-Grounding
cd ComfyUI-Grounding
pip install -r requirements.txt

Or just search "Grounding" in ComfyUI Manager and hit install. The SAM2 weights aren't bundled - on first use the loader downloads the one you picked from Kijai/sam2-safetensors into ComfyUI/models/sam2. That's a couple of GB for the big variants, so the first run is slow and subsequent ones are instant thanks to the pack's model cache.

Where people get burned

  • CPU + fp16 - SAM2 will refuse to load fp16/bf16 on CPU; pick fp32 in the loader if you're not on CUDA.
  • SAM2 2.0 + video + bboxes - the video segmentor won't take bboxes on the 2.0 line; use 2.1 or points.
  • GIMM-VFI conflicts - the README warns this pack (SAM2/Florence-2 modify global CUDA/tf32 state) can't reliably coexist with kijai's GIMM-VFI frame-interpolation pack. If you see random import errors and you have both, that's the first thing to suspect.
CategorySAM2

Inputs (11)

NameTypeDefaultDescription
sam2_modelSAM2MODEL
imageIMAGE
keep_model_loadedBOOLEANfalse
coordinates_positiveoptSTRING
coordinates_negativeoptSTRING
bboxesoptBBOX
individual_objectsoptBOOLEANfalse
maskoptMASK
mask_thresholdoptFLOAT0.0-10–10Threshold for converting mask logits to binary. Positive=stricter, negative=looser
max_hole_areaoptFLOAT00–100Fill holes smaller than this area (0=disabled)
max_sprinkle_areaoptFLOAT00–100Remove isolated regions smaller than this area (0=disabled)

Outputs (1)

NameTypeDescription
maskMASK