Nodes/Comfy_KepMatteAnything/MatteAnything_SAMMaskFromBoxes
ComfyUI Node

MatteAnything_SAMMaskFromBoxes

Turn text-prompted boxes into a crisp mask

By M1kep·Created 3 years ago·Updated 2 years ago· 11
MatteAnything_SAMMaskFromBoxes
  • sam_predictor
  • boxes
  • MASK

GroundingDINO tells you where the thing is. This node tells you what it looks like. MatteAnything_SAMMaskFromBoxes is the handoff where a bounding box becomes a precise, object-hugging mask - the moment the pipeline stops being a detector and becomes a segmenter. It's the SAM payoff, and it's the node that produces the silhouette everything else refines.

How it works

It takes the SAM_PREDICTOR from MatteAnything_InitSamPredictor and the DINO_BOXES from MatteAnything_DinoBoxes, stacks the boxes, and calls SAM's predict_torch with multimask_output=False. That last flag is the interesting one: SAM normally hedges by returning several candidate masks, but this pack asks for exactly one per box. For the two-step GroundingDINO+SAM dance, that's the right call - the box has already told SAM roughly what you want, so you don't need the menu of interpretations.

The output is a single MASK at full image resolution, one per detected box. Wire it into MatteAnything_ToTrimap and you're on the road to an actual alpha matte. Wire it straight into a compositor and you get a hard-edged cutout - which is sometimes exactly what you want, and sometimes a lie about the subject's edges.

Inputs and outputs

  • sam_predictor - from MatteAnything_InitSamPredictor. Same image it was built with, or the mask won't line up with your frame.
  • boxes - the DINO_BOXES from MatteAnything_DinoBoxes.
  • Output: one MASK.

That's the whole node. The interesting decisions all happen in the nodes feeding it.

The empty-detection failure

Here's the failure mode that will confuse you the first time. If DinoBoxes matched nothing - vague caption, hard image - it hands this node zero boxes. SAM's predict_torch gets an empty batch and the node dies mid-graph with a stack trace that says absolutely nothing about a missing detection. The error looks like a SAM bug. It isn't; your caption just failed. Back up, reword the caption, and watch the annotated image from DinoBoxes to confirm a red box actually appeared before you debug this node.

Installing this pack

ComfyUI Manager, search Comfy_KepMatteAnything, install. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/M1kep/Comfy_KepMatteAnything

then restart ComfyUI. It's the standard pack dependency trio (segment-anything, detectron2, groundingdino-py) plus the SAM weights in models/sams/:

mkdir -p ComfyUI/models/sams
# sam_vit_h_4b8939.pth into ComfyUI/models/sams/

Gotchas

  • Hard edges are a feature of this step, not of matting. The mask from SAM is binary. If your subject has hair or glass, don't judge this node's output as the final result - that's literally why the pack adds ViTMatte on top.
  • Multiple boxes, multiple masks. A caption that matches three objects returns three masks stacked into one MASK. If you wanted just one, fix the caption upstream, not here.

Honest take: this node is the boring, reliable bridge in a pipeline full of moving parts. It's where the phrase "Matte Anything" starts being true - GroundingDINO finds anything you can name, and SAM carves it out. Just remember the mask it hands you is the draft, not the final cut.

CategoryMatte Anything

Inputs (2)

NameTypeDefaultDescription
sam_predictorSAM_PREDICTOR
boxesDINO_BOXES

Outputs (1)

NameTypeDescription
MASKMASK