ComfyUI Node

Mask_math

Combining two masks with boolean/arithmetic operations

By cardenluoĀ·Created 2 years agoĀ·Updated 17 days agoĀ· 309
Mask_math
  • mask1
  • mask2
  • mask
ā—„operation+ā–ŗ
ā—„algorithmcv2ā–ŗ
ā—„invert_mask1falseā–ŗ
ā—„invert_mask2falseā–ŗ

Two masks, one operation: Mask_math is the simple arithmetic node you reach for when you need to add, subtract, multiply, or AND two masks together instead of writing that logic out of separate, more specialized nodes.

What it does

This is one of cardenluo's ComfyUI-Apt_Preset mask utilities, and worth flagging up front: the pack itself files it under mask/backup rather than the main mask group - the author's own folder structure suggests this is an older or superseded node kept around for compatibility rather than the current recommended way to combine masks. It still works, and it's genuinely useful if you just need quick mask arithmetic, but if you're starting fresh, check whether the pack has a newer mask-combination node in the main mask category before defaulting to this one.

Functionally, it takes mask1 and mask2 and an operation: + (addition - union-like, brightening overlapping areas), - (subtraction - removes mask2's coverage from mask1), * (multiplication - intersection-like, only where both masks have signal), and & (bitwise AND - a hard intersection on binarized masks). algorithm picks the backend doing the math: cv2 (OpenCV/NumPy, CPU-side) or torch (stays on the GPU tensor pipeline). Each input mask can be inverted independently before the operation runs, via invert_mask1 and invert_mask2 - useful for turning "everything except this region" into an operand without a separate invert step.

Inputs and outputs

  • mask1, mask2 - the two masks to combine (required).
  • operation - -, +, *, or &.
  • algorithm - cv2 or torch.
  • invert_mask1, invert_mask2 - booleans, invert either input before combining.

Single mask output.

For a beginner, * is the one to reach for when you want "only where both masks agree" (a clean intersection), and + for "either mask counts" (a union). - and & are more situational - - for carving one region out of another, & when you specifically want a hard binary intersection rather than multiplication's smoother blend on soft-edged masks.

Where this fits

Combine a segmentation mask with a hand-painted mask, subtract a face mask from a full-body mask to isolate "everything but the face," or intersect two detector outputs to keep only the region both agree on. It's a small, general-purpose tool that shows up anywhere your masking logic needs more than one source.

Installing it

Through ComfyUI Manager: search ComfyUI-Apt_Preset. Manual install:

cd ComfyUI/custom_nodes
git clone https://github.com/cardenluo/ComfyUI-Apt_Preset.git

Run install.bat (Windows) or install requirements.txt yourself, restart ComfyUI. No external models needed - this is pure mask arithmetic.

Troubleshooting

If the node's missing entirely, check for a pack-wide import failure first - ComfyUI-Apt_Preset is large enough that one missing dependency can take the whole pack down, and there's a documented case of exactly that happening to a user. Check the ComfyUI console at startup for the real error.

For the node itself: if + or * gives you a result that looks blown out or overly dark, that's usually the algorithm choice interacting with soft (non-binary) masks differently than you expect - try switching between cv2 and torch if the math looks off, since they can clamp and normalize slightly differently. And since this node lives in the pack's backup folder, if you can't find it by searching "Mask_math" in the node browser, make sure you're not filtering to the main mask category only - check under the pack's full category tree.

CategoryApt_Preset/mask/😺backup

Inputs (6)

NameTypeDefaultDescription
mask1MASK—
mask2MASK—
operationCOMBO+4 options: -, +, *, &
algorithmCOMBOcv22 options: cv2, torch
invert_mask1BOOLEANfalse—
invert_mask2BOOLEANfalse—

Outputs (1)

NameTypeDescription
maskMASK—