ComfyUI Node

IG_Undistort

Un-bend that wide-angle photo with IG_Undistort

By iFREEGROUP·Created 2 years ago·Updated 2 years ago· 2
IG_Undistort
  • image
  • matrix
  • dist_coef
  • image

This is the payoff node in iFREEGROUP/comfyui-undistort. The other two either calibrate a camera (IG_LoadCheckerboardImageForCalibrateCamera) or print its numbers (IG_MatrixAndDistCoefToText); this one actually fixes the image. Feed it a distorted photo plus the calibration for the same camera, and out comes the rectified version, ready for the rest of the graph.

It's the node you reach for when your pipeline is choking on real-world geometry: a phone wide-angle that bends door frames, an action cam's barrel distortion, the curving edges of a photographed document. Undistort first, then img2img, upscaling, or IPAdapter sees something that matches the pinhole-camera assumption generative pipelines quietly make. It's the opposite of what photorealism guides chase - they treat lens distortion as a realistic-capture tell; this strips the lens out so the content, not the glass, drives what happens next.

How it works

OpenCV's classic two-step, straight from the handbook. First cv2.getOptimalNewCameraMatrix computes a corrected camera matrix with alpha=0, which means the black, empty corners you'd otherwise get from un-bending a frame are cropped away instead. Then cv2.undistort remaps the pixels, the node crops to the valid region, and hands the result back as a normal ComfyUI IMAGE tensor.

Inputs and output

Three required inputs:

  • image - any IMAGE. A LoadImage output, or even something generated elsewhere in the graph.
  • matrix and dist_coef - the MATRIX and DIST_COEF from IG_LoadCheckerboardImageForCalibrateCamera, calibrated for the same camera and lens that took the photo.

One output: image, the corrected frame. Wire it to a preview or directly into whatever needs clean geometry.

What to expect, and the sharp edges

Three things will trip you up, and all three are visible in the source rather than rumor:

  • It handles one image at a time. The node squeezes off the batch dimension (squeeze(0)), so if you feed it a batch of frames it silently processes only the first. Keep it on single images.
  • The output is cropped, not padded. Because alpha=0, the corrected image comes back slightly smaller and reframed; the edges you "saved" from bending get cut. That's intended, but it surprises people the first time.
  • There's a latent height/width swap. The node passes (h1, w1) to getOptimalNewCameraMatrix, which expects (width, height). On landscape images it mostly washes out; on tall, portrait-orientation shots the ROI can land slightly off, which shows up as a subtly wrong crop. If you shoot verticals, eyeball the output carefully.

Install

One install covers all three nodes in the pack. ComfyUI Manager → search "comfyui-undistort", or:

cd ComfyUI/custom_nodes
git clone https://github.com/iFREEGROUP/comfyui-undistort

then restart ComfyUI. requirements.txt is just opencv-python, which your ComfyUI install almost certainly already has.

One more thing: a calibration from a different lens is worse than no calibration. If the output looks more bent than the input, you're almost certainly feeding it a matrix from the wrong camera - recalibrate, don't blame the node.

CategoryIG

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
matrixMATRIX
dist_coefDIST_COEF

Outputs (1)

NameTypeDescription
imageIMAGE