Nodes/ComfyUI Impact Pack/SAMDetector (segmented)
ComfyUI Node Runs on cloud

SAMDetector (segmented)

SAM masks from your detections, kept separate

By ltdrdata·Created 4 years ago·Updated about 7 hours ago· 3,397
SAMDetector (segmented)
  • sam_model
  • segs
  • image
  • combined_mask
  • batch_masks
◄detection_hint▾►
◄dilation0►
◄threshold0.93►
◄bbox_expansion0►
◄mask_hint_threshold0.70►
◄mask_hint_use_negative▾►

SAMDetector (segmented) uses Meta's Segment Anything Model to carve precise masks out of the regions you already detected - and unlike its "combined" sibling, it keeps those masks separate instead of merging them into one. You feed it a rough location (your SEGS, usually from a bounding-box detector) and SAM tightens that into an accurate silhouette that hugs the actual subject. The "segmented" part means you get the results split apart, which is what you want when you're going to treat each region differently.

Why bother? Because a box is a blunt instrument. Impact Pack's core move is detect-crop-refine, and the quality of the crop depends on the quality of the mask. A rectangle around a face includes background corners; when you refine and composite that back, the seams show. SAM traces the real outline instead, so the refined region blends into the original with far less visible edge. This is the same reason the community moved toward segmentation-shaped masks generally - the mask following the subject's contour is what kills the tell-tale box seam. The README's own showcase uses exactly this: detect a face with a bbox detector, then use a SAM detector to find the segment inside it, giving a mask that's precise instead of rectangular.

The inputs that matter

There are several knobs, but the ones that move the needle:

  • sam_model - a loaded SAM model (from Impact's SAMLoader). Required; this is the engine.
  • segs - the regions to refine into masks. SAM doesn't scan the whole image blindly here; it works from your detections.
  • image - the picture the masks are cut from.
  • detection_hint (an enum: center-1, horizontal-2, rect-4, mask-area, and so on) - how SAM is prompted inside each region. This tells SAM where to sample its point/box prompts. center-1 (a single center point) is a sane default; the multi-point and rect hints help on awkward shapes. If a mask comes out wrong, this is a good knob to try.
  • dilation and bbox_expansion - grow the mask or the search box outward for a little margin.
  • threshold (default 0.93) - SAM's confidence cutoff for what counts as part of the segment.

Two outputs: combined_mask, a single unified mask of everything, and batch_masks, the detected segments grouped as a batch so you can work with them separately. Heads up from the pack's own docs: batch_masks groups segments in arbitrary sets (the maintainer notes they're currently grouped roughly in threes and may not be fully separated), so it "provides some level of separation" rather than one-perfect-mask-per-subject. If you need clean per-subject masks, don't assume this hands them to you pristine - verify.

When to reach for the "segmented" version

Use combined when you just want one accurate mask of everything SAM found. Use segmented when the point is to handle regions independently downstream - different treatment per segment, or feeding a batch into something that iterates. If you only ever merge them again, the combined node is simpler.

Common issues

First: you need a SAM model. Impact Pack auto-downloads one (sam_vit_b) into ComfyUI/models/sams on first run, but if your SAMLoader dropdown is empty, that's the missing piece - grab a SAM checkpoint. Second: bad SEGS in, bad masks out. SAM refines what you give it; if the upstream detector missed the subject, SAM can't invent it. Third, per above: don't over-trust batch_masks to be perfectly separated - it's an approximation the author flags as a work in progress. And if a mask latches onto the wrong thing inside a region, change the detection_hint before touching anything else; it's the setting that most directly controls what SAM grabs.

Installing it

The node ships with ComfyUI Impact Pack. Install via ComfyUI-Manager (search ComfyUI Impact Pack, Install, restart), or manually: cd ComfyUI/custom_nodes && git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack, install its requirements.txt in ComfyUI's Python environment (portable: ..\..\..\python_embeded\python.exe -m pip install -r requirements.txt), restart. On first run the pack auto-downloads a SAM model to ComfyUI/models/sams - that's the dependency this node lives on. To generate the SEGS it consumes, you'll usually also want a bounding-box detector; the YOLO UltralyticsDetectorProvider now lives in the separate ComfyUI-Impact-Subpack since v8.0. Impact Pack is ltdrdata's, the ComfyUI-Manager maintainer, so it's actively kept and trustworthy.

CategoryImpactPack/Detector

Inputs (9)

NameTypeDefaultDescription
sam_modelSAM_MODELSegment Anything Model for Silhouette Detection. Be sure to use the SAM_MODEL loaded through the SAMLoader (Impact) node as input.
segsSEGSThe areas found by a detector. SAM (Segment Anything) makes a mask for each area of the SEGS. The masks are returned combined into a single mask, and separately as a batch: one mask for each SEG, or with mask-points one mask for all of them.
imageIMAGEThe image the SEGS were detected in. SEGS hold only the detected areas and not the image, so SAM (Segment Anything) reads this image.
detection_hintCOMBOIt is recommended to use only center-1. center-1 gives SAM (Segment Anything) the rectangular area of each SEG and a single point at its exact center as hints. none gives only the rectangular area. The other options were added during the experimental stage and do not work well.
dilationINT0-512–512The size in pixels of the kernel that dilates the result mask. A negative value erodes the mask. Only combined_mask is dilated.
thresholdFLOAT0.930–1SAM (Segment Anything) proposes several masks for an area, each with a score. The masks that score at least this value are combined; if none does, the best one is used. A higher value gives a narrower mask. For a person, it may take only the clothes instead of the whole person.
bbox_expansionINT00–1000The rectangular area of each SEG is expanded by this many pixels on every side before it is given to SAM (Segment Anything) as a hint.
mask_hint_thresholdFLOAT0.700–1When detection_hint is mask-area, points of the SEGS mask are given to SAM (Segment Anything) as hints. Only the points where the mask value is greater than mask_hint_threshold are used.
mask_hint_use_negativeCOMBOAdds negative hints, which mark what SAM (Segment Anything) must leave out of the mask. False: No negative hint. Small: With mask-points, a SEG narrower than 10 pixels becomes a negative point. With mask-area, the points where the SEGS mask is 0 become negative points. Outter: Points sampled at regular intervals outside the area of the SEG. Not used with mask-points.

Outputs (2)

NameTypeDescription
combined_maskMASK—
batch_masksMASK—