SAM Mask Generator — Points + BBox + Text
Point, box, or words — this is the SAM masking workhorse
- sam_model
- image
- bbox
- existing_mask
- mask
- all_masks
- detected_bbox
- score
- info
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.
Inputs (17)
| Name | Type | Default | Description |
|---|---|---|---|
| sam_model | SAM_MODEL | Loaded SAM model from SAM Model Loader | |
| image | IMAGE | Input image to segment (first frame is used) | |
| points_json | STRING | [] | JSON array: [{"x":100,"y":200,"label":1}, ...]. label=1=foreground, label=0=background. |
| bbox_json | STRING | Bounding box as JSON: [x1, y1, x2, y2] or {"x":..,"y":..,"w":..,"h":..}. Leave empty to use only point prompts. | |
| text_prompt | STRING | Text 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_prompt | STRING | Text 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_model | COMBO | none | GroundingDINO model for text-to-bbox grounding. Set to 'none' to disable text prompting. |
| text_threshold | FLOAT | 0.250–1 | GroundingDINO box confidence threshold. |
| text_box_threshold | FLOAT | 0.300–1 | GroundingDINO text-box association threshold. |
| multimask_output | BOOLEAN | true | Return 3 candidate masks (SAM default) vs 1 |
| mask_index | INT | 00–2 | Which mask to return when multimask=True (0=best score) |
| score_threshold | FLOAT | 0.000–1 | Discard masks below this confidence score |
| apply_bbox_crop | BOOLEAN | false | Crop output to bbox region |
| refine_iterations | INT | 11–5 | Iterative refinement passes. Each pass feeds the previous mask back into SAM with augmented prompts. 2-3 significantly improves accuracy. |
| auto_negative_points | BOOLEAN | false | Automatically sample negative points just outside the mask boundary. Helps in cluttered scenes and similar-color backgrounds. |
| bboxopt | BBOX | Bounding box from BBox node (overrides bbox_json) | |
| existing_maskopt | MASK | Use this mask as the starting point instead of running SAM from scratch |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| mask | MASK | Selected mask for the chosen mask_index. |
| all_masks | MASK | All candidate masks returned by SAM (when multimask_output is True). |
| detected_bbox | BBOX | Bounding box derived from the selected mask. |
| score | FLOAT | SAM confidence score of the selected mask. |
| info | STRING | JSON summary of prompts, scores, and refinement steps. |