Lesson 6 of 9 · 28m
Camera Calibration and Undistortion
Estimate intrinsics and distortion with calibration targets, validate reprojection error, undistort images correctly, and avoid calibration theater.
Camera calibration is the least glamorous part of vision and the part that decides whether measurements deserve trust. If your application measures distance, estimates pose, stitches wide-angle images, or maps pixels into the physical world, lens distortion and focal length are not implementation details. They are the measurement system.
Intrinsics describe the camera, not the scene
The camera matrix maps 3D camera coordinates into pixel coordinates:
fx 0 cx
0 fy cy
0 0 1fx and fy are focal lengths in pixel units; cx and cy are the principal point. Distortion coefficients describe radial and tangential bending. Resolution matters: a calibration at 1920x1080 does not automatically apply to 640x480 unless you scale it correctly.
Capture calibration data like a measurement engineer
A checkerboard or ChArUco board must be flat, well lit, printed accurately, and observed across the image. Capture views near edges and corners, at different distances and angles:
pattern = (9, 6) # inner corners, not squares
objp = np.zeros((pattern[0] * pattern[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:pattern[0], 0:pattern[1]].T.reshape(-1, 2)
objp *= square_size_metersDetect corners and refine them:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ok, corners = cv2.findChessboardCorners(gray, pattern, None)
if ok:
corners = cv2.cornerSubPix(
gray, corners, (11, 11), (-1, -1),
(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001),
)Do not include detections that are partially occluded, motion blurred, or geometrically implausible. Bad calibration images produce a precise-looking calibration of the wrong camera.
Calibrate and inspect reprojection error
rms, camera_matrix, dist, rvecs, tvecs = cv2.calibrateCamera(
object_points, image_points, gray.shape[::-1], None, None
)The returned RMS error is in pixels. What is acceptable depends on resolution and application, but a low number is not enough. Inspect per-view error. One terrible view can dominate or reveal a bad corner detection. Check whether cx and cy land plausibly near the image center for a normal lens.
Undistort with intention
h, w = img.shape[:2]
new_matrix, roi = cv2.getOptimalNewCameraMatrix(
camera_matrix, dist, (w, h), 1, (w, h)
)
fixed = cv2.undistort(img, camera_matrix, dist, None, new_matrix)alpha controls cropping versus retaining invalid black border pixels. Keeping every original pixel can introduce empty regions; cropping aggressively loses coverage. For video, precompute undistortion maps with cv2.initUndistortRectifyMap and use cv2.remap rather than recomputing.
Calibration is stateful and operational
Cameras get bumped, lenses get refocused, housings vibrate, and resolution settings change. Store calibration with camera serial number, lens, focus setting, resolution, date, board specification, and validation metrics. If focus changes focal length or a operator swaps lenses, the old calibration is historical data.
ChArUco is usually worth the trouble
Plain checkerboards require full visibility. ChArUco combines checkerboard geometry with ArUco markers, making partial observations and pose labeling more robust. In field conditions — glare, occlusion, awkward mounting — that robustness matters more than the elegance of a perfectly printed chessboard.
Field notes
- Count inner corners, not squares; off-by-one pattern sizes are the classic calibration failure.
- Do not autofocus during calibration capture. Focus changes effective intrinsics.
- Capture target views across the full image. Center-only calibration cannot model edge distortion well.
- Validate calibration by measuring known distances, not only by reading RMS.
- Store raw calibration images. They let you re-estimate when you discover a bad detection months later.
Was this lesson helpful?