Nodes/ComfyUI_LayerStyle_Advance/LayerMask: SAM2 Ultra(Advance)
ComfyUI Node Runs on cloud

LayerMask: SAM2 Ultra(Advance)

SAM2 Ultra — ComfyUI Node Guide

By chflame163·Created 2 years ago·Updated 4 months ago· 696
LayerMask: SAM2 Ultra(Advance)
  • image
  • bboxes
  • image
  • mask
sam2_model
precision
bbox_select
select_index0,
cache_modelfalse
detail_method
detail_erode6
detail_dilate4
black_point0.15
white_point0.99
process_detailtrue
device
max_megapixels2.0

This is the single-image half of this pack's SAM2 integration - feed it an image plus bounding boxes telling it what to look at, and it gives back a clean segmentation mask with the same VITMatte-grade edge refinement the pack's other "Ultra" nodes use. If you're working with a video sequence instead, use SAM2VideoUltra; the README is explicit that this node only handles single images, and if you need to process multiple frames you should convert your batch to an image list first rather than expecting this one to track across frames.

SAM2, credited to kijai's ComfyUI-segment-anything-2 implementation in the README, is Meta's second-generation Segment Anything model - the same family that shows up across the wider masking ecosystem alongside GroundingDINO for exactly this "detect the box, then get a precise mask" pipeline. This node is that second half: it doesn't find objects itself, it needs boxes handed to it (from ObjectDetectorFL2, ObjectDetectorYOLO8, or any other bbox-producing node in this pack) and turns them into pixel-accurate masks.

The inputs that matter most:

  • image - your single source image.
  • bboxes - detection box data from an upstream detector node. This is required; SAM2Ultra has no way to find objects on its own.
  • sam2_model - sam2/sam2.1 hiera checkpoints from tiny through large; bigger generally means better segmentation at real speed cost.
  • bbox_select + select_index - if your detector found multiple objects, choose all, first (highest confidence), or by_index to control which boxes actually get segmented.
  • cache_model - keeps the model loaded between runs so repeated calls in the same session skip reload time.

The edge-refinement block matches the pattern across this pack's Ultra nodes: detail_method (VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, or GuidedFilter), detail_erode/detail_dilate for how far refinement reaches from the detected edge, black_point/white_point for edge sampling thresholds, process_detail as the master toggle, device (cuda/cpu), and max_megapixels capping the resolution the refinement pass runs at.

Outputs: image (pass-through) and mask, ready for compositing, inpainting, or a further crop step.

Installing it: ComfyUI Manager, search "ComfyUI Layer Style Advance," or git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance into custom_nodes, then install_requirements.bat (or Aki variant) / pip install -r requirements.txt, then repair_dependency.bat, restart. Model files come from huggingface.co/Kijai/sam2-safetensors or BaiduNetdisk, into ComfyUI/models/sam2. If you're using a VITMatte detail method, you'll also need the VITMatte model in ComfyUI/models/vitmatte.

Where people get stuck: the most common one is skipping the detector step entirely and wondering why this node has nothing to segment - remember bboxes is required, not optional, so you need something upstream producing them first. If masks come back with rough or halo-y edges, check detail_method before assuming the SAM2 model itself is the problem; PyMatting and GuidedFilter are meaningfully cruder than the VITMatte options and are there for speed, not quality. And this pack's whole family of dependency-heavy nodes shares the same general install failure mode - if the node won't even import, that's almost always a transformers/opencv version conflict fixable with repair_dependency.bat, not something specific to SAM2 itself.

Category😺dzNodes/LayerMask

Inputs (15)

NameTypeDefaultDescription
imageIMAGE
bboxesBBOXES
sam2_modelCOMBO8 options: sam2_hiera_base_plus.safetensors, sam2_hiera_large.safetensors, sam2_hiera_small.safetensors, sam2_hiera_tiny.safetensors, sam2.1_hiera_base_plus.safetensors, sam2.1_hiera_large.safetensors, +2
precisionCOMBO3 options: fp16, bf16, fp32
bbox_selectCOMBO3 options: all, first, by_index
select_indexSTRING0,
cache_modelBOOLEANfalse
detail_methodCOMBO5 options: VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, GuidedFilter
detail_erodeINT61–255
detail_dilateINT41–255
black_pointFLOAT0.150.01–0.98
white_pointFLOAT0.990.02–0.99
process_detailBOOLEANtrue
deviceCOMBO2 options: cuda, cpu
max_megapixelsFLOAT2.01–999

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK