cv2.getDefaultNewCameraMatrix
The cheap camera matrix you need before you can undistort anything
- cameraMatrix
- imgsize
- nparray
Undistortion needs three things: the intrinsics the camera was calibrated with (K), the distortion coefficients that came with them (D), and a second camera matrix describing the camera you wish you had. Most people spend their entire first undistortion session confused about that third matrix, because the calibration only ever gave them two.
cv2.getDefaultNewCameraMatrix produces that third matrix the lazy way. Feed it your calibration K, optionally the frame size, and you get a new K back - no scaling parameter to tune, no valid-pixel ROI, no decision to make about how much of the frame survives.
The two behaviours
centerPrincipalPoint = false(the default): you get an exact copy of your input matrix. That's the "my calibration's principal point is fine, I just need a matrix in this slot" case, and it's a perfectly legitimate answer.centerPrincipalPoint = true: you get the same focal lengths, but the principal point is moved to the centre of the image. That's what you want when the calibration put the optical centre somewhere odd - a cropped sensor, an off-centre mount - and you'd rather the undistorted result be centred than geometrically faithful.
imgsize is a two-component (w, h) value and it only matters in the second case, because the new principal point is derived from it. Wire it from CV Tuple or type it in place, the same as every other size input in this pack.
The single output is nparray: a 3×3 matrix, ready for the newCameraMatrix input of cv2.undistort or cv2.initUndistortRectifyMap.
When to use this instead of the full thing
The pack also wraps cv2.getOptimalNewCameraMatrix, which is the real workhorse: it takes an alpha and returns both a new matrix and the ROI of pixels that survived, letting you trade black borders against field of view. Reach for it when you care about the framing of the undistorted result.
Reach for getDefaultNewCameraMatrix when you don't: you're rectifying stereo pairs (where the rectification supplies the geometry anyway), or you're scaling a calibration to a different resolution and just want the focal lengths carried over with the centre kept sane. It's two knobs and no judgement calls, which is sometimes what you want at 1am.
Where people get burned
- Turning on
centerPrincipalPointand leavingimgsizeat(0, 0). You've just asked for the principal point of a 0×0 image. Set the size to the frame you're actually undistorting. - A 3×4 projection matrix in. This wants
K- the 3×3 intrinsics - not the full[K | t]projection. If your matrices came out of astereoRectifyor a homography decomposition, check which one you're holding. KandDare a pair. A new camera matrix derived from one calibration applied to distortion coefficients from a different one produces a warped, wrong-looking result that increases confidence in nothing. The pack hasCV Load Camera Params/CV Save Camera Paramsfor keeping them together on disk.- The whole family is pixel-unit geometry. Change the resolution and the numbers stop meaning what you think they mean; that's why the size input exists rather than being inferred.
Install
Manager → search ComfyUI CV, or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Restart ComfyUI. Python ≥ 3.12 and a recent ComfyUI on the V3 node API; no models, no downloads. The surrounding workflow - chessboard detection, calibrateCamera, saving intrinsics and distortion to a file - is what workflows 49–54 in the pack walk through, and the matrix node here is the small piece those pipelines need in the middle.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| cameraMatrix | NPARRAY | Input camera matrix. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| imgsizeopt | CV_TUPLE | 0,0 | Camera view image size in pixels. One value with 2 components (w, h) - it travels as a whole, so it cannot arrive half-connected. Wire it from 'CV Tuple' or type the components in place. |
| centerPrincipalPointopt | BOOLEAN | false | Location of the principal point in the new camera matrix. The parameter indicates whether this location should be at the image center or not. Preset to the OpenCV default (False). |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |