Calibration Reader

This example shows how to read calibration data stored on device over XLink.

Similiar samples:

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

git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py

For additional information, please follow installation guide

Source code

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3

from pathlib import Path
import cv2
import depthai as dai
import numpy as np
import time
import argparse

# Connect Device
with dai.Device() as device:

    calibData = device.readCalibration()
    M_rgb, width, height = np.array(calibData.getDefaultIntrinsics(dai.CameraBoardSocket.RGB))
    print("RGB Camera Default intrinsics...")
    print(M_rgb)
    print(width)
    print(height)

    M_left = np.array(calibData.getCameraIntrinsics(dai.CameraBoardSocket.LEFT, 1280, 720))
    print("LEFT Camera resized intrinsics...")
    print(M_left)

    R1 = np.array(calibData.getStereoLeftRectificationRotation())
    R2 = np.array(calibData.getStereoRightRectificationRotation())
    M_right = np.array(calibData.getCameraIntrinsics(calibData.getStereoRightCameraId(), 1280, 720))

    H_left = np.matmul(np.matmul(M_right, R1), np.linalg.inv(M_left))
    print("LEFT Camera stereo rectification matrix...")
    print(H_left)

    lr_extrinsics = np.array(calibData.getCameraExtrinsics(dai.CameraBoardSocket.LEFT, dai.CameraBoardSocket.RIGHT))
    print("Transformation matrix of where left Camera is W.R.T right Camera's optical center")
    print(lr_extrinsics)

Also available on GitHub

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <cstdio>
#include <iostream>
#include <string>

// Inludes common necessary includes for development using depthai library
#include "depthai-shared/common/CameraBoardSocket.hpp"
#include "depthai-shared/common/EepromData.hpp"
#include "depthai/depthai.hpp"

int main(int argc, char** argv) {
    using namespace std;

    // Connect Device
    dai::Device device;

    dai::CalibrationHandler calibData = device.readCalibration();
    // calibData.eepromToJsonFile(filename);
    std::vector<std::vector<float>> intrinsics;
    int width, height;

    cout << "Intrinsics from defaultIntrinsics function" << endl;
    std::tie(intrinsics, width, height) = calibData.getDefaultIntrinsics(dai::CameraBoardSocket::RIGHT);

    for(auto row : intrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Width -> " << width << endl;
    cout << "Height -> " << height << endl;

    cout << "Intrinsics from getCameraIntrinsics function full resolution ->" << endl;
    intrinsics = calibData.getCameraIntrinsics(dai::CameraBoardSocket::RIGHT);

    for(auto row : intrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Intrinsics from getCameraIntrinsics function 1280 x 720  ->" << endl;
    intrinsics = calibData.getCameraIntrinsics(dai::CameraBoardSocket::RIGHT, 1280, 720);

    for(auto row : intrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Intrinsics from getCameraIntrinsics function 720 x 450 ->" << endl;
    intrinsics = calibData.getCameraIntrinsics(dai::CameraBoardSocket::RIGHT, 720);

    for(auto row : intrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Intrinsics from getCameraIntrinsics function 600 x 1280 ->" << endl;
    intrinsics = calibData.getCameraIntrinsics(dai::CameraBoardSocket::RIGHT, 600, 1280);

    for(auto row : intrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    std::vector<std::vector<float>> extrinsics;

    cout << "Extrinsics from left->right test ->" << endl;
    extrinsics = calibData.getCameraExtrinsics(dai::CameraBoardSocket::LEFT, dai::CameraBoardSocket::RIGHT);

    for(auto row : extrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Extrinsics from right->left test ->" << endl;
    extrinsics = calibData.getCameraExtrinsics(dai::CameraBoardSocket::RIGHT, dai::CameraBoardSocket::LEFT);

    for(auto row : extrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Extrinsics from right->rgb test ->" << endl;
    extrinsics = calibData.getCameraExtrinsics(dai::CameraBoardSocket::RIGHT, dai::CameraBoardSocket::RGB);

    for(auto row : extrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Extrinsics from rgb->right test ->" << endl;
    extrinsics = calibData.getCameraExtrinsics(dai::CameraBoardSocket::RGB, dai::CameraBoardSocket::RIGHT);

    for(auto row : extrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    cout << "Extrinsics from left->rgb test ->" << endl;
    extrinsics = calibData.getCameraExtrinsics(dai::CameraBoardSocket::LEFT, dai::CameraBoardSocket::RGB);

    for(auto row : extrinsics) {
        for(auto val : row) cout << val << "  ";
        cout << endl;
    }

    return 0;
}

Got questions?

We’re always happy to help with code or other questions you might have.