diff options
Diffstat (limited to 'src/argaze/ArUcoMarker/ArUcoMarker.py')
-rw-r--r-- | src/argaze/ArUcoMarker/ArUcoMarker.py | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/src/argaze/ArUcoMarker/ArUcoMarker.py b/src/argaze/ArUcoMarker/ArUcoMarker.py index bfd6350..fddc2aa 100644 --- a/src/argaze/ArUcoMarker/ArUcoMarker.py +++ b/src/argaze/ArUcoMarker/ArUcoMarker.py @@ -28,80 +28,80 @@ import cv2.aruco as aruco @dataclass class ArUcoMarker(): - """Define ArUco marker class.""" + """Define ArUco marker class.""" - dictionary: ArUcoMarkerDictionary.ArUcoMarkerDictionary - """Dictionary to which it belongs.""" + dictionary: ArUcoMarkerDictionary.ArUcoMarkerDictionary + """Dictionary to which it belongs.""" - identifier: int - """Index into dictionary""" + identifier: int + """Index into dictionary""" - size: float = field(default=math.nan) - """Size of marker in centimeters.""" + size: float = field(default=math.nan) + """Size of marker in centimeters.""" - corners: numpy.array = field(init=False, repr=False) - """Estimated 2D corners position in camera image referential.""" + corners: numpy.array = field(init=False, repr=False) + """Estimated 2D corners position in camera image referential.""" - translation: numpy.array = field(init=False, repr=False) - """Estimated 3D center position in camera world referential.""" + translation: numpy.array = field(init=False, repr=False) + """Estimated 3D center position in camera world referential.""" - rotation: numpy.array = field(init=False, repr=False) - """Estimated 3D marker rotation in camera world referential.""" + rotation: numpy.array = field(init=False, repr=False) + """Estimated 3D marker rotation in camera world referential.""" - points: numpy.array = field(init=False, repr=False) - """Estimated 3D corners positions in camera world referential.""" + points: numpy.array = field(init=False, repr=False) + """Estimated 3D corners positions in camera world referential.""" - @property - def center(self) -> numpy.array: - """Get 2D center position in camera image referential.""" + @property + def center(self) -> numpy.array: + """Get 2D center position in camera image referential.""" - return self.corners[0].mean(axis=0) + return self.corners[0].mean(axis=0) - def image(self, dpi) -> numpy.array: - """Create marker matrix image at a given resolution. + def image(self, dpi) -> numpy.array: + """Create marker matrix image at a given resolution. - !!! warning - Marker size have to be setup before. - """ + !!! warning + Marker size have to be setup before. + """ - assert(not math.isnan(self.size)) + assert(not math.isnan(self.size)) - dimension = round(self.size * dpi / 2.54) # 1 cm = 2.54 inches - matrix = numpy.zeros((dimension, dimension, 1), dtype="uint8") + dimension = round(self.size * dpi / 2.54) # 1 cm = 2.54 inches + matrix = numpy.zeros((dimension, dimension, 1), dtype="uint8") - aruco.generateImageMarker(self.dictionary.markers, self.identifier, dimension, matrix, 1) + aruco.generateImageMarker(self.dictionary.markers, self.identifier, dimension, matrix, 1) - return numpy.repeat(matrix, 3).reshape(dimension, dimension, 3) + return numpy.repeat(matrix, 3).reshape(dimension, dimension, 3) - def draw(self, image: numpy.array, K: numpy.array, D: numpy.array, color: tuple = None, draw_axes: dict = None): - """Draw marker in image. + def draw(self, image: numpy.array, K: numpy.array, D: numpy.array, color: tuple = None, draw_axes: dict = None): + """Draw marker in image. - Parameters: - image: image where to - K: - D: - color: marker color (if None, no marker drawn) - draw_axes: enable marker axes drawing + Parameters: + image: image where to + K: + D: + color: marker color (if None, no marker drawn) + draw_axes: enable marker axes drawing - !!! warning - draw_axes needs marker size and pose estimation. - """ + !!! warning + draw_axes needs marker size and pose estimation. + """ - # Draw marker if required - if color is not None: + # Draw marker if required + if color is not None: - aruco.drawDetectedMarkers(image, [numpy.array([list(self.corners)])], numpy.array([self.identifier]), color) + aruco.drawDetectedMarkers(image, [numpy.array([list(self.corners)])], numpy.array([self.identifier]), color) - # Draw marker axes if pose has been estimated, marker have a size and if required - if self.translation.size == 3 and self.rotation.size == 9 and not math.isnan(self.size) and draw_axes is not None: + # Draw marker axes if pose has been estimated, marker have a size and if required + if self.translation.size == 3 and self.rotation.size == 9 and not math.isnan(self.size) and draw_axes is not None: - cv2.drawFrameAxes(image, numpy.array(K), numpy.array(D), self.rotation, self.translation, self.size, **draw_axes) + cv2.drawFrameAxes(image, numpy.array(K), numpy.array(D), self.rotation, self.translation, self.size, **draw_axes) - def save(self, destination_folder, dpi): - """Save marker image as .png file into a destination folder.""" + def save(self, destination_folder, dpi): + """Save marker image as .png file into a destination folder.""" - filename = f'{self.dictionary.name}_{self.dictionary.format}_{self.identifier}.png' - filepath = f'{destination_folder}/{filename}' + filename = f'{self.dictionary.name}_{self.dictionary.format}_{self.identifier}.png' + filepath = f'{destination_folder}/{filename}' - cv2.imwrite(filepath, self.image(dpi)) + cv2.imwrite(filepath, self.image(dpi)) |