aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/argaze/ArUcoMarkers/ArUcoCamera.py83
1 files changed, 32 insertions, 51 deletions
diff --git a/src/argaze/ArUcoMarkers/ArUcoCamera.py b/src/argaze/ArUcoMarkers/ArUcoCamera.py
index 06438ac..dd254d2 100644
--- a/src/argaze/ArUcoMarkers/ArUcoCamera.py
+++ b/src/argaze/ArUcoMarkers/ArUcoCamera.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python
+from dataclasses import dataclass, field
+
import json
import numpy
import cv2.aruco as aruco
@@ -10,73 +12,52 @@ K0 = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 0.]])
D0 = numpy.array([0.0, 0.0, 0.0, 0.0, 0.0])
"""Define default camera distorsion coefficients vector."""
-class ArUcoCamera():
- """Handle optical calibration data with:
- - camera intrinsic parameters matrix as K (focal lengths and principal point),
- - camera distorsion coefficients vector as D."""
-
- def __init__(self):
- """Define optical parameters."""
+@dataclass(frozen=True)
+class CalibrationData():
+ """Define optical camera calibration data."""
- self.__rms = 0
+ rms: float = field(default=0)
+ """Root Mean Square error of calibration."""
- self.__dimensions = [0, 0]
+ dimensions: numpy.ndarray = field(default=numpy.array([0, 0]))
+ """Frame dimensions in pixels from which the calibration have been done."""
- self.__K = K0
- self.__D = D0
+ K: numpy.array = field(default=K0)
+ """Intrinsic parameters matrix (focal lengths and principal point)."""
- # define calibration data
- self.__corners_set_number = 0
- self.__corners_set = []
- self.__corners_set_ids = []
+ D: numpy.array = field(default=D0)
+ """Distorsion coefficients vector."""
- def __del__(self):
- pass
+ def __init__(self, **kwargs):
+
+ self.__dict__.update(kwargs)
- def load_calibration_file(self, camera_calibration_filepath):
+ @classmethod
+ def from_json(self, json_filepath):
"""Load optical parameters from .json file."""
- with open(camera_calibration_filepath) as calibration_file:
+ with open(json_filepath) as calibration_file:
- calibration_data = json.load(calibration_file)
+ return CalibrationData(**json.load(calibration_file))
- self.__rms = calibration_data['rms']
- self.__dimensions = calibration_data['dimensions']
- self.__K = numpy.array(calibration_data['camera matrix'])
- self.__D = numpy.array(calibration_data['distortion coefficients'])
-
- def save_calibration_file(self, camera_calibration_filepath):
+ def to_json(self, json_filepath):
"""Save optical parameters into .json file."""
- calibration_data = {'rms': self.__rms, 'dimensions': self.__dimensions, 'camera matrix': self.__K.tolist(), 'distortion coefficients': self.__D.tolist()}
-
- with open(camera_calibration_filepath, 'w', encoding='utf-8') as calibration_file:
+ with open(json_filepath, 'w', encoding='utf-8') as calibration_file:
- json.dump(calibration_data, calibration_file, ensure_ascii=False, indent=4)
+ json.dump(self, calibration_file, ensure_ascii=False, indent=4)
- @property
- def rms(self) -> float:
- """Get Root Mean Square (rms) error."""
+class ArUcoCamera(CalibrationData):
+ """Handle camera calibration process."""
- return self.__rms
-
- @property
- def dimensions(self) -> list:
- """Get camera frame dimensions in pixels from which the calibration have been done."""
+ def __init__(self, **kwargs):
+
+ super().__init__(**kwargs)
- return self.__dimensions
-
- @property
- def K(self) -> numpy.array:
- """Get camera intrinsic parameters matrix."""
-
- return self.__K
-
- @property
- def D(self) -> numpy.array:
- """Get camera distorsion coefficients vector."""
-
- return self.__D
+ # Calibration data
+ self.__corners_set_number = 0
+ self.__corners_set = []
+ self.__corners_set_ids = []
def calibrate(self, board, frame_width, frame_height):
"""Retrieve camera K and D from stored calibration data."""