ComfyUI Node

Apply Matting

Where rough masks become soft edges

By hackkhai·Created 2 years ago·Updated 2 years ago· 19
Apply Matting
  • matting_model
  • matting_preprocessor
  • image
  • trimap
  • IMAGE
  • MASK

The problem it solves

If you've ever cut a subject out and composited it onto a new background, you know the tell: the edges look pasted on. A hard, aliased line where hair, glass or a fuzzy sweater should fade. That's a segmentation mask doing a matting job, and Apply Matting is the fix. It takes a rough trimap plus your image and returns a proper soft alpha matte - the fractional, edge-by-edge transparency that makes a cutout look real instead of cut.

It's the workhorse of the small ComfyUI-Image-Matting pack. The name undersells it: this node doesn't just produce an alpha, it also defringes the foreground, so the background color that used to bleed into the edges gets reconstructed away.

How it works

Under the hood it runs ViTMatte, the CVPR 2024 matting model from HUST's vision lab (the hustvl weights on Hugging Face). Given the image and a trimap, ViTMatte predicts a per-pixel alpha between 0 and 1 - not a hard yes/no. That's the whole point of matting versus segmentation: a strand of hair that's genuinely half-transparent gets alpha around 0.5 instead of being forced white or black.

Then there's a second pass most people never notice. Apply Matting runs a blur-fusion foreground estimator over the result, which re-estimates the foreground color from the alpha and the image. This is the defringe step - it removes the colored halo you get when a bright background tints the edges of a dark subject. That halo is the thing that makes a matte technically perfect and still look composited.

Inputs and outputs

All four inputs are required, and there's no skipping one:

  • matting_model and matting_preprocessor - the pair straight out of Load Matting Model. Wire both in; they ship together for a reason. The preprocessor handles the resizing and normalization ViTMatte expects.
  • image - the actual subject image, straight from LoadImage.
  • trimap - a three-value MASK (255 foreground, 0 background, 128 unknown) from CreateTrimap. Feed it a plain hard mask and the result will be measurably worse; the unknown band is what gives the model room to work.

The outputs:

  • IMAGE - the image with alpha baked in as RGBA. Background pixels are transparent, so the preview shows the checkerboard. Save as PNG to keep the alpha.
  • MASK - the fractional alpha matte on its own. Useful if you want the matte for inspection (drop it through MaskToImage) or to feed another stage.

Installing it

One line in the right directory:

cd ComfyUI/custom_nodes
git clone https://github.com/hackkhai/ComfyUI-Image-Matting

Then restart ComfyUI. The nodes live under the image_matting category, and the first run downloads the model automatically - more on that in the Load Matting Model article.

Worth knowing before you install: the pack's requirements install opencv in four variants plus transformers pulled straight from the GitHub master branch. The git pin is the risky bit, and the opencv quadruple is why the pack ships an import_error_install.bat that uninstalls and reinstalls opencv when cv2 stops importing. If you hit that, run it from the pack's folder.

Common issues

  • Chunky, hard edges in the result. The trimap's unknown band is too narrow, so ViTMatte never got room to refine. Widen kernel_size on CreateTrimap and rerun.
  • Color fringing left at the edges. Sometimes the blur-fusion pass doesn't fully clear a strong halo. Knowing it exists saves you from debugging the alpha as if it were the problem.
  • The model download stalls or fails. First run needs network and writes into ComfyUI/models/matting_models/. If you can't reach Hugging Face, place the hustvl/vitmatte-small-composition-1k folder there manually - the loader picks up local copies.
  • The alpha silently disappears downstream. Some ComfyUI nodes drop the alpha channel when an RGBA image passes through. If a compositing node eats the transparency, save the PNG early.

One honest caveat: this is a trimap-based pipeline, so it's only as good as the mask you start from. You need SAM, GroundingDINO or similar to get a mask in the first place. For plain "cut the background out of a product photo" you're probably better off with a one-click remover like BiRefNet. Reach for this pack when you're compositing and the edges are the thing that matters.

Categoryimage_matting

Inputs (4)

NameTypeDefaultDescription
matting_modelMATTING_MODEL
matting_preprocessorMATTING_PREPROCESSOR
imageIMAGE
trimapMASK

Outputs (2)

NameTypeDescription
IMAGEIMAGE
MASKMASK