aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/ArUcoMarkers/ArUcoBoard.py
blob: be475d580f845fd9f810570059e591677c179a2a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Calibration chess board with ArUco markers inside.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

from dataclasses import dataclass, field
from typing import Sequence

import cv2 as cv
import cv2.aruco as aruco

from argaze.ArUcoMarkers import ArUcoMarkersDictionary


@dataclass
class ArUcoBoard():
    """ """

    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) -> Sequence[int]:
        """Get board markers identifiers."""

        return self.model.getIds()

    @property
    def size(self) -> Sequence[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))