Nodes/opencv-comfyui/OpenCV getDefaultNewCameraMatrix_0
ComfyUI Node

OpenCV getDefaultNewCameraMatrix_0

The Boring Half of Undistorting Your Photos

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getDefaultNewCameraMatrix_0
  • cameraMatrix
  • nparray
imgsize
centerPrincipalPoint

OpenCV getDefaultNewCameraMatrix_0 wraps cv2.getDefaultNewCameraMatrix(), the boring little sibling in the camera-calibration family. It takes a 3×3 camera matrix and returns an adjusted version of it. That's it. It does not remove distortion, it does not know anything about lens coefficients, and it does not touch your image. If that sounds underwhelming, that's because this node is a helper for helpers - you usually reach for it when you're assembling a distortion-correction or stereo pipeline and need a clean, well-behaved camera matrix to feed the next step.

Quick orientation in case calibration is new territory: the camera matrix is the 3×3 object that maps 3D points into 2D pixels, and its four interesting numbers are the focal lengths (fx, fy) and the principal point (cx, cy) - roughly, where the optical axis hits the sensor. Calibration gives you one of these; this node gives you a re-derived one.

The inputs that matter

  • cameraMatrix (NPARRAY) - your calibrated 3×3 matrix, as a numpy array. In this pack, NPARRAY values are OpenCV-style arrays, not Comfy IMAGE tensors, so feed it from a calibration routine's output or construct one (a 3×3 float array).
  • imgsize (STRING) - the image size, entered as a literal: [1920, 1080]. Remember the pack parses these with ast.literal_eval, so 1920, 1080 without brackets will throw an invalid syntax error.
  • centerPrincipalPoint (BOOLEAN) - the only knob that actually changes behavior. False (default) keeps the principal point from your calibration. True returns a matrix with the principal point moved to the exact center of the image.

When you'd actually want it

The centerPrincipalPoint=True case is the practical one. Calibration data often has the principal point slightly off-center, which is correct for the physical camera but annoying when you want a "textbook" matrix - for stereo rectification, for feeding into getOptimalNewCameraMatrix, or when you just want cleaner numbers for math elsewhere in the workflow. Set it to True and you get a normalized matrix that behaves better as a building block.

The output nparray wires into the other camera nodes in this same pack - undistort, initUndistortRectifyMap, and getOptimalNewCameraMatrix. If your actual goal is "my photos have barrel distortion and I want them straight," skip straight to getOptimalNewCameraMatrix instead, which handles the distortion coefficients and the crop ROI together. This node is for the cases where you want to replace the matrix without touching the image.

Install and gotchas

Install via ComfyUI Manager (search "OpenCV" / opencv-comfyui) or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python   # usually already present

Restart and you're done. No model downloads.

The gotchas are the pack's universal ones: keep batch size at 1 (Image2Nparray will refuse otherwise), and format imgsize as a bracket literal. If you leave imgsize as an empty [], OpenCV uses the default behavior - but given it's a required input here, just type the real size and avoid surprises. The _1 variant of this node is byte-for-byte identical - the generator numbered OpenCV's overloads and these two collapsed to the same signature. Pick either; there's no functional difference.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
cameraMatrixNPARRAY
imgsizeSTRING
centerPrincipalPointBOOLEAN

Outputs (1)

NameTypeDescription
nparrayNPARRAY