Skip to content

Calibration

Calibration is the process of determining the parameters of a model. In the case of the pipeline, the model is the set of parameters that define the transformation of the data. The calibration process is the process of determining the values of these parameters.

This is done by using the provided calibration images.

get_calib_params(calib_images, width=9, height=6)

Calculate the camera calibration parameters based on the calibration images.

Parameters:

Name Type Description Default
calib_images list[str]

The list of paths to the calibration images.

required
width int, optional

The number of inner corners in the calibration images in the x direction. By default 9.

9
height int, optional

The number of inner corners in the calibration images in the y direction. By default 6.

6

Returns:

Type Description
tuple[cv.Mat, cv.Mat]

The camera matrix and the distortion coefficients.

Source code in src/pipeline/calibration.py
 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
def get_calib_params(calib_images: list[str], width: int = 9, height: int = 6) -> tuple[cv.Mat, cv.Mat]:
    """Calculate the camera calibration parameters based on the calibration images.

    Parameters
    ----------
    calib_images : list[str]
        The list of paths to the calibration images.
    width : int, optional
        The number of inner corners in the calibration images in the x direction. _By default `9`._
    height : int, optional
        The number of inner corners in the calibration images in the y direction. _By default `6`._

    Returns
    -------
    tuple[cv.Mat, cv.Mat]
        The camera matrix and the distortion coefficients.
    """
    logger.info("Calibrating camera...")

    img_gray = None

    objp = np.zeros((width * height, 3), np.float32)
    objp[:, :2] = np.mgrid[:width, :height].T.reshape(-1, 2)
    objpoints = []
    imgpoints = []

    for filename in calib_images:
        img_gray = cv.cvtColor(cv.imread(filename), cv.COLOR_BGR2GRAY)
        ret, corners = cv.findChessboardCorners(img_gray, (width, height), None)

        if ret:
            objpoints.append(objp)
            imgpoints.append(corners)

    if img_gray is None:
        logger.error("Something went wrong. Exiting...")
        sys.exit(1)

    _, mtx, dist, _, _ = cv.calibrateCamera(objpoints, imgpoints, img_gray.shape[::-1], None, None)

    logger.success("Camera calibrated!")
    return mtx, dist