cv2.invert
It inverts a matrix, not your image — and that trips up everyone who googles it
- src
- float
- nparray
Say what it is first, because half the people who land on this page want the other thing: cv2.invert inverts a matrix. It is not "negative image". If you want a photographic negative, you want cv2_bitwise_not on a uint8 image, or cv2_subtract from 255, or a contrast-stretch with the band flipped. This node takes a matrix of floating-point numbers and computes its inverse.
It's one of the ~470 raw wrappers in bmad4ever/comfyui_cv. Its input is typed NPARRAY-only, because a matrix is data, not an image - the pack is consistent about keeping those two apart.
Mechanism
Matrix inversion with the method you pick. flags offers cv2's DecompTypes: DECOMP_LU (the default - Gaussian elimination with partial pivoting, the general-purpose choice), DECOMP_CHOLESKY (faster, and only valid for symmetric positive-definite matrices), DECOMP_EIG (symmetric matrices), DECOMP_QR, DECOMP_SVD, and DECOMP_NORMAL to combine with another method. src should be a floating-point M × N matrix; NP-only, no IMAGE link.
Two outputs: a float status and the nparray result. Read the float - cv2 uses it to report whether the requested method actually did the job, and a singular or non-square input is the case where it doesn't. That's also the case where you should think about what you're doing rather than assuming the numbers are fine: an ill-conditioned inverse produces a result that looks like a matrix and is numerically worthless.
The uses worth knowing
Undoing a transform. You have a homography H from CV Find Homography (RANSAC), and you want to go the other way - matching points in the target image back to the source. That's H⁻¹. The pack has a whole pipe for applying matrices to points (CV Transform Points (Scale, Rotate, Translate), CV Paste Through Warp, the map generators), and this is the node that flips the direction.
Getting back from intrinsics. Inverting a 3×3 K is common enough in calibration work that it's almost a idiom. With the right R, K⁻¹ is what turns a pixel into a ray.
Checking whether an inverse is even meaningful. Inverting a matrix you suspect is rank-deficient and comparing against the original is a legitimate way to find out. DECOMP_SVD gives you a pseudo-inverse where a plain inverse wouldn't exist.
That said: if you're solving a linear system, cv2_solve is the right node - inverting explicitly and then multiplying is slower and less numerically stable, and it's the classic first-year linear algebra mistake. Invert when you genuinely need the inverse as an object (to compose it with other transforms, to keep it as data, to apply it to many different inputs), not as a means of solving one system.
Installing the pack
Manager → search 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"
Python ≥ 3.12 and a ComfyUI recent enough for the V3 node API. The OpenCV line to stay near is 5.0.0.93 - the pack's behaviour is curated against it. No models, no contrib needed for this node: core function, matrix maths.
Where people get burned
Expecting an image back. The single most common confusion. Nothing here touches pixels.
Feeding it an integer array. Make it float first - CV Cast Array to float32 or float64. Integer matrices and inverses don't belong together.
Ignoring the float status. If your "inverse" produces wild values, the status was telling you the whole time.
Wrong method for the matrix. DECOMP_LU on a singular matrix, DECOMP_CHOLESKY on a non-symmetric one - pick the method that matches the structure, or fall back to SVD when you don't know.
Inverting a nearly-singular transform. Homographies fitted from collinear or near-collinear points produce an inverse that amplifies every error. If the result is garbage, the fit was the problem.
The standing note about this pack: its README says it was written with heavy LLM involvement, that some code may be overfitted to its own test cases, and that it isn't production-recommended without independent review. For a one-line wrapper around a stable cv2 call, check the result against a known case (a rotation by 30° should invert to a rotation by −30°) and move on.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | input floating-point M x N matrix. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| flagsopt | COMBO | DECOMP_LU | inversion method (cv::DecompTypes) |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| float | FLOAT | — |
| nparray | NPARRAY | — |