SDMatte 精细抠图
The matting node that refines rough masks
- sdmatte_model
- image
- mask
- alpha
- cutout
Every matting thread on r/comfyui eventually gets the same advice: for hair, fur, glass, and smoke, a plain background remover isn't enough - you need a matting model. RuiSDMatte is RUI-Nodes' implementation of SDMatte (vivo Camera Research, ICCV 2025), and it's the pack's flagship: you feed it a rough mask that roughly covers your subject, and it refines that mask down to a high-quality fractional alpha, right down to the flyaway strands and transparent glass edges that make segmentation models give up.
The mental model is important: this node doesn't find the subject, it polishes the edge. Your rough mask can come from anywhere - SAM, BiRefNet, rembg, a hand-drawn scribble. SDMatte's whole job is taking that sloppy silhouette and turning it into a precise cutout. The KB's background-removal essay covers why that distinction (segmentation vs. matting) is the entire game when it comes to hard edges.
How it works
Wire up the model object from RuiSDMatteLoader, your image, and a rough mask, and the node builds the visual prompt from that mask, runs the diffusion-based matting network, and returns alpha (a MASK, fractional 0–1) plus cutout (the image composited onto the alpha). The settings that matter:
prompt_type- how the mask is turned into a prompt.bbox_mask(default) is the one to use; it's the official test path and the most robust.maskuses the mask directly (good when you already have an accurate coarse segmentation).point_masksamples 10 points inside the mask, andauto_maskuses no localization at all (single-subject images only).is_transparent- turn this on for glass, gauze, smoke, anything translucent. The author measured it changing output on ~25% of pixels for a glass subject. Forget it and you'll get a solid-looking glass that's wrong.inference_size- default 1024, matching the official config. Dropping to 512 cost the biggest quality hit of any setting in the author's own tests, so leave it at 1024 unless VRAM forces your hand.caption- an English description of the subject. Here's the sharp edge: it only works with the non-plusSDMatte.pth. The plus weights were trained with COCO-Matte instead of RefMatte and never learned text grounding - a caption on plus weights is at best inert, at worst slightly worse. Leave it blank withSDMatte_plus; only bother with it if you swapped in the originalSDMatte.pth.point_radius/seed- only active forpoint_maskmode (andpoint_maskitself only works onSDMatte.pth).
Installing it
Install the pack (ComfyUI Manager → search "RUI-Nodes", or git clone + pip install -r requirements.txt), make sure diffusers, transformers, safetensors, scipy, and opencv-python are installed, drop a checkpoint into ComfyUI/models/SDMatte/, and restart. You do not need SD 2.1 weights - the config ships with the node.
Where people get burned
Three things, all grounded in real reports. First, the model-detection wall: this is the most common SDMatte failure in the wild - the node won't see your downloaded weights. Weights must be in models/SDMatte/, and the diffusers/transformers deps must be installed or the node never loads. Second, black output on point_mask with the plus weights - the node prints a warning when the alpha maxes near 0.1, which is your cue to switch to bbox_mask or the original SDMatte.pth. Third, the transformers 5.x trap: newer versions silently drop the text-encoder keys in other implementations; this node patches that automatically. Keep inference_size at 1024, keep is_transparent on for translucent subjects, and let the rough mask be rough - precision is the model's job, not yours.
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| sdmatte_model | SDMATTE_MODEL | 由「SDMatte 加载器」输出的模型。 加载器有缓存,多个抠图节点可共用同一个加载器。 | |
| image | IMAGE | 待抠图的原图。RGBA 输入会自动丢弃 alpha 只取 RGB (VAE 编码器只收 3 通道,多出的 alpha 不能当颜色喂进去)。 | |
| mask | MASK | 指示要抠哪个目标的提示掩码,不必精确,粗略覆盖主体即可。 | |
| prompt_type | COMBO | bbox_mask | 视觉提示类型。 bbox_mask:取掩码外接框作为提示,官方测试脚本的默认路径,通常最稳; mask:直接用掩码本身,适合已有较准的粗分割; point_mask:在掩码内随机取 10 个点。 ⚠ 仅 SDMatte.pth 支持;SDMatte_plus.pth 用它会输出全黑, 因为 SDMatte*(plus) 的训练集用 COCO-Matte 替换了 RefMatte,未训练点提示; auto_mask:不给定位信息,全图自动,画面只有单一主体时可用。 |
| inference_size | COMBO | 1024 | 官方测试固定用 1024,降低会明显损失边缘细节。 |
| is_transparent | BOOLEAN | false | 目标是否为玻璃、纱、烟雾等透明/半透明物体。 该开关会切换模型的不透明度嵌入分支,抠透明物时务必打开。 |
| captionopt | STRING | 目标物体的英文描述(对应 RefMatte 的指代表达式),经 CLIP 编码后 注入 UNet 的下采样/上采样两段 cross-attention。 · SDMatte.pth:有语义作用,填对能小幅提升(实测羊驼图 MAD 0.01120→0.01072) · SDMatte_plus.pth:无语义作用,填了反而更差,请留空 (plus 的训练集用 COCO-Matte 替换了 RefMatte,未学过文本指代) 留空即官方测试时的默认行为。 | |
| point_radiusopt | INT | 355–100 | 仅 prompt_type=point_mask 时生效,其余模式完全不参与运算。 含义:在提示区域随机取 10 个点,每点用 sigma=该值的高斯核晕开成光斑, 再逐像素取最大值合成提示图 —— 即每个点的影响半径。 官方训练用 25,测试期用 25+10=35,故默认 35。 |
| seedopt | INT | 00–4294967295 | 随机种子,仅在 prompt_type=point_mask 时真正起作用 (决定在提示区域里随机取哪 10 个点)。 其余提示类型下改它不会改变结果。 |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| alpha | MASK | — |
| cutout | IMAGE | — |