Webcam calibration for robot coordinate mapping

How-to: Webcam calibration for robot coordinate mapping

This manual describes the complete procedure for calibrating a webcam and establishing a coordinate transformation between the camera coordinate system and the robot coordinate system.

charuco_calibration_v6.py (21.5 KB)

ring_homography_capture.py (17.4 KB)

The calibration process consists of four steps:

  1. Camera setup and height measurement.
  2. Product height measurement.
  3. Calibration canvas setup at product height.
  4. Matching robot coordinates to camera coordinates.

Each step requires a minimum of 30 calibration measurements, observations or coordinate pairs, depending on the procedure.

The camera calibration is performed using Python, OpenCV and a ChArUco calibration board. The supplied Python script automatically detects calibration markers, collects images and calculates the camera’s intrinsic parameters.

What you need

  • A webcam or industrial camera.
  • A robot with an accessible coordinate system.
  • A measuring tool (e.g. tape measure or digital height gauge).
  • A large, flat calibration canvas.
  • A printed ChArUco calibration board.
  • A Windows computer with Python installed.
  • OpenCV with ArUco support.
  • NumPy.

Software requirements

Install the required Python packages:

pip install opencv-contrib-python numpy

The opencv-contrib-python package is required because the calibration script uses OpenCV’s ArUco and ChArUco functionality.

Steps

1. Set up the camera and measure its height

First, mount the webcam in its intended operating position.

The camera must be securely fixed above the working area to prevent movement during calibration.

Make sure:

  • The entire working area is visible.
  • The camera is securely mounted.
  • The camera is focused correctly.
  • The camera position will remain unchanged during calibration.

Measure the vertical distance between the optical center of the camera lens and the reference plane.

Record at least 30 height measurements to verify the mounting position and measurement consistency.

The following camera settings are used in the supplied Python script:

CAMERA_ID = 0
FRAME_WIDTH = 1920
FRAME_HEIGHT = 1080

DISPLAY_WIDTH = 1280
DISPLAY_HEIGHT = 720

These settings select the first available webcam and configure the requested capture resolution to 1920 × 1080 pixels.

The camera can be opened using OpenCV:

cap = cv2.VideoCapture(CAMERA_ID, cv2.CAP_DSHOW)

If the DirectShow interface cannot open the camera, the script automatically tries the default OpenCV interface.

Important: The actual camera resolution should be checked because the webcam or driver may not support the requested resolution.

2. Measure the product height

Next, measure the height of the product that will be handled by the robot.

Measure the vertical distance between the reference plane and the top surface of the product.

This measurement determines the working plane on which the camera-to-robot coordinate mapping must be established.

Perform at least 30 measurements using different product positions or samples.

Record the measurements and calculate the average product height.

The product height is important because the camera perspective changes depending on the distance between the camera and the observed surface.

The camera’s intrinsic calibration does not automatically compensate for differences in product height. The robot coordinate mapping must be established at the actual working height.

3. Set up a large calibration canvas at product height

Place a large, flat calibration canvas in the working area.

The canvas must be positioned at exactly the same height as the product surface measured in Step 2.

The canvas should cover the complete working area in which the robot will pick or place products.

Identify at least 30 calibration observations, distributed across the canvas.

ChArUco calibration board

The supplied Python script uses a ChArUco board to detect known reference points in the camera image.

The board configuration is defined as follows:

SQUARES_X = 7
SQUARES_Y = 5

SQUARE_LENGTH_MM = 39.0
MARKER_LENGTH_MM = 30.0

ARUCO_DICTIONARY = cv2.aruco.DICT_6X6_250

This means:

  • The board contains 7 × 5 squares.
  • Each square has a physical size of 39 mm.
  • Each ArUco marker has a size of 30 mm.
  • The marker dictionary is DICT_6X6_250.

The printed board must have the correct physical dimensions.

The supplied configuration uses a print scale factor of 1.0. No additional print scaling correction is applied.

Collecting calibration observations

Start the Python calibration script.

The webcam will display a live image with detected ChArUco corners highlighted.

Move the ChArUco board through different positions within the camera’s field of view.

Capture observations across the complete working area, including the corners and edges.

The script uses the following settings:

MIN_CHARUCO_CORNERS = 8

AUTO_CAPTURE = True

AUTO_CAPTURE_DELAY_SECONDS = 2.0
STABLE_TIME_SECONDS = 1.0

REQUIRE_MOVEMENT_PX = 35.0
STABLE_MOTION_PX = 1.25

The automatic capture functionality:

  1. Detects the ChArUco board.
  2. Checks whether at least 8 corners are visible.
  3. Checks whether the board has moved sufficiently since the previous capture.
  4. Waits until the board remains stable for at least 1 second.
  5. Waits for the 2-second capture interval.
  6. Automatically saves the observation.

Collect at least 30 good calibration observations.

The board should be moved to different positions and orientations to obtain a varied dataset.

Important: The ChArUco board is used to calibrate the camera’s intrinsic parameters. The large canvas establishes the physical working plane for the subsequent robot coordinate calibration. These are related but separate calibration operations.

