LayerMask: SAM2 Video Ultra(Advance)
SAM2 Video Ultra — ComfyUI Node Guide
- image
- bboxes
- first_frame_mask
- pre_mask
- mask
- preview
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 supportsVITMattefor edge refinement, unlike the pack's other Ultra nodes which also offer PyMatting/GuidedFilter.detail_erode,detail_dilate,black_point,white_point, andprocess_detailcontrol 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.
Inputs (17)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| sam2_model | COMBO | 8 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 | |
| precision | COMBO | 2 options: fp16, bf16 | |
| cache_model | BOOLEAN | false | — |
| individual_objects | BOOLEAN | false | — |
| mask_preview_color | STRING | #FF0080 | — |
| detail_method | COMBO | 2 options: VITMatte, vitmatte-base-composition-1k | |
| detail_erode | INT | 61–255 | — |
| detail_dilate | INT | 41–255 | — |
| black_point | FLOAT | 0.150.01–0.98 | — |
| white_point | FLOAT | 0.990.02–0.99 | — |
| process_detail | BOOLEAN | true | — |
| device | COMBO | 1 options: cuda | |
| max_megapixels | FLOAT | 0.50.1–10 | — |
| bboxesopt | BBOXES | — | |
| first_frame_maskopt | MASK | — | |
| pre_maskopt | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |
| preview | IMAGE | — |