Create Trimap
The boring node your matte quality actually depends on
- mask
- MASK
Why this node exists
Apply Matting only looks as good as the trimap you feed it, and CreateTrimap is the node that makes that trimap. It takes whatever mask you already have - from SAM, GroundingDINO, rembg, doesn't matter - and turns it into the three-value map that ViTMatte actually wants: 255 for definitely foreground, 0 for definitely background, and 128 for the in-between "unknown" band where the model is allowed to spend its effort.
That band is the entire trick of trimap-based matting. The model doesn't try to figure out the whole image; it just refines the edges inside the unknown region. So the width of that band is effectively a knob for how much work you're asking the matting model to do - and how much trust you're placing in the mask you already have.
How it works
CreateTrimap is pure OpenCV morphology, no neural network involved. It dilates your mask with an elliptical structuring element to get the safe foreground, and erodes it to get the safe background. Everything between those two boundaries becomes the unknown band, filled with 128. Three-value map in hand, Apply Matting has what it needs to produce soft edges instead of hard ones.
The one input that matters
mask(MASK) - your foreground mask. It can be soft or hard; it gets thresholded internally, so treat it as binary.kernel_size(INT, default 9, range 0–50) - the diameter of the elliptical kernel, and honestly the only knob on this node.
Too small a kernel and the unknown band is a sliver - ViTMatte has nothing to refine and you get back the same hard edge you were trying to escape. Too large and the model has a big grey zone to resolve, which is slower and lets it invent detail where your mask was already right. The author's sample workflow ships with 25; on hair-heavy subjects that's a decent starting point. Tune from there.
The output is a single MASK (the trimap) that goes into Apply Matting's trimap input. It's also worth previewing - drop it through MaskToImage and you'll actually see the grey band, which makes the kernel_size tuning far less guesswork.
Installing and wiring it
Same install as the rest of the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/hackkhai/ComfyUI-Image-Matting
Restart, and it shows up in the image_matting category. The natural chain is LoadImage → SAM/GroundingDINO mask → CreateTrimap → Apply Matting, which is exactly the workflow the pack ships as its example. No model download is involved in this node - that's all on Load Matting Model's side, first time you run it.
Common issues
- You fed a plain mask straight to Apply Matting and skipped this node. Some people do; the result is a worse matte. It's three nodes, not two, and this is the middle one.
- Edges come back hard anyway. Kernel too small for the subject's fine detail. Widen it and rerun - the model needs a band wide enough to catch the hair.
- Slow on huge masks. Morphology on a large mask with a big kernel is pure CPU work. It's a few seconds at worst, not a hang, but don't mistake it for a frozen graph.
- Kernel at 0 or 1. There's no unknown band left at all. You've built a trimap with nothing to refine, and you might as well have skipped the whole pack.
The trap worth internalizing: CreateTrimap is where a bad matte is decided, not where it's diagnosed. If your cutout looks chunky, the instinct is to blame the matting model - nine times out of ten the fix is on this node's kernel_size.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| kernel_size | INT | 90–50 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |