Nodes/opencv-comfyui/OpenCV bitwise_xor_1
ComfyUI Node

OpenCV bitwise_xor_1

OpenCV bitwise_xor_1

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV bitwise_xor_1
  • src1
  • src2
  • dst
  • mask
  • nparray

If you've ever wanted to flip part of an image or a mask - not darken it, not erase it, flip it - XOR is the operator you're after, and OpenCV bitwise_xor_1 is it wrapped as a node. XOR compares two images pixel-by-pixel and outputs a 1 wherever they differ, a 0 wherever they match. In the mask world that's the "toggle" primitive: XOR a mask with a filled region and you invert it, XOR it twice and you're back where you started. It's also the cheapest way to spot differences between two versions of the same frame, which is a genuinely handy trick when you're A/B-ing outputs.

This node is part of opencv-comfyui, Gerold Meisinger's auto-generated pack that wraps every top-level standalone function of OpenCV (cv2). The pack's own README opens with "Expect dragons!" and it means it - these nodes are thin, faithful, and ugly. But the mechanism is dead simple: the node calls cv2.bitwise_xor(src1, src2, dst, mask) and hands you the nparray result.

The inputs that matter

  • src1 / src2 - the two images. Both come in as NPARRAY, so they have to be BGR, 0–255 uint8 arrays, not ComfyUI's RGB 0–1 tensors. Wire them through Image2Nparray first (the pack's converter) or the byte values won't mean what OpenCV thinks they mean.
  • mask (optional) - a single-channel NPARRAY. If you wire one, XOR only runs where the mask is non-zero and the rest of src1 passes through untouched. This is the part that makes the node useful for targeted region math instead of whole-image toggling.
  • dst (optional) - this is the function's out-parameter, surfaced as an optional input. Leave it unwired. OpenCV allocates the result for you, which is exactly what you want.

Output: one nparray. Feed it to Nparrays2Image to get back a ComfyUI IMAGE and preview it. One honest gotcha: XOR of two photo-like images looks like TV static, because every differing pixel lights up. It shines on masks - binary images where "differs" means something - not on natural photos.

Install

ComfyUI Manager is the easy path: search for opencv-comfyui and install. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Then restart ComfyUI. The pack needs opencv-contrib-python (plus numpy and torch), which you almost certainly already have from some other node - the README calls out that a Cannot import name 'guidedFilter' error means conflicting OpenCV packages, and the fix is to sort out which pack installed which.

Where people get burned

Beyond the NPARRAY conversion dance, two things. First, the _1 in the name is just overload numbering - OpenCV has two signatures for bitwise_xor, and the generator emitted them as _0 and _1. They do the same job here; don't hunt for a behavioral difference. Second, the whole pack only supports batch size 1 - feed it a 2-image batch and Image2Nparray refuses with Only images with batch_size==1 are supported!. Slice with an ImageFromBatch node (length=1) if your video loader hands you frames in a batch.

XOR isn't something you'll reach for every day. But when you need a mask inverted, a region toggled, or two frames diffed, it's the one deterministic operator that does it in a single node and a few milliseconds.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
dstoptNPARRAY
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY