Nodes/Kongshan Nodes/SAM 按框分割
ComfyUI Node

SAM 按框分割

Turn Detection Boxes Into Clean Per-Object Masks With SAM

By kongshan4219·Created 3 months ago·Updated 3 months ago· 0
SAM 按框分割
  • sam_model
  • image
  • boxes
  • segmented_images
  • masks

Bounding boxes are rectangles; products are not. KSSAMSegmentByBoxes is the node that fixes that: it takes the boxes from KSGroundingDinoDetect, feeds each one to a loaded SAM model as a box prompt, and gets back a precise mask that follows the product's actual outline - then cuts each masked instance out as its own image. Rectangles in, clean product crops out.

Why the two-step dance

This is the classic GroundingDINO + SAM pairing that the KB's masking-detection-detailing essay describes, and the division of labor is the whole trick:

  • GroundingDINO is a detector: it knows what "bottle" means and where bottles are, but it hands you a rectangle.
  • SAM is a segmenter: it knows nothing about "bottle," but you hand it a rectangle and it traces the exact object boundary inside it.

So the detector decides where, SAM decides what shape. The result is a mask that hugs the product - no background bleeding in at the edges - which is exactly what you want before compositing onto white or transparent. SAM's image encoder runs once per image and the box prompts are cheap, so segmenting five boxes on one photo costs far less than five fresh SAM calls.

The inputs

  • sam_model - from KSSAMModelLoader. Bigger model, cleaner edges, more VRAM.
  • image - the original image batch. Must match the images the boxes came from.
  • boxes - the KS_DINO_BOXES output of KSGroundingDinoDetect. One list per image.

Outputs: segmented_images (each instance cut out; the background of each crop is black/transparent) and masks (one mask per instance, batch-aligned). Wire masks into KSProductsToBackground or KSApplyMaskBackground for the white-background step.

One detail worth knowing: if detection found zero boxes, the node returns an empty black image and empty mask placeholder rather than erroring - so a workflow can keep running and a later node like KSIsMaskEmpty can make the decision.

Installing

With the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes

restart ComfyUI. No keys needed - fully local, but it needs the SAM weights downloaded on first use (models/sams/).

Gotchas

  • Boxes from another pack won't connect. KS_DINO_BOXES is this pack's private type; if you detected with Impact Pack or another GroundingDINO node, you can't plug those boxes in here. Either stay inside the pack or do the crop yourself.
  • The crops are full-canvas, not tight. Each "segmented image" is the full image with everything but the instance zeroed out, not a tight crop. That's intentional - the tightening happens in KSProductsToBackground (which centers and crops to a square). If you expected small crops straight out, that's a later step in this pipeline.
  • VRAM is real. sam_vit_h + GroundingDINO on the same card adds up. If you OOM, drop to sam_vit_b before touching any settings.

This node is the quiet star of the pack's local half: two open models you already know, wired the way everyone ends up wiring them, with the crop/center step handled for you afterward. Young pack, zero impressions everywhere as of now, but this particular node is one you could rebuild yourself in an afternoon - which is also the best way to appreciate why the author bothered.

CategoryKongshan/Local

Inputs (3)

NameTypeDefaultDescription
sam_modelSAM_MODEL已加载的 SAM 模型。模型越大通常边缘越准,防爆和显存占用更高。
imageIMAGE原始图片批次,应与 boxes 所属图片保持一致。
boxesKS_DINO_BOXESGroundingDINO 输出的检测框列表。无框时节点会输出空遮罩占位。

Outputs (2)

NameTypeDescription
segmented_imagesIMAGE
masksMASK