diff options
-rw-r--r-- | src/argaze.test/ArUcoMarkers/ArUcoBoard.py | 50 | ||||
-rw-r--r-- | src/argaze/ArUcoMarkers/ArUcoBoard.py | 17 |
2 files changed, 61 insertions, 6 deletions
diff --git a/src/argaze.test/ArUcoMarkers/ArUcoBoard.py b/src/argaze.test/ArUcoMarkers/ArUcoBoard.py new file mode 100644 index 0000000..5ca1324 --- /dev/null +++ b/src/argaze.test/ArUcoMarkers/ArUcoBoard.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import unittest +import os + +from argaze.ArUcoMarkers import ArUcoBoard, ArUcoMarkersDictionary + +import numpy + +class TestArUcoBoardClass(unittest.TestCase): + """Test ArUcoBoard class.""" + + def test_new_from_dictionary_string(self): + """Test ArUcoBoard creation using a dictionary string.""" + + # Check ArUco board creation + columns = 4 + rows = 3 + square_size = 2 + marker_size = 1 + + aruco_board = ArUcoBoard.ArUcoBoard('DICT_ARUCO_ORIGINAL', columns, rows, square_size, marker_size) + + # Check ArUco board + self.assertIsNone(numpy.testing.assert_array_equal(aruco_board.identifiers, [i for i in range(int((columns*rows)/2))])) + self.assertIsNone(numpy.testing.assert_array_equal(aruco_board.size, [columns, rows])) + self.assertEqual(aruco_board.markers_number, int((columns*rows)/2)) + self.assertEqual(aruco_board.corners_number, (columns-1)*(rows-1)) + + def test_new_from_dictionary_instance(self): + """Test ArUcoBoard creation using a dictionary instance.""" + + # Check ArUco board creation + columns = 4 + rows = 3 + square_size = 2 + marker_size = 1 + + aruco_dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary('DICT_ARUCO_ORIGINAL') + aruco_board = ArUcoBoard.ArUcoBoard(aruco_dictionary, columns, rows, square_size, marker_size) + + # Check ArUco board + self.assertIsNone(numpy.testing.assert_array_equal(aruco_board.identifiers, [i for i in range(int((columns*rows)/2))])) + self.assertIsNone(numpy.testing.assert_array_equal(aruco_board.size, [columns, rows])) + self.assertEqual(aruco_board.markers_number, int((columns*rows)/2)) + self.assertEqual(aruco_board.corners_number, (columns-1)*(rows-1)) + +if __name__ == '__main__': + + unittest.main()
\ No newline at end of file diff --git a/src/argaze/ArUcoMarkers/ArUcoBoard.py b/src/argaze/ArUcoMarkers/ArUcoBoard.py index 2fa3da7..e6772c4 100644 --- a/src/argaze/ArUcoMarkers/ArUcoBoard.py +++ b/src/argaze/ArUcoMarkers/ArUcoBoard.py @@ -9,11 +9,16 @@ import cv2.aruco as aruco class ArUcoBoard(): """Calibration chess board with ArUco markers inside.""" - def __init__(self, aruco_dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary, columns: int, rows: int, square_size: float, marker_size: float): + def __init__(self, dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary | str, columns: int, rows: int, square_size: float, marker_size: float): """Create columns x rows chess board with ArUco markers at given size in centimeters.""" - # Store ArUco markers dictionary - self.__aruco_dict = aruco_dictionary + # Handle dictionary str or instance + if type(dictionary) == str: + self.__dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary(dictionary) + elif isinstance(dictionary, ArUcoMarkersDictionary.ArUcoMarkersDictionary): + self.__dictionary = dictionary + else: + raise ValueError(f'dictionary: {dictionary}') # Store properties self.__columns = columns @@ -22,7 +27,7 @@ class ArUcoBoard(): self.__marker_size = marker_size # in cm # Create board model - self.__board = aruco.CharucoBoard((self.__columns, self.__rows), self.__square_size/100., self.__marker_size/100., self.__aruco_dict.markers) + self.__board = aruco.CharucoBoard((self.__columns, self.__rows), self.__square_size/100., self.__marker_size/100., self.__dictionary.markers) def __del__(self): pass @@ -58,9 +63,9 @@ class ArUcoBoard(): return (self.__board.getChessboardSize()[0] - 1 ) * (self.__board.getChessboardSize()[1] - 1) def save(self, destination_folder: str, dpi: int): - """Save a picture of the calibration board.""" + """Save a picture of the calibration board into a destination folder.""" - output_filename = f'{self.__aruco_dict.name}_{self.__marker_size}cm_{self.__columns*self.__square_size}cmx{self.__rows*self.__square_size}cm.png' + output_filename = f'{self.__dictionary.name}_{self.__marker_size}cm_{self.__columns*self.__square_size}cmx{self.__rows*self.__square_size}cm.png' dimension = [round(d * self.__board.getSquareLength() * 100 * dpi / 2.54) for d in self.__board.getChessboardSize()] # 1 cm = 2.54 inches |