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

LayerMask: SAM2 Video Ultra(Advance)

SAM2 Video Ultra — ComfyUI Node Guide

By chflame163·Created 2 years ago·Updated 4 months ago· 696
LayerMask: SAM2 Video Ultra(Advance)
  • image
  • bboxes
  • first_frame_mask
  • pre_mask
  • mask
  • preview
sam2_model
precision
cache_modelfalse
individual_objectsfalse
mask_preview_color#FF0080
detail_method
detail_erode6
detail_dilate4
black_point0.15
white_point0.99
process_detailtrue
device
max_megapixels0.5

SAM2's headline feature over the original Segment Anything is tracking an object across frames without re-detecting it every time, and this node is built specifically for that - feed it an image sequence (or video frames), tell it what to track on the first frame, and it propagates that segmentation through the rest of the sequence.

The mechanism, per the README: you define your object once, on the first frame - either with bboxes (recognition box data from a detector node) or first_frame_mask (a mask you already have). If you give it both, first_frame_mask wins and the bboxes get ignored. From there SAM2's video tracking carries that identification forward frame by frame, which is fundamentally more reliable than running per-frame detection, since a per-frame approach can flicker or lose the subject entirely on a bad frame - this node tracks continuity instead.

The inputs worth knowing:

  • image - your frame sequence.
  • bboxes (optional) / first_frame_mask (optional) - at least one required; defines what to track, applied only to the first frame.
  • pre_mask (optional) - a mask that limits where propagation is allowed to look, described in the README as improving recognition accuracy by giving the tracker a bounded search area rather than the whole frame.
  • sam2_model - pick from the sam2/sam2.1 hiera tiny/small/base_plus/large checkpoints; larger models generally track more robustly at the cost of speed.
  • individual_objects - true focuses on tracking a single object; false attempts to generate recognition boxes for multiple objects at once.
  • mask_preview_color - the color used to show non-masked areas in the preview output, purely visual.
  • detail_method - this node only supports VITMatte for edge refinement, unlike the pack's other Ultra nodes which also offer PyMatting/GuidedFilter. detail_erode, detail_dilate, black_point, white_point, and process_detail control that refinement pass the same way they do across the rest of the pack.
  • device - cuda only for this node; there's no CPU fallback.
  • max_megapixels - caps the resolution VITMatte processes at; higher values give finer edges but cost real speed, per the README's own warning.

Outputs: mask (the tracked segmentation across the sequence) and preview (a visualization using your chosen preview color).

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 / 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 - note the credit to kijai in the README, this node is built on his ComfyUI-segment-anything-2 work.

Where people get stuck: the single-image caveat matters - this is specifically for sequences; if you only have one frame, use SAM2Ultra instead, not this node. max_megapixels at 0.5 (the default) is conservative on purpose, since VITMatte at higher resolutions gets expensive fast across a whole sequence rather than one image - raise it only once you've confirmed the tracking itself is solid, so you're not burning time refining edges on a run you'll discard. And if tracking drifts partway through a sequence, pre_mask is worth trying before switching models - constraining the search area often fixes drift that a bigger sam2_model alone won't.

Category😺dzNodes/LayerMask

Inputs (17)

NameTypeDefaultDescription
imageIMAGE
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
precisionCOMBO2 options: fp16, bf16
cache_modelBOOLEANfalse
individual_objectsBOOLEANfalse
mask_preview_colorSTRING#FF0080
detail_methodCOMBO2 options: VITMatte, vitmatte-base-composition-1k
detail_erodeINT61–255
detail_dilateINT41–255
black_pointFLOAT0.150.01–0.98
white_pointFLOAT0.990.02–0.99
process_detailBOOLEANtrue
deviceCOMBO1 options: cuda
max_megapixelsFLOAT0.50.1–10
bboxesoptBBOXES
first_frame_maskoptMASK
pre_maskoptMASK

Outputs (2)

NameTypeDescription
maskMASK
previewIMAGE