OpenCV getPerspectiveTransform_0
The One Node in This Pack You'll Actually Build Workflows Around
- src
- dst
- nparray
OpenCV getPerspectiveTransform_0 computes the 3×3 homography that maps four points in one image to four points in another - the mathematical heart of perspective correction. It wraps cv2.getPerspectiveTransform(src, dst, solveMethod). Give it the four corners of a tilted document or screen in one image and the four corners of where you want them, and it returns the matrix that, fed to the pack's warpPerspective node, straightens the whole thing out. This is the "make that photographed whiteboard or business card front-facing" node, and it's one of the genuinely useful ones in a pack the author himself warns contains dragons.
How it works
A perspective transform is an 8-degrees-of-freedom mapping, which is why it needs exactly four point correspondences: four source points and four destination points give you eight constraints, enough to solve for the homography uniquely (up to scale). The src and dst inputs are point sets, and this is the pack's awkward part - they're NPARRAY inputs, not drawn-on-image selections. Each is a (4, 2) float array (or (4, 1, 2)), meaning you have to construct the point arrays yourself. There's no click-on-the-corners UI here; you're typing coordinates, ideally from an upstream point-detection or coordinate node.
- src (
NPARRAY) - 4×2 array of source points. - dst (
NPARRAY) - 4×2 array of destination points. - solveMethod (
INT) - the linear solver:0=DECOMP_LU,1=DECOMP_SVD. SVD is more numerically stable for near-degenerate point sets; LU is faster and fine for clean rectangles.
Output is a single nparray: the 3×3 homography matrix. That's it - no warped image. To see the result you chain it into warpPerspective (same pack), with your image and target size.
When you'd reach for it
Document scanning, screen capture cleanup, removing keystone distortion from photos of flat surfaces - any "photograph a rectangle, get a front-on rectangle" job. It's the deterministic, millisecond version of something people are weirdly tempted to prompt a model to do. If you have four corner coordinates (from a corner detector, a QR/localization pass, or plain manual coordinates), this is the correct, precise tool. The catch is generating those four points; the pack has no convenience node for that, so budget some plumbing. And if you have more than four correspondences and want a robust best-fit, that's findHomography's job instead.
Install
ComfyUI Manager, search opencv-comfyui (display "OpenCV"), or:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python
Restart, no models. Remember images travel as NPARRAY (batch size 1) via Image2Nparray, and if your point arrays are computed upstream they must come out as float32 (4, 2) - a shape mismatch throws an OpenCV assertion. The _1 variant is an identical duplicate overload; pick either.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | — | |
| dst | NPARRAY | — | |
| solveMethod | INT | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |