Mask: Alpha Matte
When a binary mask chops off the hair, this is the node you want
- trimap
- guide
- alpha
Ever cut out a portrait with SAM or rembg and watched every flyaway strand of hair come back as a hard, chipped edge? That's the moment you stop dealing in binary masks and start dealing in alpha mattes. JHPixelProAlphaMatteExtractor from the ComfyUI-JH-PixelPro pack solves for the real per-pixel opacity between foreground and background - the soft, half-transparent fringe that binary cutouts just can't represent.
It's the quality-first node in the pack's mask refinement group. If you need a fast cleanup of rough edges, its sibling JHPixelProEdgeAwareMaskRefiner is the cheaper first step; this one is what you reach for when the edge genuinely needs to not be a hard edge - hair, fur, motion blur, glass, anything with sub-pixel transparency.
How it works
This is the Levin et al. 2008 "closed-form solution to natural image matting" algorithm, and the implementation is honest about it. You feed it a trimap - a mask where 0.0 is definitely background, 1.0 is definitely foreground, and 0.5 marks the unknown band to be solved - plus a guide RGB image. The node builds a sparse matting Laplacian from local color covariance in the guide, then solves for alpha with the trimap as hard constraints. On CUDA it uses a PyTorch-native sparse conjugate-gradient solver; on CPU it falls back to SciPy's sparse solver. Same math either way, so you don't get "different results on GPU" - you get the same result at different speeds.
The two knobs that matter:
window_radius(1–3, default 1): the local neighborhood for the color model. Smaller is finer, and honestly you rarely need to move off 1.lambda_constraint(default 100): how hard the solve pins the known trimap regions. Higher = stricter adherence to your trimap, lower = the solver trusts the image's color more.
epsilon is just a numerical regularizer for the covariance estimate - leave it. compute_device (auto/cuda/cpu) picks the solve path; use cuda for large portraits, because building that Laplacian at 4K is not cheap.
Building the trimap
The node is strict: it expects exactly the three values 0.0 / 0.5 / 1.0 (with ±0.05 tolerance). The pack ships JHPixelProTrimapBuilder to make one from a binary mask plus erosion/dilation radii, and that's the path of least resistance - you want a foreground core, a background ring, and an unknown band straddling the edge. Where people get burned: feeding a grayscale soft mask in and getting back something that's neither a trimap nor useful.
Output is a single alpha MASK ([0,1]) that you can composite with, feather further, or wire into an inpaint denoise mask.
Install
From ComfyUI Manager, search ComfyUI-JH-PixelPro. Or by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/ComfyUI-JH-PixelPro.git
cd ComfyUI-JH-PixelPro
pip install -r requirements.txt
Then restart ComfyUI. The pack pulls in kornia, mediapipe, opencv-python-headless and scipy (PyTorch comes with ComfyUI), and wants ComfyUI ≥ 0.43.x with Python ≥ 3.10. This node is the one node in the pack where I'd genuinely say: prefer an NVIDIA GPU with 8 GB+ - the matting solve at high resolution on CPU is "correct but you'll go make coffee" territory. There's a reason the README calls the CPU path exact-but-slower.
One honest caveat: the matting Laplacian is a pixel-count-sized sparse matrix. On a big canvas this node is memory-hungry, so trim the region you're matting before you run it if you can. That's not a bug, that's Levin's algorithm being what it is. For anything that doesn't need true alpha, the Edge-Aware Mask Refiner is the fast lane; this node is for the shots worth the wait.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| trimap | MASK | 3-value trimap MASK: 0.0 background, 0.5 unknown, 1.0 foreground. Tolerance ±0.05. | |
| guide | IMAGE | — | |
| epsilon | FLOAT | 01e-8–0.01 | — |
| window_radius | INT | 11–3 | — |
| lambda_constraint | FLOAT | 1001–10000 | — |
| compute_device | COMBO | auto | 3 options: auto, cuda, cpu |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| alpha | MASK | — |