cv2.bitwise_not
The boolean that also works on photographs
- src
- mask
- result
An inversion. On a mask it's the complement - "everything except this region", the single most-typed operation in any masking graph. On an 8-bit image it's 255 − x, which is a photographic negative, and that's a legitimate thing to want too.
It's the one member of the bitwise family you can point at a picture without it being nonsense, because inverting 8-bit values happens to produce a meaningful image. That's a coincidence of two's-complement-ish arithmetic being the same as a subtraction in the range you care about, not a designed property.
How it works
Every bit flipped: NOT x. On a uint8 in 0–255 that's 255 − x - black becomes white, white becomes black, mid-grey is its own inverse. On a MASK, where the conversion produces 0 or 255, that's a clean complement.
The wrapper accepts an optional mask that restricts which output elements are inverted - everything outside it isn't touched. You will almost never want this; it's how OpenCV expresses "invert here, leave the destination elsewhere", and since there's no dst input on this node, don't lean on what sits outside the mask.
Output echoes the input's format, so MASK in → MASK out, which makes this node disposable anywhere in a mask chain. The pack uses it in a dozen of its own workflows for exactly that.
Inputs and outputs
src(COMFY_MATCHTYPE_V3) - array or image; decides the output format.mask(optional) - restrict the inversion to the masked elements.- result - echoes
src.
What it's for
- Complement a mask. Foreground mask → background mask, with no fuss. The pack's inpainting, salt-and-pepper and paste-through-homography subgraphs all do this.
- Choose the other side of a threshold. A threshold that produced "the ink" gives you "the paper" after an inversion - often fewer wires than re-running the threshold with
THRESH_BINARY_INV. - Detect the subtle, not the obvious. Inverting a luminance channel before comparison makes a small bright defect stand out against its surroundings rather than disappearing into them.
- Photographic negative. It's an aesthetic option, and it's instant.
- Mask the inverse for compositing. Invert a subject mask to get the plate's half of a premultiplied composite.
Install
ComfyUI Manager → comfyui_cv (ComfyUI CV), or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Restart ComfyUI. Needs Python ≥ 3.12 and a recent ComfyUI on the V3 node API; behaviour is curated against OpenCV 5.0.0.93. Keep the contrib wheel - a non-contrib opencv-python installed over it silently empties the contrib submodules.
Where people get burned
Inverting a float array. Bitwise-not on IEEE float data inverts exponent and mantissa bits, not values. You'll get garbage that looks like noise. What you mean is 1 − x: do it with cv2_subtract against a CV Scalar of 1.0, or cv2_addWeighted with alpha = −1 and gamma = 1. And if the array is a LATENT, note the pack refuses bitwise ops on latents by design.
Inverting a normalised score map. 1 − x is a different question from "where is the score low"; thresholds and comparisons are the tools there. Inversion is for masks and for pixels.
Expecting the mask to blend. It doesn't - it fences the operation. Unmasked regions come back with zero rather than with the original values (there's no dst to preserve), which surprises people who wired a mask "just to be safe".
Double inversion. Easy to do when two subgraphs each assume they're the one flipping polarity, and then your mask is upside-down in a way that looks like the detector failing rather than the algebra. Inspect once with a preview node.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| src | COMFY_MATCHTYPE_V3 | input array. The image output(s) echo this input's format. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. | |
| maskopt | NPARRAY,IMAGE,MASK | optional operation mask, CV_8U, CV_8S or CV_Bool single channel array, that specifies elements of the output array to be changed. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result | COMFY_MATCHTYPE_V3 | Echoes the 'src' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY. |