OpenCV invertAffineTransform_0
Undo a warp with one matrix
- M
- iM
- nparray
OpenCV invertAffineTransform_0 wraps cv2.invertAffineTransform, and it does one clean job: take a 2×3 affine transform matrix and produce the matrix that undoes it. If you've warped an image with warpAffine_0 (also in this pack) using a matrix from getRotationMatrix2D_0, this node gives you the inverse matrix that warps it back - no manual 3×3 expansion, no hand-inverting.
Affine transforms are the simple geometry class: rotation, scaling, translation, shearing, and any combination, all packed into a 2×3 matrix. They preserve straight lines and parallelism. The inverse matrix is exactly what you need the moment a workflow has to round-trip - warp a region for processing, then un-warp the result back into place. In ComfyUI that pattern shows up in crop-and-replace pipelines, perspective-stabilization experiments, and anything that transforms a frame twice.
How it works
cv2.invertAffineTransform(M, iM) mathematically inverts the 2×3 affine matrix M. The math is straightforward (OpenCV computes the inverse of the 2×2 linear part and applies it to the translation), which means it's fast, deterministic, and has no tunable parameters - the only "knob" is whether the matrix is actually invertible. A degenerate matrix (e.g., a scale of zero collapsed a dimension) has no inverse, and OpenCV will happily return garbage rather than complaining, so sanity-check your inputs.
Inputs and outputs
M- NPARRAY, the 2×3 affine transform matrix to invert.iM- optional NPARRAY out-parameter; returned on the output anyway.- Output
nparray- the 2×3 inverse matrix.
Wire M from getRotationMatrix2D_0 or warpAffine_0-style sources. Then feed the output back into a warpAffine_0 with the same interpolation to run the transform forward and backward.
Installing
From 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 biggest trap is a silent one: if M is singular (a zero scale, duplicate rows), the "inverse" comes back as nonsense without an error. There's no built-in check, so verify by composing M * iM if it matters. Also, this node is about transform matrices, not images - you can't feed it a picture and get the picture back. That's invert_0 territory, and even that is matrix math. And invertAffineTransform_0 vs _1? Identical overload duplicates, as always in this pack. Pick one.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| M | NPARRAY | — | |
| iMopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |