OpenCV getOptimalNewCameraMatrix_1
The Undistortion Helper's Identical Twin
- cameraMatrix
- distCoeffs
- nparray
- literal
OpenCV getOptimalNewCameraMatrix_1 is a duplicate of getOptimalNewCameraMatrix_0. The pack's generator walks OpenCV's type stubs, numbers each overload, and cv2.getOptimalNewCameraMatrix has two overloads that flatten to the same signature - so you get two byte-identical nodes. Same six inputs, same two outputs, same behavior. If this is the one you found, everything that follows applies; there is no hidden difference in the _1 version.
What it does (and why you care)
This is the pre-undistortion calculator: feed it your camera matrix, distortion coefficients, and source image size, and it returns an adjusted camera matrix plus the ROI of valid pixels, so undistort can straighten your lens-distorted photo without leaving a border of dead black pixels. The alpha input is the whole knob:
alpha = 0- crop to valid pixels only: no borders, maximum resolution, edges get trimmed.alpha = 1- keep every original pixel: black borders appear.- In between - the trade-off.
0.5is a reasonable starting point.
The inputs
- cameraMatrix (
NPARRAY) and distCoeffs (NPARRAY) - calibration data as OpenCV numpy arrays. - imageSize (
STRING) - source size as a literal,[1920, 1080]style. Brackets matter. - alpha (
FLOAT) - free-scaling parameter, 0–1. - newImgSize (
STRING) - optional output size literal,[]to match source. - centerPrincipalPoint (
BOOLEAN) - usuallyFalsehere.
Outputs are nparray (the tuned 3×3 camera matrix, for undistort) and literal (STRING) - the valid-pixels ROI as a text tuple like (x, y, w, h). That second output is the pack's classic composite-type wart: the ROI is a Rect, so it comes out serialized as a string you'll have to parse back into numbers for a crop node. Annoying, but optional - undistortion works with the matrix alone; the ROI just tells you the honest crop.
Install
Standard for the pack - 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.
Pack-wide gotchas apply: image data travels as NPARRAY (numpy, BGR) with batch size 1 via Image2Nparray/Nparrays2Image, and every Size literal needs brackets or you'll hit invalid syntax (<unknown>, line 0). And the standing advice for duplicates: pick one variant, standardize, and don't diff them looking for differences that don't exist.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| cameraMatrix | NPARRAY | — | |
| distCoeffs | NPARRAY | — | |
| imageSize | STRING | — | |
| alpha | FLOAT | — | |
| newImgSize | STRING | — | |
| centerPrincipalPoint | BOOLEAN | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |
| literal | STRING | — |