4. Match robot coordinates to camera coordinates

After completing the camera calibration, establish the relationship between the camera coordinate system and the robot coordinate system.

The supplied Python script calculates the camera’s intrinsic parameters, but it does not directly calculate the robot-to-camera coordinate transformation.

This transformation must be established separately using the calibration canvas.

For each calibration point:

  1. Identify a reference point on the calibration canvas.
  2. Detect the corresponding point in the undistorted camera image.
  3. Record its camera pixel coordinates (X, Y).
  4. Move the robot to the corresponding physical point.
  5. Record the robot’s actual coordinates (X, Y).
  6. Store both coordinate sets as a matching data pair.

Repeat this procedure for at least 30 coordinate pairs.

The collected coordinate pairs can be used to calculate a homography transformation.

The transformation converts camera image coordinates into robot working coordinates.

Conceptually:

H, status = cv2.findHomography(
    camera_points,
    robot_points,
    cv2.RANSAC
)

The transformation can subsequently be applied to detected camera positions:

robot_position = cv2.perspectiveTransform(
    camera_position,
    H
)

The camera image should first be undistorted using the intrinsic calibration results.

The robot coordinate system and camera coordinate system must use consistent reference points, axis directions and units.

Important: The homography assumes that the measured points lie on the same physical plane. The calibration must therefore be performed at product height.

Running the calibration software

Save the supplied Python script as:

charuco_calibration.py

Configure the output directory in the script:

OUTPUT_DIR = Path(
    r"C:\Users\stefv\mijn_dataset\charuco_calibration"
)

Run the script from the terminal:

python charuco_calibration.py

The webcam window will open and display the live calibration image.

Keyboard controls

Key Function
C Manually capture and save the current observation
K Run camera calibration and save a checkpoint
S Save a checkpoint without recalibrating
Q Quit the calibration software

Automatic capture is enabled by default, so the script will collect valid observations without requiring the C key.

When at least 30 valid observations have been collected, press K to calculate the camera calibration.

Calibration output files

The script automatically saves the collected observations and calibration results.

The output directory contains:

charuco_calibration/
│
├── intrinsics_checkpoint.npz
│
├── intrinsics_checkpoint.json
│
└── captures/
    ├── frame_XXXXXXXX.png
    ├── frame_XXXXXXXX.png
    └── ...

intrinsics_checkpoint.npz

Contains the numerical camera calibration results:

  • Camera matrix.
  • Distortion coefficients.
  • Image resolution.
  • RMS calibration error.
  • Per-view errors.

intrinsics_checkpoint.json

Contains human-readable calibration information:

  • Calibration timestamp.
  • ChArUco board dimensions.
  • Camera resolution.
  • Calibration RMS.
  • Camera matrix.
  • Distortion coefficients.
  • Number of captured samples.

captures/

Contains the saved images used for the camera calibration.

Important code configuration: minimum 30 observations

The supplied script currently allows calibration with a minimum of 8 observations.

To enforce the requirement of at least 30 observations, change the following check inside calibrate_from_samples():

Original:

if len(samples) < 8:
    raise RuntimeError(
        "Te weinig samples voor een stabiele calibratie."
    )

Replace with:

if len(samples) < 30:
    raise RuntimeError(
        "Minimum 30 calibration observations required."
    )

Also change the second validation check:

Original:

if len(all_charuco_corners) < 8:
    raise RuntimeError(
        "Er zijn te weinig bruikbare detections om te calibreren."
    )

Replace with:

if len(all_charuco_corners) < 30:
    raise RuntimeError(
        "Minimum 30 valid ChArUco detections required."
    )

This ensures that the camera calibration cannot be calculated with fewer than 30 usable observations.

The memory limit can remain at 200 observations:

MAX_SAMPLES_TO_KEEP_IN_MEMORY = 200

Calibration validation

After completing all four steps, validate the calibration results.

  • Check whether the camera detects the calibration points correctly.
  • Verify that the robot reaches the corresponding physical positions.
  • Test multiple positions across the complete working area.
  • Pay particular attention to the edges and corners.
  • Calculate the positioning error between the expected and actual robot positions.
  • Repeat the calibration if the positioning error exceeds the application’s acceptable tolerance.

The RMS value generated by OpenCV represents the camera calibration reprojection error. It is not the same as the robot positioning error. Both should be evaluated separately.

Summary

Step Description Minimum
1 Camera setup and height measurement 30 measurements
2 Product height measurement 30 measurements
3 Calibration canvas and ChArUco observations 30 observations
4 Camera-to-robot coordinate matching 30 coordinate pairs

Final result

After completing the calibration procedure, the system has:

  • A calibrated webcam with known intrinsic parameters.
  • Corrected lens distortion.
  • A defined working plane at product height.
  • A transformation between camera coordinates and robot coordinates.
  • A validated coordinate mapping for robot positioning.

The resulting calibration data can be used as the basis for camera-guided robot pick-and-place operations.