DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Calibration Reader
  • Similar samples
  • Camera intrinsics
  • Setup
  • Source code

Calibration Reader

This example shows how to read calibration data stored on device over XLink. This example will print camera extrinsic and intrinsic parameters, along with other calibration values written on device (EEPROM).

Similar samples

Camera intrinsics

Calibration also contains camera intrinsics and extrinsics parameters.
Command Line
1import depthai as dai
2
3with dai.Device() as device:
4  calibData = device.readCalibration()
5  intrinsics = calibData.getCameraIntrinsics(dai.CameraBoardSocket.RIGHT)
6  print('Right mono camera focal length in pixels:', intrinsics[0][0])
Here's theoretical calculation of the focal length in pixels:
Python
1f_x = width * (1 / (2 * math.tan((HFOV / 2) * (math.pi / 180))))
To get the HFOV you can use this script, which also works for wide-FOV cameras and allows you to specif alpha parameter.With 400P (640x400) camera resolution where HFOV=71.9 degrees:
Code
1f_x_640 = 640 * (1 / (2 * math.tan((71.9 / 2) * (math.pi / 180))))  # = 441.25
And for 800P (1280x800) camera resolution where HFOV=71.9 degrees:
Code
1f_x_1280 = 1280 * (1 / (2 * math.tan((71.9 / 2) * (math.pi / 180))))  # = 882.5

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import depthai as dai
4import numpy as np
5import sys
6from pathlib import Path
7
8# Connect Device
9with dai.Device() as device:
10    calibFile = str((Path(__file__).parent / Path(f"calib_{device.getMxId()}.json")).resolve().absolute())
11    if len(sys.argv) > 1:
12        calibFile = sys.argv[1]
13
14    calibData = device.readCalibration()
15    calibData.eepromToJsonFile(calibFile)
16
17    M_rgb, width, height = calibData.getDefaultIntrinsics(dai.CameraBoardSocket.CAM_A)
18    print("RGB Camera Default intrinsics...")
19    print(M_rgb)
20    print(width)
21    print(height)
22
23    if "OAK-1" in calibData.getEepromData().boardName or "BW1093OAK" in calibData.getEepromData().boardName:
24        M_rgb = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.CAM_A, 1280, 720))
25        print("RGB Camera resized intrinsics...")
26        print(M_rgb)
27
28        D_rgb = np.array(calibData.getDistortionCoefficients(dai.CameraBoardSocket.CAM_A))
29        print("RGB Distortion Coefficients...")
30        [print(name + ": " + value) for (name, value) in
31         zip(["k1", "k2", "p1", "p2", "k3", "k4", "k5", "k6", "s1", "s2", "s3", "s4", "τx", "τy"],
32             [str(data) for data in D_rgb])]
33
34        print(f'RGB FOV {calibData.getFov(dai.CameraBoardSocket.CAM_A)}')
35
36    else:
37        M_rgb, width, height = calibData.getDefaultIntrinsics(dai.CameraBoardSocket.CAM_A)
38        print("RGB Camera Default intrinsics...")
39        print(M_rgb)
40        print(width)
41        print(height)
42
43        M_rgb = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.CAM_A, 3840, 2160))
44        print("RGB Camera resized intrinsics... 3840 x 2160 ")
45        print(M_rgb)
46
47
48        M_rgb = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.CAM_A, 4056, 3040 ))
49        print("RGB Camera resized intrinsics... 4056 x 3040 ")
50        print(M_rgb)
51
52
53        M_left, width, height = calibData.getDefaultIntrinsics(dai.CameraBoardSocket.CAM_B)
54        print("LEFT Camera Default intrinsics...")
55        print(M_left)
56        print(width)
57        print(height)
58
59        M_left = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.CAM_B, 1280, 720))
60        print("LEFT Camera resized intrinsics...  1280 x 720")
61        print(M_left)
62
63
64        M_right = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.CAM_C, 1280, 720))
65        print("RIGHT Camera resized intrinsics... 1280 x 720")
66        print(M_right)
67
68        D_left = np.array(calibData.getDistortionCoefficients(dai.CameraBoardSocket.CAM_B))
69        print("LEFT Distortion Coefficients...")
70        [print(name+": "+value) for (name, value) in zip(["k1","k2","p1","p2","k3","k4","k5","k6","s1","s2","s3","s4","τx","τy"],[str(data) for data in D_left])]
71
72        D_right = np.array(calibData.getDistortionCoefficients(dai.CameraBoardSocket.CAM_C))
73        print("RIGHT Distortion Coefficients...")
74        [print(name+": "+value) for (name, value) in zip(["k1","k2","p1","p2","k3","k4","k5","k6","s1","s2","s3","s4","τx","τy"],[str(data) for data in D_right])]
75
76        print(f"RGB FOV {calibData.getFov(dai.CameraBoardSocket.CAM_A)}, Mono FOV {calibData.getFov(dai.CameraBoardSocket.CAM_B)}")
77
78        R1 = np.array(calibData.getStereoLeftRectificationRotation())
79        R2 = np.array(calibData.getStereoRightRectificationRotation())
80        M_right = np.array(calibData.getCameraIntrinsics(calibData.getStereoRightCameraId(), 1280, 720))
81
82        H_left = np.matmul(np.matmul(M_right, R1), np.linalg.inv(M_left))
83        print("LEFT Camera stereo rectification matrix...")
84        print(H_left)
85
86        H_right = np.matmul(np.matmul(M_right, R1), np.linalg.inv(M_right))
87        print("RIGHT Camera stereo rectification matrix...")
88        print(H_right)
89
90        lr_extrinsics = np.array(calibData.getCameraExtrinsics(dai.CameraBoardSocket.CAM_B, dai.CameraBoardSocket.CAM_C))
91        print("Transformation matrix of where left Camera is W.R.T right Camera's optical center")
92        print(lr_extrinsics)
93
94        l_rgb_extrinsics = np.array(calibData.getCameraExtrinsics(dai.CameraBoardSocket.CAM_B, dai.CameraBoardSocket.CAM_A))
95        print("Transformation matrix of where left Camera is W.R.T RGB Camera's optical center")
96        print(l_rgb_extrinsics)

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.