aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/ArUcoMarkers/ArUcoBoard.py
blob: ce4abd8eee651311ca4a02974fd37c0fbd8b6962 (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
#!/usr/bin/env python

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

# Built-in ArUco dictionaries from OpenCV library
ARUCO_DICT = {
    'DICT_4X4_50': aruco.DICT_4X4_50,
    'DICT_4X4_100': aruco.DICT_4X4_100,
    'DICT_4X4_250': aruco.DICT_4X4_250,
    'DICT_4X4_1000': aruco.DICT_4X4_1000,
    'DICT_5X5_50': aruco.DICT_5X5_50,
    'DICT_5X5_100': aruco.DICT_5X5_100,
    'DICT_5X5_250': aruco.DICT_5X5_250,
    'DICT_5X5_1000': aruco.DICT_5X5_1000,
    'DICT_6X6_50': aruco.DICT_6X6_50,
    'DICT_6X6_100': aruco.DICT_6X6_100,
    'DICT_6X6_250': aruco.DICT_6X6_250,
    'DICT_6X6_1000': aruco.DICT_6X6_1000,
    'DICT_7X7_50': aruco.DICT_7X7_50,
    'DICT_7X7_100': aruco.DICT_7X7_100,
    'DICT_7X7_250': aruco.DICT_7X7_250,
    'DICT_7X7_1000': aruco.DICT_7X7_1000,
    'DICT_ARUCO_ORIGINAL': aruco.DICT_ARUCO_ORIGINAL
}

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

    def __init__(self, aruco_dictionary_name: str, columns: int, rows: int, square_size: float, marker_size: float):
        """Create columnsxrows chess board with ArUco markers type at given sizes in centimeters."""

        # check aruco dictionary name
        if ARUCO_DICT.get(aruco_dictionary_name, None) is None:
            raise NameError(f'Bad ArUco dictionnary name: {aruco_dictionary_name}')

        dict_name_split = aruco_dictionary_name.split('_')

        self.__aruco_dict_format = dict_name_split[1]
        self.__aruco_dict_number = int(dict_name_split[2])

        # load ArUco dictionary
        self.__aruco_dict = aruco.Dictionary_get(ARUCO_DICT[aruco_dictionary_name])

        # store property
        self.__columns = columns
        self.__rows = rows
        self.__square_size = square_size  # in cm
        self.__marker_size = marker_size  # in cm

        # create board model
        self.__board = aruco.CharucoBoard_create(self.__columns, self.__rows, self.__square_size/100., self.__marker_size/100., self.__aruco_dict)

    def __del__(self):
        pass

    def get_model(self):
        """Get the board model."""
        return self.__board

    def get_ids(self):
        """Get board markers ids."""
        return self.__board.ids

    def get_size(self):
        """Get numbers of columns and rows."""

        return self.__board.getChessboardSize()

    def export(self, destination_folder: str, dpi: int):
        """Save a picture of the calibration board."""

        output_filename = f'board_{self.__columns*self.__square_size}cmx{self.__rows*self.__square_size}cm_markers_{self.__aruco_dict_format}_{self.__marker_size}cm.png'

        dimension = [int(e * self.__board.getSquareLength() * 254 * dpi) for e in self.__board.getChessboardSize()]  # 1 meter = 254 inches

        cv.imwrite(f'{destination_folder}/{output_filename}', self.__board.draw(dimension))