Nodes/opencv-comfyui/OpenCV warpPerspective_0
ComfyUI Node

OpenCV warpPerspective_0

Straighten any photo of a screen, sign, or page with a 3×3 homography

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV warpPerspective_0
  • src
  • M
  • dst
  • nparray
dsize
flags
borderMode
borderValue

Take a photo of a whiteboard, a billboard, or a phone screen at an angle and you see the same problem: it's a trapezoid when it should be a rectangle. warpPerspective is the fix. It's the 3×3-matrix sibling of warpAffine that handles perspective - the "parallel lines converge in the distance" distortion that no affine transform can touch - and in ComfyUI it's how you flatten a photographed plane back into a clean rectangular element you can composite onto or edit.

How it works

Perspective warping is driven by a 3×3 homography matrix M, which maps four points in the source image to four points in the output. Because a homography has 8 degrees of freedom, four point-correspondences pin it down exactly - which is why the classic workflow is: pick the four corners of the tilted object, get M from getPerspectiveTransform_0 (in this pack: takes src and dst point sets as NPARRAYs, plus a solveMethod int), and warp. The output is the image as if you'd been standing directly in front of the plane. This is the exact same math document-scan apps and street-photo-to-menu readers have used for years.

Inputs and outputs

  • src - the NPARRAY image (through Image2Nparray first).
  • M - the 3×3 homography as NPARRAY, usually from getPerspectiveTransform_0. Note the difference from affine: this matrix is 3×3, not 2×3.
  • dsize - output size as a literal string, (1280, 960) style.
  • flags - interpolation: 1 linear (fine for most), 0 nearest, 2 cubic, 4 Lanczos. For perspective warps of photography, 1 or 2 both look fine; for pixel art stay on 0.
  • borderMode / borderValue - edges outside the warped quad. 0 constant fill with borderValue (literal, e.g. (255, 255, 255) for a white background), 1 replicate.

Skip the optional dst. Output is one nparray - a flattened, perspective-corrected image to hand back to Nparrays2Image.

Installing

Same as the whole pack. ComfyUI Manager, search "opencv-comfyui", install, restart. Or:

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

requirements.txt pulls opencv-contrib-python, numpy, torch.

Common issues

  • Getting the point order wrong in getPerspectiveTransform_0 produces a folded or exploded warp. The four points must be a consistent clockwise (or counter-clockwise) quad in both src and dst - match corners to corners.
  • invalid syntax (<unknown>, line 0) on dsize/borderValue - the literals must be valid Python tuples, (1280, 960), not space-separated.
  • Unexpected blank regions - either M is degenerate (three collinear points) or borderMode is filling the outside with constant. Fix the quad or switch to 1.

Where it fits

In a generation pipeline, warpPerspective_0 is the honest post-process move: instead of prompting for "a sign perfectly facing camera," generate the sign flat and warp it onto the photographed surface. It's deterministic, instant, and it's exactly the kind of cheap geometric primitive the community keeps re-discovering it should have used. One light aside: your four-corner clicks need real accuracy - the homography will faithfully preserve your sloppy corners.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
srcNPARRAY
MNPARRAY
dsizeSTRING
flagsINT
borderModeINT
borderValueSTRING
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY