Nodes/opencv-comfyui/OpenCV getAffineTransform_0
ComfyUI Node

OpenCV getAffineTransform_0

Compute a 2×3 affine matrix from three point pairs

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getAffineTransform_0
  • src
  • dst
  • nparray

You've got three points on an image and three points where they should be - a detected face that needs straightening, a sign photographed at an angle that should be frontal, a quadrilateral you want to map onto another. getAffineTransform_0 computes the 2×3 affine matrix that maps the first three points onto the second three. That matrix is then the input to an affine warp - the thing that actually moves the pixels.

It's a geometry node, the "solve for the transform" half of the classic warp pipeline. In OpenCV terms: getAffineTransformwarpAffine. This pack wraps the first one; you'd feed its output to cv2.warpAffine (also in this pack) to apply the transformation.

An affine transform is a combination of rotation, scale, translation, and shear - the linear plus offset family. Three point correspondences pin it down exactly: each point pair gives you two equations, three pairs give you six, which is precisely the number of unknowns in a 2×3 matrix. That's why three points, and only three, are needed. It's also why an affine transform can't do perspective - it maps straight lines to straight lines and parallel lines to parallel lines. If your points imply a perspective distortion (a trapezoid mapping to a rectangle), you want getPerspectiveTransform instead, which needs four points. Reaching for affine when you need perspective is a classic mistake; knowing the difference is half the battle.

Inputs

  • src (NPARRAY) - the source points: three (x, y) coordinates, as a 3×2 array.
  • dst (NPARRAY) - the destination points: three (x, y) coordinates, 3×2.

Important subtlety: in this node, dst is not the usual ignored out-parameter. It's a required input - the target points. The "avoid the out-params" rule from the README doesn't apply here; both fields are real inputs. Get that wrong and the matrix comes out backwards (or rather, it computes the reverse transform, which is at least occasionally useful).

Output is nparray, the 2×3 affine matrix - a float matrix, not an image, so don't route it through Nparrays2Image.

Installing

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager: search "opencv-comfyui". Or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Restart ComfyUI. The pack's requirements.txt installs opencv-contrib-python, numpy, and torch.

Common issues

If OpenCV complains about the number of points (error: (-215:Assertion failed) ...), your src/dst arrays aren't 3×2 - get exactly three correspondences. Order matters: source points must line up with destination points position by position, or the transform is garbage that nonetheless "works." And the pack-wide rules hold: Image2Nparray is single-batch only, and mind the BGR/RGB split when points come from image processing.

It's a niche node, but a precise one. If you're doing alignment, deskewing, or any point-driven geometry in ComfyUI, this is the clean way to get the matrix without leaving the graph.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY
dstNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY