mask and mask math
Combine two masks with add, subtract, multiply, or intersect
- mask1
- mask2
- mask
This is the mask-only sibling of image_math: same four operations, same cv2/torch backend choice, but it only ever works on two masks and only ever outputs one. If image_math feels like overkill because you're not touching any actual images, this is the leaner version built exactly for that case.
The inputs and outputs that matter
mask1 and mask2 go in, operation picks what happens between them - subtract (-), add (+), multiply (*), or bitwise AND (&, effectively "keep only where both masks overlap"). algorithm chooses whether the math runs through OpenCV (cv2, the default) or PyTorch tensors (torch). invert_mask1 and invert_mask2 flip either input before the operation runs, so you can do things like "everything in mask1 except where mask2 is" without a separate invert node.
The four operations map onto genuinely different real tasks. AND gets you an intersection - the region two independently-generated masks agree on, a solid way to sanity-check a segmentation result against a hand-drawn mask, or to combine a face-detection mask with a skin-tone mask. Add merges coverage - useful when you've got two partial masks from separate detector passes and want the union. Subtract carves a hole - remove one region from another. Multiply behaves like AND for binary masks but blends smoothly for soft-edged ones, since it's true pixel-value multiplication rather than a hard boolean.
The author documents one behavior explicitly, and it's genuinely useful to know before you hit it: if OpenCV isn't installed, this node automatically falls back to the torch backend on its own rather than erroring out. So if you ever wonder why the algorithm dropdown says cv2 but the node clearly ran anyway on a machine where you never installed OpenCV, that's the documented fallback doing its job silently, not a bug.
Installing it
Comes with the rest of ComfyUI-WJNodes. ComfyUI Manager: search "ComfyUI-WJNodes," install, restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git
Restart and find it under WJNode → ImageEdit → MaskEdit. If you want the cv2 path specifically (typically the faster of the two), make sure opencv-python is present in your environment - but given the documented auto-fallback, it's genuinely optional here rather than a hard requirement.
Where people get burned
Mismatched mask resolutions are the most likely source of a bad result or an outright error - both masks should be the same size going in. And remember the invert toggles apply before the operation, not after: if you're trying to invert the result of an AND rather than one of the inputs, you'll need a separate invert step downstream, since invert_mask1/invert_mask2 only ever touch what goes in.
If you're not sure which operation you actually want, AND is the one to reach for first when combining detector output - it's a cheap way to check whether two independent mask sources genuinely agree on a region before you trust either one alone. And if you find yourself using this node's image_math sibling just to run mask-only operations, switching over to this leaner version keeps your graph a little more honest about what's actually happening - no unused image inputs sitting there unwired.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| mask1 | MASK | — | |
| mask2 | MASK | — | |
| operation | COMBO | 4 options: -, +, *, & | |
| algorithm | COMBO | cv2 | 2 options: cv2, torch |
| invert_mask1 | BOOLEAN | false | — |
| invert_mask2 | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |