[ar_tag_pose]

- refactored calibrateload/save into ar_tag_pose
This commit is contained in:
2025-11-28 16:26:58 +01:00
parent d78f98a54c
commit 05399fdce1
3 changed files with 34 additions and 42 deletions
+22 -12
View File
@@ -28,6 +28,20 @@ class ArTagPose(abc.ABC):
def is_calibrated(self):
return self.mtx is not None and self.dist is not None
def calibrate_save(self):
filename = f"results/calib_{self.get_name()}.npz"
np.savez(f"{filename}", mtx=self.mtx, dist=self.dist, rvecs=self.r_vecs, tvecs=self.t_vecs)
def calibrate_load(self):
filename = f"results/calib_{self.get_name()}.npz"
try:
with np.load(filename) as X:
self.mtx, self.dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
except FileNotFoundError:
pass
except KeyError:
pass
def calibrate_camera(self, cal_img_size: tuple[int,int], r_vecs=None, t_vecs=None, flags=0):
success = False
if len(self.obj_points_list) > 0 and len(self.img_points_list) > 0:
@@ -47,18 +61,6 @@ class ArTagPose(abc.ABC):
return success
@abc.abstractmethod
def calibrate_points(self, image: cv.typing.MatLike):
pass
@abc.abstractmethod
def calibrate_save(self):
pass
@abc.abstractmethod
def calibrate_load(self):
pass
def process(self):
# Open camera
@@ -110,6 +112,14 @@ class ArTagPose(abc.ABC):
cv.imshow('img', img)
cv.destroyAllWindows()
@abc.abstractmethod
def get_name(self):
pass
@abc.abstractmethod
def calibrate_points(self, image: cv.typing.MatLike):
pass
@abc.abstractmethod
def _process(self, image, draw_marker_box=True, draw_marker_id=False, draw_marker_axis=True) -> (bool, np.ndarray, np.ndarray):
pass
+6 -15
View File
@@ -14,27 +14,18 @@ def factory(dict_type: int, board_size: cv.typing.Size, marker_length: float=100
im_h = int(board_size[1] * (marker_length + marker_sep) - marker_sep + 2 * margins)
image = board.generateImage((im_w, im_h), None, margins, 1)
keyname = [key for key, val in ARUCO_DICT.items() if val == dict_type][0]
cv.imwrite(f"results/aruco_board_w{board_size[0]}_h{board_size[1]}_{keyname}.png", image)
name = f"aruco_board_w{board_size[0]}_h{board_size[1]}_{keyname}"
cv.imwrite(f"results/{name}.png", image)
return detector, board
return detector, board, name
class ArTagPoseArucoBoard(ArTagPose):
def __init__(self, dict_type: int, board_size: tuple[int, int]):
self.board_size = board_size
self.calib_filename = f"results/cam_calib_aruco_board_w{board_size[0]}_h{board_size[1]}.npz"
self.detector, self.board = factory(dict_type, board_size)
self.detector, self.board, self.name = factory(dict_type, board_size)
def calibrate_save(self):
np.savez(f"{self.calib_filename}", mtx=self.mtx, dist=self.dist, rvecs=self.r_vecs, tvecs=self.t_vecs)
def calibrate_load(self):
try:
with np.load(self.calib_filename) as X:
self.mtx, self.dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
except FileNotFoundError:
pass
except KeyError:
pass
def get_name(self):
return self.name
def calibrate_points(self, image: cv.typing.MatLike):
success = False
+6 -15
View File
@@ -14,28 +14,19 @@ def factory(dict_type: int, board_size: cv.typing.Size, square_length: float = 5
detector = cv.aruco.CharucoDetector(board, params)
image = board.generateImage((im_w, im_h), None, margin_length, 1)
keyname = [key for key, val in ARUCO_DICT.items() if val == dict_type][0]
cv.imwrite(f"results/charuco_board_w{board_size[0]}_h{board_size[1]}_{keyname}.png", image)
name = f"aruco_board_w{board_size[0]}_h{board_size[1]}_{keyname}"
cv.imwrite(f"results/{name}.png", image)
return detector, board
return detector, board, name
class ArTagPoseCharucoBoard(ArTagPose):
def __init__(self, dict_type: int, board_size: tuple[int, int]):
ArTagPose.__init__(self)
self.board_size = board_size
self.calib_filename = f"results/cam_calib_charuco_board_w{board_size[0]}_h{board_size[1]}.npz"
self.detector, self.board = factory(dict_type, board_size)
self.detector, self.board, self.name = factory(dict_type, board_size)
def calibrate_save(self):
np.savez(f"{self.calib_filename}", mtx=self.mtx, dist=self.dist, rvecs=self.r_vecs, tvecs=self.t_vecs)
def calibrate_load(self):
try:
with np.load(self.calib_filename) as X:
self.mtx, self.dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
except FileNotFoundError:
pass
except KeyError:
pass
def get_name(self):
return self.name
def calibrate_points(self, image: cv.typing.MatLike):
success = False