ComfyUI Node Runs on cloud

Image Matte

Turn a hard mask into a matte that keeps the hair

By WASasquatch·Created 3 years ago·Updated 4 days ago· 1,844
Image Matte
  • images
  • masks
  • mattes
  • foreground
  • cutout
certain_foreground240
certain_background10
band10
unmix_foregroundtrue

The moment your cut-out subject has hair, fur, smoke or motion blur, a hard mask from a segmenter is lying to you. It says each pixel is either subject or background, but the real edges of hair are a blend, and if you composite a hard cut onto a new background you carry a white or green fringe with it. WAS Image Matte exists to fix exactly that: it turns a rough mask into a proper matte with a soft, correct edge, solved against the picture's own colours rather than just blurred into fakeness.

The mechanism is the classic matting trick, and the node's search aliases name it: closed-form matting, built around a trimap. You tell it which pixels are certainly foreground, which are certainly background, and the band in between is what it solves. Your rough mask supplies the certainty. Then three integers set the trimap:

  • certain_foreground (default 240, on the mask's 0–255 scale) - mask level above which a pixel is definitely the subject. 240 trusts only what the mask calls solid white; dropping it toward 128 trusts more and leaves a narrower band to solve.
  • certain_background (default 10) - below which a pixel is definitely not the subject.
  • band (default 10) - how many pixels get pulled back off both certain regions to form the band the matte is solved across. 0 means "solve nothing, answer the mask." 10 handles a soft edge; crank it to 40 for flyaway hair, which needs a wide band because the transition is genuinely wide.

What comes out is where this node earns its keep. Three outputs: mattes (the solved alpha, white where the subject is, soft across the band), foreground, and cutout. The foreground output isn't just the original frame masked - with unmix_foreground on, it estimates the subject's own colour with the background taken out of it. That's what stops a green-screen or white-fringe halo from following the edge onto your new plate: you composite the unmixed foreground, not the original pixels. The tooltip is blunt about the alternative - composite the original and the old background follows the edge onto the new one. The cutout output is the foreground with the matte as its fourth channel, ready for a PNG save or any alpha-aware join.

The honest trade-off, stated in the node itself: unmix_foreground off answers the frame unchanged on that output and is several times faster. If you're batch-processing hundreds of frames and the subject is cleanly separated from its background, you can leave it off and only solve the matte. If there's any fringe at all, you need it on.

A practical note on masks: it takes one mask per frame, or a single mask that's applied to every frame. Feed it the raw output of Image Remove Background, CLIPSeg Masking or SAM and you're using it as intended. This is a post-processing discipline the wider community keeps re-learning - the "please composite after you cut out" lesson that recurs every time a background-removal model ships. WAS's own suite has the background remover on the front end; this node is the cleanup on the back.

Install

Part of WAS Node Suite v3 - ComfyUI Manager, search "WAS Node Suite", or:

cd ComfyUI/custom_nodes
git clone https://github.com/WASasquatch/was-node-suite-comfyui

Restart after. Requires ComfyUI 0.14.0+ and Python 3.10+. Nothing extra to install - the matte solver runs on torch on ComfyUI's own device, no model weights, no pip packages. (The v2 pack's notorious dependency pile is gone; v3 ships clean.)

Common issues

  • Hard edge still shows. Your band is too small for the transition. Hair wants 30–40, not 10.
  • Green or white halo on the new background. You composited the original frame. Use the foreground output with unmix_foreground on.
  • Band never seems to matter. If the picture and mask disagree about where the edge is, widen certain_background so more of the uncertain region is actually in the band being solved.
CategoryWAS Suite/Image/Process

Inputs (6)

NameTypeDefaultDescription
imagesIMAGEThe frames the matte is solved against. Each one is run on its own and comes back at the size it went in at.
masksMASKThe rough mask, white where the subject is. A hard cut-out from Image Remove Background, CLIPSeg Masking or SAM is what this is for. One mask is used for every frame; a batch is paired frame by frame.
certain_foregroundINT2401–255Mask level above which a pixel is certainly the subject, on a 0 to 255 scale. 240 trusts only what the mask calls solid; 128 trusts more of it and leaves a narrower band to solve.
certain_backgroundINT100–254Mask level below which a pixel is certainly not the subject, on a 0 to 255 scale. 10 trusts only what the mask calls empty; 64 trusts more of it.
bandINT100–128Pixels pulled back off both certain regions, which is what the matte is solved across. 0 = solve nothing and answer the mask; 10 = a 10px band, enough for a soft edge; 40 = enough for flyaway hair.
unmix_foregroundBOOLEANtrue`on` also estimates the subject's own colour with the background taken out of it, which removes a fringe. `off` answers the frame unchanged on that output and is several times faster.

Outputs (3)

NameTypeDescription
mattesMASKThe solved matte, white where the subject is, soft across the band.
foregroundIMAGEThe subject's colour with the background unmixed out of it. Composite this rather than the original frame, or the old background follows the edge onto the new one. A frame carrying light above white comes back on the scale it arrived on.
cutoutIMAGEThe foreground with the matte as its fourth channel, ready for Add Layer, Join Image with Alpha or a PNG save.