OpenCV warpPerspective_0
Straighten any photo of a screen, sign, or page with a 3×3 homography
- src
- M
- dst
- nparray
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- theNPARRAYimage (throughImage2Nparrayfirst).M- the 3×3 homography asNPARRAY, usually fromgetPerspectiveTransform_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:1linear (fine for most),0nearest,2cubic,4Lanczos. For perspective warps of photography,1or2both look fine; for pixel art stay on0.borderMode/borderValue- edges outside the warped quad.0constant fill withborderValue(literal, e.g.(255, 255, 255)for a white background),1replicate.
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_0produces a folded or exploded warp. The four points must be a consistent clockwise (or counter-clockwise) quad in bothsrcanddst- match corners to corners. invalid syntax (<unknown>, line 0)ondsize/borderValue- the literals must be valid Python tuples,(1280, 960), not space-separated.- Unexpected blank regions - either
Mis degenerate (three collinear points) orborderModeis filling the outside with constant. Fix the quad or switch to1.
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.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | — | |
| M | NPARRAY | — | |
| dsize | STRING | — | |
| flags | INT | — | |
| borderMode | INT | — | |
| borderValue | STRING | — | |
| dstopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |