Mask: Trimap Builder
Build a Trimap for Real Alpha Matting
- mask
- trimap
JHPixelProTrimapBuilder converts a binary mask into a trimap - a three-value mask encoding 1.0 foreground, 0.0 background, and 0.5 unknown. If you've never touched alpha matting, here's the context: hard binary masks from SAM and rembg assume every pixel is fully in or fully out, which is exactly wrong for hair, fur, and soft edges. A trimap tells a matting algorithm "these pixels are definitely foreground, these are definitely background, and this ambiguous band in the middle is where the real alpha lives." That band is where the matting happens.
Why you need this node specifically: the pack's Alpha Matte Extractor (the Levin 2008 closed-form matting node in the same group) is strict about its input convention - it expects exactly 0.0/0.5/1.0 with ±0.05 tolerance. This node is the recommended way to produce that. Feed a SAM mask in, get a compliant trimap out, wire it into the matte extractor. It also works standalone as a mask-softening step for compositing.
Inputs:
mask- a binary or soft MASK to derive the trimap from.fg_radius(default 4) - how much of the interior to keep solid foreground.bg_radius(default 8) - how far out to mark solid background. The unknown band lives between the two.smoothing(default 0) - optional blur on the boundaries, 0..16.
Output: a single trimap MASK.
The practical pattern: a thinner unknown band means faster, cleaner matting but more risk if the mask edge is inaccurate. Start with the defaults (4/8) and widen the band for hair-heavy subjects, where the real edge is genuinely uncertain. That's the whole tuning game - the band should cover where the true alpha lives, not more.
Install is the shared pack install:
cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/ComfyUI-JH-PixelPro.git
cd ComfyUI-JH-PixelPro
pip install -r requirements.txt
Restart and it's under ComfyUI-JH-PixelPro/mask, or use ComfyUI Manager → search "ComfyUI-JH-PixelPro".
Troubleshooting: don't hand-fake a trimap elsewhere and expect the matte extractor to accept it - the ±0.05 tolerance is strict, and a "trimap" with mis-placed 0.5 values silently degrades the matte instead of erroring. If matting output looks torn at the edges, widen bg_radius; if it's overblending into the background, narrow both radii. It's a mask-group node, so like the rest of this pack it needs no model downloads - just the base kornia/OpenCV/SciPy requirements.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | Binary or soft MASK used to derive a 3-value trimap. | |
| fg_radius | INT | 41–64 | — |
| bg_radius | INT | 81–64 | — |
| smoothing | FLOAT | 0.00–16 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| trimap | MASK | — |