Nodes/ComfyUI CV/cv2.getAffineTransform
ComfyUI Node

cv2.getAffineTransform

Three points in, one warp matrix out

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.getAffineTransform
  • src
  • dst
  • nparray

cv2.getAffineTransform solves the 2×3 matrix that maps one triangle onto another. Give it three source points and the three points you want them to land on, and you get the matrix that does it exactly - no fitting, no RANSAC, no error term, because three point correspondences and an affine transform are exactly matched in degrees of freedom (six unknowns).

That's the entire node. Two inputs, one output. The value is in knowing when a problem is already "I have three correspondences" rather than "I have two hundred matches and want the best fit".

What it's for

Affine means rotation, uniform-or-anisotropic scale, shear and translation - everything a 2×3 matrix can do, i.e. everything that keeps parallel lines parallel. So this is your node when you know the geometry exactly:

  • You found the three corners of a skewed card, screen or label and want them mapped to a rectangle.
  • You have three known landmarks in two views of the same flat thing.
  • You want to place a marker overlay at three surveyed points in a photo.

If you instead have many correspondences from feature matching, don't use this - use CV Find Homography (RANSAC) or estimateAffine2D, which fit robustly and hand you an inlier mask so you can tell whether the fit is real. This node has no opinion about whether your three points are any good.

Inputs and outputs

src - the three source points, an NPARRAY. dst - the three destination points, also an NPARRAY. Both must be point arrays of shape (3,1,2) or (3,2), and they must be in matching order: point 2 of src maps to point 2 of dst. Nothing checks that for you, and a swapped pair produces a valid-looking matrix that warps your image into garbage.

Both sockets are NPARRAY-only - no IMAGE, no MASK. You author the points with the pack's CV Points Literal node, or take them from a contour/detector node and slice out three of them.

The single output is nparray: the 2×3 matrix, ready to wire into cv2.warpAffine's M input. That's the standard pairing - this node describes the motion, warpAffine applies it to the actual pixels. One wrapper detail worth knowing: cv2 writes its result into dst in place, but the pack hands cv2 a private copy, so your input array comes back unmodified and you can safely fan dst out to three other nodes.

Degenerate input is the one failure mode with no error message: if your three source points are collinear they don't define a triangle, and the solve produces a singular matrix that warps the image into a line. cv2 won't stop you.

Install

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"

Restart ComfyUI. Needs Python ≥ 3.12 and a recent ComfyUI on the V3 node API. No models, no downloads.

  • CV Affine Warp - the curated version, if you want one node that warps with a supplied or computed matrix.
  • CV Transform (Rotate/Scale/Shift) - rotate about a centre, scale, shift and flip in one warpAffine, which is what most people actually wanted when they went looking for a matrix.
  • estimateAffine2D - same output shape, fitted from many points with RANSAC and an inlier mask. Reach for that when your points came from a detector rather than from a ruler.

Where it gets burned: this is a points node living in a pack of image nodes, so the most common confusion is feeding it pictures and getting a type error. It's raw geometry - the pixels only enter the story at warpAffine.

Categoryimage/CV/low-level/cv2 G

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.
dstNPARRAY - - - The low-level cv2 function writes its result into this array in place, but this wrapper passes cv2 a private copy, so your input array is never modified. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.

Outputs (1)

NameTypeDescription
nparrayNPARRAY—