aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/ArUcoMarkers/ArUcoBoard.py
blob: 768549456a7c2cd51eb146b6d64bdfb98256a591 (plain)
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
#!/usr/bin/env python
from dataclasses import dataclass, field

from argaze.ArUcoMarkers import ArUcoMarkersDictionary

import numpy
import cv2 as cv
import cv2.aruco as aruco

@dataclass
class ArUcoBoard():
    """Calibration chess board with ArUco markers inside."""

    columns: int = field(default=0)
    """Number of columns."""

    rows: int = field(default=0)
    """Number of rows."""

    square_size: float = field(default=0.)
    """Size of board square in centimeter."""

    marker_size: float = field(default=0.)
    """Size of ArUco markers inside board squares in centimeter."""

    dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary = field(default_factory=ArUcoMarkersDictionary.ArUcoMarkersDictionary)
    """ArUco markers dictionary."""

    def __post_init__(self):

        # Create board model
        self.model = aruco.CharucoBoard((self.columns, self.rows), self.square_size/100., self.marker_size/100., self.dictionary.markers)

    @property
    def identifiers(self) -> list[int]:
        """Get board markers identifiers."""

        return self.model.getIds()

    @property
    def size(self)-> int:
        """Get numbers of columns and rows."""

        return self.model.getChessboardSize()

    @property
    def markers_number(self) -> int:
        """Get number of markers."""

        return len(self.model.getIds())

    @property
    def corners_number(self) -> int:
        """Get number of corners."""

        return (self.model.getChessboardSize()[0] - 1 ) * (self.model.getChessboardSize()[1] - 1)

    def save(self, filepath: str, dpi: int):
        """Save calibration board picture at a given resolution."""

        dimension = [round(d * self.model.getSquareLength() * 100 * dpi / 2.54) for d in self.model.getChessboardSize()]  # 1 cm = 2.54 inches

        cv.imwrite(filepath, self.model.generateImage(dimension))