Nodes/opencv-comfyui/OpenCV initUndistortRectifyMap_0
ComfyUI Node

OpenCV initUndistortRectifyMap_0

Camera calibration's map builder — only reach for this if you have calibration data

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV initUndistortRectifyMap_0
  • cameraMatrix
  • distCoeffs
  • R
  • newCameraMatrix
  • map1
  • map2
  • nparray_0
  • nparray_1
size
m1type

initUndistortRectifyMap_0 is the most specialized node in this pack, and you should treat it accordingly: it doesn't process an image at all. It computes the remap tables that tell OpenCV how to correct a photo for lens distortion - barrel/pincushion distortion, the pincushion you get from cheap wide lenses - so that the result looks like it was shot by a mathematically ideal camera. The actual correction happens later, when you feed those tables into remap_0 along with your image.

Who is this for? People with real camera calibration data. cv2.initUndistortRectifyMap takes the outputs of cv2.calibrateCamera - the camera matrix, distortion coefficients, a rectification transform, an adjusted camera matrix - the fruits of photographing a checkerboard pattern from many angles. If you've done camera calibration before, you know the drill. If you haven't, this node is a wall: it needs matrices you don't have, and there's no shortcut to them. There is no "auto-fix lens distortion" here, and nothing in ComfyUI's world is going to produce these matrices for you. This is computer-vision plumbing, in the honest sense - a deterministic OpenCV function that the post-processing layer of ComfyUI has no other home for.

How it works. Given the intrinsic camera model, it builds two maps (map1, map2) of the same size as the output image. Each map says, for every output pixel, where to sample the source pixel from. remap_0 then applies them. Because the maps are computed once, you compute them for a given camera and reuse them across every frame from that camera - which is exactly why this is a map builder and not a filter: the expensive per-frame work is the remap, and the map is the one-time setup.

Inputs - all of them matter, and none is friendly:

  • cameraMatrix (NPARRAY) - the 3×3 intrinsic matrix.
  • distCoeffs (NPARRAY) - the distortion coefficient vector (often 4 or 5 values).
  • R (NPARRAY) - rectification transform; identity (np.eye(3)) if you don't need rectification.
  • newCameraMatrix (NPARRAY) - the adjusted matrix for the output; typically the camera matrix with sensible intrinsics, or the free-scaling version.
  • size (STRING) - the output image size as a Python literal, e.g. (1920, 1080). This is one of the pack's composite params parsed with ast.literal_eval, so it must be exactly (width, height) - a common place to hit the invalid syntax error.
  • m1type (INT) - map precision: 5 (CV_32FC1, float maps) for accuracy, 3 (CV_16SC2) for faster/less-memory remaps. 5 unless you have a speed problem.

The optional map1/map2 are out-parameters; the README's rule is to leave them alone - you get the maps through the outputs anyway.

Outputs. Two NPARRAYs, nparray_0 (map1) and nparray_1 (map2). Wire both into remap_0 (its map1 and map2 inputs) along with your image.

Installing. ComfyUI Manager → search "opencv-comfyui", or:

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

Restart ComfyUI; it's under image/OpenCV.

Where people get burned. The size literal - forget the parentheses or use square brackets in the wrong way and you get the README's invalid syntax error; it must be (w, h). Feeding the wrong-shape matrices throws OpenCV assertion errors. And the _1 twin (initUndistortRectifyMap_1) is, as everywhere in this pack, identical generated code - same call, same inputs. If you get this far, you're in calibrated-camera territory where you already know which node to grab: either one.

Categoryimage/OpenCV

Inputs (8)

NameTypeDefaultDescription
cameraMatrixNPARRAY
distCoeffsNPARRAY
RNPARRAY
newCameraMatrixNPARRAY
sizeSTRING
m1typeINT
map1optNPARRAY
map2optNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY