Sam2Segmentation
Turn a click or a box into a clean mask
- sam2_model
- image
- bboxes
- mask
- mask
This is the node you're actually here for. Point at something in an image, and it gives you a pixel-accurate mask of it. Not "foreground vs background" - a mask of that specific object. The dog and not the couch. The left person and not the right one. That's the thing SAM2 does that a background remover can't, and it's why segmentation feeds the interesting half of ComfyUI: masked inpainting, regional prompting, compositing, detail passes.
Worth being clear on where this sits. If all you want is "subject on transparent background," this is overkill - grab BiRefNet or rembg and move on. Reach for SAM2 when you need a named region to hand to something else: inpaint just the jacket, regenerate just the face at full res, composite just the product. The mask is the deliverable; what you do with it is the workflow.
How it works
You give SAM2 the image plus one or more prompts - a point, a set of points, or a bounding box - and it predicts the mask for whatever's under them. Positive points say "include this," negative points say "not this." A box says "the object roughly in here." Under the hood SAM2 encodes the image once and the prompt cheaply, which is why it's fast and why you can nudge the mask by adding a point rather than re-running from scratch.
Where do the prompts come from? Two common routes. You place them by hand with a points editor. Or - the pattern that scales - you let a detector find the object by name and feed its box in. That's exactly what Florence2toCoordinates is for in this pack: Florence-2 detects "the red car," you convert its output to coordinates or a bbox, and SAM2 turns that into a precise mask. Text in, mask out, no clicking.
The inputs that matter
Two required, plus the optional prompts that make it do anything:
sam2_model(SAM2MODEL) - from the loader, set tosingle_imagemode. Get that wrong and this node errors.image(IMAGE) - what you're segmenting.keep_model_loaded(default false) - flip to true if you're running this repeatedly; it skips reloading the model each time at the cost of holding VRAM.
The optional prompt inputs are the point of the node:
coordinates_positive/coordinates_negative(STRING) - the include/exclude points. Wire these from a points editor or from Florence2toCoordinates'center_coordinates.bboxes(BBOX) - box prompts instead of (or alongside) points; Florence2toCoordinates emits these too.individual_objects(default false) - this one matters. Off, you get one merged mask. On, you get a separate mask per object - what you want when you're masking several things and need to treat them independently.mask(MASK) - an optional mask input to refine against.
Output is a single mask (MASK), which drops straight into your inpaint sampler, a SetLatentNoiseMask, ImageCompositeMasked, or a regional prompting node.
How to install it
ComfyUI Manager → search ComfyUI-segment-anything-2 → install → restart. Or by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-segment-anything-2
Restart afterward. You don't fetch model files for this node directly - the loader (DownloadAndLoadSAM2Model) pulls the weights. On ComfyICU the whole pack ships in the image.
Common issues
- Empty or garbage mask. Almost always the prompt. A single point on a busy object can grab the wrong sub-region - add a second positive point, or a negative point on the thing you don't want. Boxes are more forgiving than a lone point.
- "single_image" mismatch. The loader's
segmentormust besingle_imagehere.videomode won't drive this node. - Masking multiple objects but getting one blob. Turn on
individual_objects. - Coordinates in the wrong space. If you're hand-building the coordinate string, points are relative to the image you're actually feeding in - resize the image and your old coordinates point at the wrong place. Let Florence2toCoordinates derive them from the same image and this stops being your problem.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| sam2_model | SAM2MODEL | — | |
| image | IMAGE | — | |
| keep_model_loaded | BOOLEAN | false | — |
| coordinates_positiveopt | STRING | — | |
| coordinates_negativeopt | STRING | — | |
| bboxesopt | BBOX | — | |
| individual_objectsopt | BOOLEAN | false | — |
| maskopt | MASK | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |