Nodes/ComfyUI-SDMatte/Apply SDMatte
ComfyUI Node

Apply SDMatte

Trimap-guided matting when a segmentation mask isn't good enough

By flybirdxx·Created 12 months ago·Updated 7 months ago· 171
Apply SDMatte
  • image
  • trimap
  • alpha_mask
  • matted_image
ckpt_name
inference_size1024
is_transparentfalse
output_modealpha_only
mask_refinetrue
trimap_constraint0.8
force_cpufalse

Most "cut the subject out" nodes you've used are segmentation models: they look at a pixel and decide foreground or background, full stop. That's fine for a person standing in front of a wall and falls apart the moment you've got hair, a veil, glass, or smoke - anything that's genuinely half-transparent. SDMatte is built for that harder case. It's a diffusion-based matting model (leans on Stable Diffusion 2.1's pretrained priors rather than training a segmentation network from scratch) that takes an image plus a trimap you provide, and outputs a proper alpha matte with fractional values instead of a hard yes/no mask. This particular pack wraps the SDMatte research model (out of vivo's Camera Research team, ICCV 2025) into one ComfyUI node - it's a third-party wrapper, unaffiliated with the original authors, but it's a straightforward one.

The thing that separates it from the BiRefNet/rembg/InSPyReNet crowd you've probably already got installed is the trimap requirement. Most matting nodes people reach for now are trimap-free - you feed an image, you get a mask, done. SDMatte instead asks you to sketch a rough map first: paint white where you're sure it's foreground, black where you're sure it's background, and leave gray for "I don't know, you figure it out." That's more setup, but it's also why it can nail genuinely ambiguous edges a trimap-free model has to guess at - you're giving it the hint it needs instead of hoping it infers correctly.

How it works

Under the hood it's SD 2.1's UNet and VAE repurposed as a matting engine, guided by your trimap as a visual prompt instead of a text prompt. The node resizes your image and trimap to the inference resolution, normalizes everything, runs it through the diffusion-prior pipeline, then resizes the result back and clamps it to a proper 0–1 alpha range. Since v1.3.0 the pack merged what used to be two nodes (a model loader and an apply node) into this single one, so you just drop it in and pick a checkpoint.

The inputs and outputs that matter

  • image and trimap - your source image, and the mask you paint: white = definite foreground, black = definite background, gray = "figure it out." This is the input that actually determines quality; a sloppy trimap gives you a sloppy matte no matter what else you tune.
  • ckpt_name - SDMatte.safetensors (standard, smaller, faster) or SDMatte_plus.safetensors (bigger, more accurate). Author's own advice in the README: start standard, upgrade only if you need it.
  • inference_size - 512 up to 1024. Higher is better quality and slower; 1024 for best results, 768 if you want the balance.
  • output_mode - alpha_only gives you just the mask, matted_rgba gives a transparent-background cutout, matted_rgb gives a black-background cutout. The author explicitly recommends matted_rgb to avoid downstream interference, which is worth taking seriously if your next node doesn't handle alpha cleanly.
  • mask_refine and trimap_constraint - refinement uses your trimap to filter out background bleed; the constraint (0.1–1.0, default 0.8) controls how strict that filtering is. Push it toward 0.9 if background is leaking through, loosen toward 0.6 if you're losing real foreground detail.
  • Outputs: alpha_mask (a MASK you can feed into compositing or inpainting) and matted_image (the cut-out IMAGE itself).

Installing it

Search ComfyUI-SDMatte in ComfyUI Manager, or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/flybirdxx/ComfyUI-SDMatte

Restart. ComfyUI is supposed to auto-install diffusers, timm, einops, lazyconfig, and safetensors from requirements.txt on startup - in practice that doesn't always take, more on that below. On first run the node downloads the SDMatte weights automatically to models/SDMatte/ (the standard checkpoint runs ~11GB per the README's FAQ, so budget the disk and the wait), plus config-only files for the SD 2.1 base to models/diffusers/stable-diffusion-2-1-base/ - that second download is small, it's just tokenizer/config files, not the full base model weights, so don't panic if it looks tiny.

Common issues

The dependency install is the thing that actually bites people. There's a real report of this exact node throwing ImportError: diffusers and transformers are required for SDMatte functionality, and separately, a cannot import name 'cached_download' from 'huggingface_hub' error - that second one is a version mismatch: newer huggingface_hub releases dropped the old cached_download function the node's dependency chain expects. If you hit either, pip install -U diffusers transformers huggingface_hub in your ComfyUI's venv (or roll huggingface_hub back if the newer one keeps breaking things) before assuming the node itself is broken.

Past that: if VRAM runs out, drop inference_size before reaching for force_cpu - 768 is a real quality/speed compromise, not just a fallback. First inference is slow because CUDA kernels need to compile; that's normal and subsequent runs speed up. And if your matte looks muddy, it's almost always the trimap - tighten your gray "unknown" band before touching trimap_constraint.

CategoryMatting/SDMatte

Inputs (9)

NameTypeDefaultDescription
ckpt_nameCOMBO2 options: SDMatte.safetensors, SDMatte_plus.safetensors
imageIMAGE需要进行抠图的输入图像
trimapMASK三值图掩码:白色=前景,黑色=背景,灰色=未知区域
inference_sizeCOMBO1024推理分辨率,越高质量越好但速度越慢。推荐1024(最高质量)或768(平衡性能)
is_transparentBOOLEANfalse输入图像是否包含透明通道。如果原图有透明背景请启用
output_modeCOMBOalpha_only输出模式:alpha_only=只输出遮罩;matted_rgba=透明背景抠图;matted_rgb=黑色背景抠图(推荐,避免干扰)
mask_refineBOOLEANtrue启用遮罩优化,使用trimap约束过滤不需要的区域,减少背景干扰
trimap_constraintFLOAT0.80.1–1trimap约束强度(0.1-1.0)。越高约束越严格,0.8=平衡,0.9=严格过滤,0.6=宽松保留
force_cpuoptBOOLEANfalse

Outputs (2)

NameTypeDescription
alpha_maskMASK
matted_imageIMAGE