Nodes/opencv-comfyui/OpenCV invert_0
ComfyUI Node

OpenCV invert_0

Matrix inverse, NOT inverted colors

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV invert_0
  • src
  • dst
  • float
  • nparray
flags

Stop right there, because the most common thing people want from a node named "invert" is not what this one does. OpenCV invert_0 is linear algebra - it computes the inverse of a matrix. It is not "invert the colors." If you want the negative of an image, that's bitwise_not_0, which ships in this very pack. Every few months someone grabs invert_0, feeds it an image, and gets an OpenCV assertion error or a matrix of nonsense because they expected a photo negative. This article exists so that's not you.

So who does want this? Anyone doing geometry or math inside ComfyUI: inverting a homography, an essential matrix, or a camera matrix; solving linear systems; undoing a transform. If you're building a warp pipeline or a calibration-style workflow, the matrix inverse is the tool. If you're touching pixels, you wanted bitwise_not_0.

How it works

cv2.invert(src, dst, flags) computes the inverse of a square matrix src using the decomposition you pick. The flags integer selects the method:

  • 0 (DECOMP_LU) - Gaussian elimination, fastest, fails on singular matrices.
  • 1 (DECOMP_SVD) - most robust, works even on singular/degenerate matrices.
  • 2 (DECOMP_EIG), 3 (DECOMP_CHOLESKY), 4 (DECOMP_QR) - other decompositions for special cases.

The function returns a float along with the inverted matrix, and what that float means depends on the flag: with DECOMP_LU it's the determinant; with DECOMP_SVD it's a condition-number ratio. Either way it's your singularity signal - if it's zero (or effectively zero), the matrix had no inverse and the result is garbage. That's the number to check before you trust anything downstream.

Inputs and outputs

  • src - NPARRAY, the matrix to invert (square, float).
  • flags - INT, decomposition method: 0 LU (default), 1 SVD, 2 EIG, 3 CHOLESKY, 4 QR. 16 (DECOMP_NORMAL) can be OR'd in when solving least-squares.
  • dst - optional NPARRAY out-parameter; returned on the output anyway.
  • Outputs: float (determinant/condition signal) and nparray (the inverted matrix).

Installing

Part of opencv-comfyui:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
cd opencv-comfyui
pip install -r requirements.txt

or ComfyUI Manager → "opencv-comfyui". No models; deps are opencv-contrib-python, numpy, torch.

Gotchas

The color-inversion confusion is the big one - remember bitwise_not_0. Second: the input must be a square float matrix, not a (H, W, 3) image; feeding an image throws or returns garbage. Third: watch the float output for near-zero and treat the result as invalid when you see it. Finally, invert_0 vs invert_1 are identical overload duplicates - cv2.invert appears twice in the stubs (MatLike/UMat), the generator numbered them, there's no feature difference.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
flagsINT
dstoptNPARRAY

Outputs (2)

NameTypeDescription
floatFLOAT
nparrayNPARRAY