Nodes/MTB Nodes/Generate Trimap (mtb)
ComfyUI Node Runs on cloud

Generate Trimap (mtb)

The grey-band map that matting models actually need

By melMass·Created 3 years ago·Updated about a month ago· 721
Generate Trimap (mtb)
  • mask
  • trimap
erode10
dilate10

If you've ever cut out a subject and watched the hair turn into a crunchy, aliased mess, this node is part of the fix. Generate Trimap converts a plain mask into a trimap - the three-region map that image-matting models like ViTMatte need to do their job. It's a setup step, not a finished result, and it pairs directly with MTB's Apply Vit Matte node.

Quick background, because it's the whole reason this exists. A normal background-removal mask is a segmentation output: every pixel is labelled either foreground or background, hard 1 or 0. That's structurally wrong for hair, fur, veils, or smoke, where a pixel is genuinely half subject. Matting models fix this by predicting fractional alpha - but they don't work on a raw image. They work on a trimap: definite foreground (white), definite background (black), and an "I'm not sure, you figure it out" band of grey in between. That grey band is where the model does its magic.

How it works

You feed it a mask you already have - from BiRefNet, SAM, rembg, whatever segmenter you like. The node erodes the mask inward to carve out a shrunken region it's confident is foreground, and dilates it outward to mark a region it's confident is background. The strip left between those two edges becomes the unknown grey zone. Out comes an image-format trimap ready for the matting stage.

The inputs and outputs that matter

  • mask (MASK) - your rough cutout. It only needs to roughly bound the subject; the matting model refines the edge.
  • erode (INT, default 10) - how far the confident-foreground region pulls inward.
  • dilate (INT, default 10) - how far the confident-background region pushes outward.

Together, erode and dilate set the width of that grey unknown band, and that's the one real knob here. Too thin and you clip fine hair before the matting model ever sees it - it never gets a chance to reconstruct those strands. Too wide and you hand the model a huge uncertain area, which is slower and can drag background into the subject. For flyaway hair, widen it; for a clean hard-edged object, keep it tight.

The output is trimap (IMAGE), which wires straight into the trimap input of Apply Vit Matte.

How to install it

Via ComfyUI Manager: search MTB Nodes, install, restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/melMass/comfy_mtb

then restart. Generating the trimap itself is pure image processing - no model download for this node. The download you'll need is for the matting model that consumes it (see Apply Vit Matte).

Common issues & troubleshooting

The matte still eats hair. Your unknown band is too narrow. Bump dilate (and maybe erode) so the grey strip actually covers the wispy region - the matting model can only refine pixels you marked "unknown".

The matte pulls in background. Opposite problem: the band is too wide, or your input mask was already sloppy. Tighten erode/dilate, or clean up the segmentation mask first.

Nothing downstream happens. This node produces a setup image, not a cutout. It's inert on its own - it only pays off wired into Apply Vit Matte (or another trimap-based matting node).

Categorymtb/vitmatte

Inputs (3)

NameTypeDefaultDescription
maskMASK
erodeINT10
dilateINT10

Outputs (1)

NameTypeDescription
trimapIMAGE