Nodes/ComfyUI-JK-TextTools/SEGS to SAM3 Query
ComfyUI Node

SEGS to SAM3 Query

Refine your SAM3 segmentation with a query built from its own output

By Nakamura2828·Created 8 months ago·Updated 8 months ago· 0
SEGS to SAM3 Query
  • segs
  • box_sam3
  • point_sam3
  • box_tbg_sam3
  • point_tbg_sam3
prompt_typepositive

SEGs to SAM3 Query is the loop node for SAM3-based segmentation: it takes the SEGS output of a segmentation pass and converts it into the box and point queries that SAM3's selector nodes accept, so you can feed the result back in for a second, sharper pass. "I segmented it once, now segment it again but better."

It's the SEGS-flavored twin of BBox to SAM3 Query. The difference is the source: instead of one manually-provided box, it derives everything from the segmentation you already have. That makes it the natural tail of a refine loop - SAM3 gives you rough segments, this converts them into precise prompts, the selector re-segments with those prompts, and you iterate until the mask is right.

How it works

It takes the SEGS tuple ((height, width), [SEG(...), ...]) and does three things:

  1. Reconstructs the full mask by splicing every segment's cropped_mask into its crop_region on a full-size canvas - handling both Impact Pack and TBG SAM3 SEGS, numpy and tensor masks.
  2. Computes a tight bounding box around the combined mask.
  3. Computes a weighted centroid for the point query - a center that reflects where the mask mass actually is, not just the geometric middle, which makes for a more accurate SAM3 point prompt.

Then it emits both query flavors:

  • box_sam3 / point_sam3 - SAM3-native normalized prompts (0–1 coords plus label flags).
  • box_tbg_sam3 / point_tbg_sam3 - TBG SAM3 Selector JSON strings, absolute pixel coordinates like [{"x1": ..., "y1": ..., "x2": ..., "y2": ...}].

prompt_type (positive/negative) flips the label flags. Because the dimensions come from the SEGS itself, the SAM3-native outputs are always properly normalized - no "set width/height manually" trap like its bbox sibling.

Inputs and outputs

  • segs (SEGS) - required, from a SAM3 segmentation node or Impact-style SEGS detector.
  • prompt_type (positive / negative, default positive).

Outputs: box_sam3, point_sam3, box_tbg_sam3, point_tbg_sam3.

The refine loop

TBG SAM3 Segmentation
  → segs
  → SEGs to SAM3 Query
  → box_query / point_query → TBG SAM3 Selector
  → refined segmentation

Empty or invalid SEGS input doesn't crash - you get empty arrays ([]) back, which is a reasonable "nothing to query" signal.

Installing it

It's part of ComfyUI-JK-TextTools: ComfyUI Manager → search "JK-TextTools" → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/Nakamura2828/ComfyUI-JK-TextTools.git

Restart ComfyUI. The pack adds no models or runtime deps - but the SAM3 model behind all this does need its own access approval and download, so factor that into the workflow you're building.

Gotchas

This node feeds a selector, it doesn't segment anything itself - if you wire the outputs to something that isn't a SAM3-compatible selector, they're inert strings. And the union mask is built from all segments; if you only want to refine one class, filter the SEGS first (its sibling SEGs to Mask has the label filtering) or the query box will span everything.

CategoryJK-TextTools/segs

Inputs (2)

NameTypeDefaultDescription
segsSEGS
prompt_typeoptCOMBOpositive2 options: positive, negative

Outputs (4)

NameTypeDescription
box_sam3SAM3_BOXES_PROMPT
point_sam3SAM3_POINTS_PROMPT
box_tbg_sam3STRING
point_tbg_sam3STRING