Skip to main content
This tutorial demonstrates camera calibration using OpenCV with a chessboard pattern. We’ll:
  1. Detect chessboard corners in multiple calibration images
  2. Calibrate the camera to find intrinsic parameters and distortion coefficients
  3. Undistort images using the calibration results
  4. Visualize the results to verify calibration quality

Run the Camera Calibration Demo

Open the camera calibration notebook in Google Colab to run the calibration process.

Camera Calibration Process

What happens during calibration:

  1. Camera Calibration: Use cv2.calibrateCamera() to find:
    • Camera matrix (K): Contains focal lengths (fx, fy) and principal point (cx, cy)
    • Distortion coefficients: Correct for lens distortion (radial and tangential)
    • Rotation/translation vectors: Camera pose for each calibration view
  2. Image Undistortion: Apply the calibration to remove lens distortion from images
  3. Results Visualization: Compare original vs. undistorted images

OpenCV Functions for Camera Calibration

initCameraMatrix2D

Finds an initial camera intrinsic matrix from 3D-2D point correspondences. This function provides a good initial estimate for camera calibration by finding the camera matrix that minimizes reprojection error for a set of calibration points. Parameters:
  • objectPoints: Vector of vectors of 3D calibration pattern points
  • imagePoints: Vector of vectors of corresponding 2D image points
  • imageSize: Image size used only to initialize camera intrinsic matrix
  • aspectRatio: If zero, both fx and fy are estimated independently. Otherwise, fx = fy * aspectRatio

findChessboardCorners

Finds the positions of internal corners of the chessboard. This function detects the positions of internal corners in a chessboard calibration pattern. Parameters:
  • image: Source chessboard view (8-bit grayscale or color)
  • patternSize: Number of inner corners per chessboard row and column
  • corners: Output array of detected corners
  • flags: Various operation flags:
    • CALIB_CB_ADAPTIVE_THRESH: Use adaptive thresholding
    • CALIB_CB_NORMALIZE_IMAGE: Normalize image gamma
    • CALIB_CB_FILTER_QUADS: Use additional criteria to filter out false quads
    • CALIB_CB_FAST_CHECK: Run fast check to quickly determine if pattern is present
Returns: True if all corners are found and properly ordered

findChessboardCornersSB

Finds the positions of internal corners using a sector-based approach. This is an improved version of corner detection that’s more robust to lighting conditions and partial occlusions.

estimateChessboardSharpness

Estimates the sharpness of a detected chessboard. This function can be used to assess the quality of calibration images by measuring how sharp the chessboard pattern appears. Parameters:
  • image: Source chessboard view
  • patternSize: Size of the chessboard pattern
  • corners: Chessboard corners detected by findChessboardCorners
  • rise_distance: Rise distance 0.8 means 10% … 90% of the final signal strength
  • vertical: By default, the function checks the vertical edges of the chessboard

drawChessboardCorners

Renders the detected chessboard corners. This function is useful for visualizing detected corners and verifying the correctness of corner detection. Parameters:
  • image: Destination image (color or grayscale, 8-bit)
  • patternSize: Number of inner corners per chessboard row and column
  • corners: Array of detected corners
  • patternWasFound: Parameter indicating whether complete board was found

drawFrameAxes

Draw axes of the world/object coordinate system from pose estimation. This function draws the 3D coordinate system axes to visualize object pose. Parameters:
  • image: Input/output image
  • cameraMatrix: Input camera intrinsic matrix
  • distCoeffs: Input distortion coefficients
  • rvec: Rotation vector
  • tvec: Translation vector
  • length: Length of the drawn axes in the same unit as tvec
  • thickness: Line thickness of the drawn axes

findCirclesGrid

Finds centers in the grid of circles. This function detects a grid of circles pattern, which is an alternative to chessboard patterns for camera calibration. Parameters:
  • image: Grid view of circles (8-bit grayscale or color)
  • patternSize: Number of circles per row and column
  • centers: Output array of detected centers
  • flags: Various operation flags:
    • CALIB_CB_SYMMETRIC_GRID: Grid is symmetric
    • CALIB_CB_ASYMMETRIC_GRID: Grid is asymmetric
    • CALIB_CB_CLUSTERING: Use k-means clustering to find grid
Returns: True if grid is found

calibrateCamera

Finds the camera intrinsic and extrinsic parameters from several views of a calibration pattern. This is the main camera calibration function that estimates all camera parameters. Parameters:
  • objectPoints: Vector of vectors of 3D calibration pattern points
  • imagePoints: Vector of vectors of corresponding 2D image points
  • imageSize: Size of the image used only to initialize camera intrinsic matrix
  • cameraMatrix: Input/output 3x3 camera intrinsic matrix
  • distCoeffs: Input/output vector of distortion coefficients
  • rvecs: Output vector of rotation vectors for each pattern view
  • tvecs: Output vector of translation vectors for each pattern view
  • flags: Different flags for calibration:
    • CALIB_USE_INTRINSIC_GUESS: cameraMatrix contains valid initial values
    • CALIB_FIX_PRINCIPAL_POINT: Principal point is not changed
    • CALIB_FIX_ASPECT_RATIO: Fix fx/fy ratio
    • CALIB_ZERO_TANGENT_DIST: Tangential distortion coefficients are set to zeros
    • CALIB_FIX_K1, CALIB_FIX_K2, etc.: Fix specific distortion coefficients
  • criteria: Termination criteria for iterative optimization algorithm
Returns: Overall RMS re-projection error
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.