Nodes/Comfy_KepMatteAnything/MatteAnything_ToTrimap
ComfyUI Node

MatteAnything_ToTrimap

The three-class bridge that makes matting possible

By M1kep·Created 3 years ago·Updated 2 years ago· 11
MatteAnything_ToTrimap
  • mask
  • TRIMAP
  • MASK
erode_kernel_size
dilate_kernel_size

Here's the concept that makes the whole pack make sense: a matting model doesn't want your mask - it wants a trimap. Instead of telling it "these pixels are foreground, these are background," a trimap gives it three values: definite foreground, definite background, and an unknown band in between that it's allowed to figure out. That unknown band is where soft alpha gets born, which is why hair and glass come out of this pack looking like hair and glass instead of a sticker cut with scissors.

MatteAnything_ToTrimap is the node that builds that band. It takes the binary mask from MatteAnything_SAMMaskFromBoxes and expands it into the three-class image that MatteAnything_GenerateVITMatte needs.

How it works

The mechanics are classic morphology. The mask gets eroded (shrunk) to mark the guaranteed-foreground core, and dilated (grown) to mark the guaranteed-background surround. Everything between those two is the unknown zone, labeled 0.5. Two knobs control how thick that band is:

  • erode_kernel_size - how aggressively to shrink the core.
  • dilate_kernel_size - how far out the background claims.

Bigger values mean a fatter unknown region, which gives ViTMatte more room to work out fine edges - but also more chances to guess wrong if the subject has messy boundaries. The original Matte Anything app's defaults were 10 and 10, and they're a fine starting point. This is genuinely the only "creative" knob in the pack's mask path, and the one you'll tune when a cutout looks slightly off.

The node returns two outputs:

  • TRIMAP - what MatteAnything_GenerateVITMatte consumes.
  • MASK - here's the trap: this second output is also the trimap, not your original mask. The code returns the same tensor twice. If you wire "MASK" into a compositor expecting a binary silhouette, you'll get the three-toned trimap instead and wonder what went wrong. Use the mask output from SAMMaskFromBoxes for that; this node's second output is a convenience, and a confusingly-labeled one.

Why not just skip it?

You can't. MatteAnything_GenerateVITMatte expects a TRIMAP, and feeding it a plain binary mask will technically run but gut the whole point - ViTMatte's edge-refinement only has something to refine if the unknown band exists. No trimap, no matting. It's just a hard mask again, which defeats the reason anyone installs this pack instead of using a one-model cutout node.

Installing this pack

ComfyUI Manager, search Comfy_KepMatteAnything, install. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/M1kep/Comfy_KepMatteAnything

then restart ComfyUI. This node itself is pure OpenCV + torch, but the pack drags in the whole dependency trio anyway (segment-anything, detectron2, groundingdino-py), so you'll have to pay the detectron2 tax regardless.

Gotchas

  • The iterations are hardcoded. Erode and dilate each run five passes internally, on top of your kernel-size choice. The size sliders give you range, but you can't control the passes themselves - it's fine in practice, just not obvious from the inputs.
  • Tune in one direction. If edges look too chunky, increase the kernels. If ViTMatte is hallucinating edge detail in the wrong places, shrink them. Start at 10/10 and move one knob at a time.

Honest take: this is the node people skip because they don't know what a trimap is, and it's exactly the one that separates this pack from every "just give me a cutout" tool. Thirty seconds of reading the concept, and it goes from baffling to the most controllable part of the whole graph.

CategoryMatte Anything

Inputs (3)

NameTypeDefaultDescription
maskMASK
erode_kernel_sizeINT
dilate_kernel_sizeINT

Outputs (2)

NameTypeDescription
TRIMAPTRIMAP
MASKMASK