Nodes/ComfyUI-CustomNodePacks/SAM Mask Generator — Points + BBox + Text
ComfyUI Node

SAM Mask Generator — Points + BBox + Text

Point, box, or words — this is the SAM masking workhorse

By Code2Collapse·Created 6 months ago·Updated a day ago· 52
SAM Mask Generator — Points + BBox + Text
  • sam_model
  • image
  • bbox
  • existing_mask
  • mask
  • all_masks
  • detected_bbox
  • score
  • info
points_json[]
bbox_json
text_prompt
negative_text_prompt
grounding_modelnone
text_threshold0.25
text_box_threshold0.30
multimask_outputtrue
mask_index0
score_threshold0.00
apply_bbox_cropfalse
refine_iterations1
auto_negative_pointsfalse

SAM's whole deal is that it turns a prompt into a mask for exactly the object you meant - a click, a box, or (from SAM 3) a phrase - with zero training for your specific subject. The KB sums it up well: SAM is the masking backbone this ecosystem reaches for when it wants a specific object selected, not when it wants the foreground cut out. SAM Mask Generator (MEC) is the node that runs that inference, and it's the most complete SAM prompt surface in the pack: points, bounding boxes, and text prompts, plus iterative refinement to tighten the result.

The mechanism

The mechanism is standard SAM with refinements bolted on. Your prompts go in as JSON - points_json as an array of {"x":..., "y":..., "label":1} objects (label 1 = foreground, 0 = background), bbox_json as [x1, y1, x2, y2] - and SAM's prompt encoder + mask decoder return one or three candidate masks. The refinement loop is where this node earns its keep: refine_iterations (default 1, 2–3 recommended by the tooltip) feeds each mask back into SAM with prompts derived from it, tightening boundaries with every pass. auto_negative_points samples negative points just outside the mask to suppress bleed in cluttered scenes.

Text prompting

Text prompting is the flashy feature: text_prompt ("person", "dog") routes through a GroundingDINO model - you pick it in grounding_model (default none, which disables text prompting) - which turns the phrase into a bounding box that SAM then segments. negative_text_prompt does the inverse: detects unwanted regions and generates negative points from them. This is exactly the Grounded-SAM pattern the KB's detailing doc describes - GroundingDINO finds the thing you named, SAM makes it precise - and it's why you can go from "type the object" to "mask" without drawing anything. The text_threshold (0.25) and text_box_threshold (0.3) are the grounding confidence cuts.

Outputs, and the pipeline extras

Outputs: mask (the selected candidate per mask_index), all_masks (all candidates when multimask_output is on), detected_bbox (a BBOX derived from the mask - wireable into the pack's bbox nodes), score (SAM's confidence), and info (JSON of prompts, scores, refinement steps). score_threshold (0 = off) lets you discard weak masks outright.

Two optional inputs matter for pipeline work: bbox from an upstream BBox node overrides bbox_json, and existing_mask lets you skip the first SAM pass and start refinement from a mask you already have - handy for iterating on a roughly-painted selection. It also respects the loader's VRAM offload: the node moves the model to GPU for inference and back, so you can chain SAM nodes on a tight card.

Install

Install: SAM needs the model loaded by SAMModelLoaderMEC (SAM 2.1 or SAM 3), plus pip install git+https://github.com/facebookresearch/sam2.git for the runtime, and GroundingDINO if you want text prompts. Pack itself is clone Code2Collapse/ComfyUI-CustomNodePacks into custom_nodes or Manager → "CustomNodePacks", weights in models/sams/ (SAM3) and models/sam2/ (2.1). The recurring community pain is the SAM 3 Triton requirement on Windows - the KB flags it as a real obstacle; if you hit it, SAM 2.1 via the loader works without it.

CategoryC2C/SAM

Inputs (17)

NameTypeDefaultDescription
sam_modelSAM_MODELLoaded SAM model from SAM Model Loader
imageIMAGEInput image to segment (first frame is used)
points_jsonSTRING[]JSON array: [{"x":100,"y":200,"label":1}, ...]. label=1=foreground, label=0=background.
bbox_jsonSTRINGBounding box as JSON: [x1, y1, x2, y2] or {"x":..,"y":..,"w":..,"h":..}. Leave empty to use only point prompts.
text_promptSTRINGText description of target object (e.g. 'person', 'dog', 'car'). Requires a GroundingDINO model. Converts text to bounding box, then feeds to SAM for precise mask generation.
negative_text_promptSTRINGText description of objects to EXCLUDE (e.g. 'background', 'wall'). Uses GroundingDINO to detect these regions, then generates negative points from them to suppress unwanted areas in the mask.
grounding_modelCOMBOnoneGroundingDINO model for text-to-bbox grounding. Set to 'none' to disable text prompting.
text_thresholdFLOAT0.250–1GroundingDINO box confidence threshold.
text_box_thresholdFLOAT0.300–1GroundingDINO text-box association threshold.
multimask_outputBOOLEANtrueReturn 3 candidate masks (SAM default) vs 1
mask_indexINT00–2Which mask to return when multimask=True (0=best score)
score_thresholdFLOAT0.000–1Discard masks below this confidence score
apply_bbox_cropBOOLEANfalseCrop output to bbox region
refine_iterationsINT11–5Iterative refinement passes. Each pass feeds the previous mask back into SAM with augmented prompts. 2-3 significantly improves accuracy.
auto_negative_pointsBOOLEANfalseAutomatically sample negative points just outside the mask boundary. Helps in cluttered scenes and similar-color backgrounds.
bboxoptBBOXBounding box from BBox node (overrides bbox_json)
existing_maskoptMASKUse this mask as the starting point instead of running SAM from scratch

Outputs (5)

NameTypeDescription
maskMASKSelected mask for the chosen mask_index.
all_masksMASKAll candidate masks returned by SAM (when multimask_output is True).
detected_bboxBBOXBounding box derived from the selected mask.
scoreFLOATSAM confidence score of the selected mask.
infoSTRINGJSON summary of prompts, scores, and refinement steps.