[ar_tag_pose]
- refactored calibrateload/save into ar_tag_pose
This commit is contained in:
+22
-12
@@ -28,6 +28,20 @@ class ArTagPose(abc.ABC):
|
|||||||
def is_calibrated(self):
|
def is_calibrated(self):
|
||||||
return self.mtx is not None and self.dist is not None
|
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):
|
def calibrate_camera(self, cal_img_size: tuple[int,int], r_vecs=None, t_vecs=None, flags=0):
|
||||||
success = False
|
success = False
|
||||||
if len(self.obj_points_list) > 0 and len(self.img_points_list) > 0:
|
if len(self.obj_points_list) > 0 and len(self.img_points_list) > 0:
|
||||||
@@ -47,18 +61,6 @@ class ArTagPose(abc.ABC):
|
|||||||
|
|
||||||
return success
|
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):
|
def process(self):
|
||||||
|
|
||||||
# Open camera
|
# Open camera
|
||||||
@@ -110,6 +112,14 @@ class ArTagPose(abc.ABC):
|
|||||||
cv.imshow('img', img)
|
cv.imshow('img', img)
|
||||||
cv.destroyAllWindows()
|
cv.destroyAllWindows()
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def get_name(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def calibrate_points(self, image: cv.typing.MatLike):
|
||||||
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _process(self, image, draw_marker_box=True, draw_marker_id=False, draw_marker_axis=True) -> (bool, np.ndarray, np.ndarray):
|
def _process(self, image, draw_marker_box=True, draw_marker_id=False, draw_marker_axis=True) -> (bool, np.ndarray, np.ndarray):
|
||||||
pass
|
pass
|
||||||
@@ -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)
|
im_h = int(board_size[1] * (marker_length + marker_sep) - marker_sep + 2 * margins)
|
||||||
image = board.generateImage((im_w, im_h), None, margins, 1)
|
image = board.generateImage((im_w, im_h), None, margins, 1)
|
||||||
keyname = [key for key, val in ARUCO_DICT.items() if val == dict_type][0]
|
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):
|
class ArTagPoseArucoBoard(ArTagPose):
|
||||||
def __init__(self, dict_type: int, board_size: tuple[int, int]):
|
def __init__(self, dict_type: int, board_size: tuple[int, int]):
|
||||||
self.board_size = board_size
|
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, self.name = factory(dict_type, board_size)
|
||||||
self.detector, self.board = factory(dict_type, board_size)
|
|
||||||
|
|
||||||
def calibrate_save(self):
|
def get_name(self):
|
||||||
np.savez(f"{self.calib_filename}", mtx=self.mtx, dist=self.dist, rvecs=self.r_vecs, tvecs=self.t_vecs)
|
return self.name
|
||||||
|
|
||||||
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 calibrate_points(self, image: cv.typing.MatLike):
|
def calibrate_points(self, image: cv.typing.MatLike):
|
||||||
success = False
|
success = False
|
||||||
|
|||||||
@@ -14,28 +14,19 @@ def factory(dict_type: int, board_size: cv.typing.Size, square_length: float = 5
|
|||||||
detector = cv.aruco.CharucoDetector(board, params)
|
detector = cv.aruco.CharucoDetector(board, params)
|
||||||
image = board.generateImage((im_w, im_h), None, margin_length, 1)
|
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]
|
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):
|
class ArTagPoseCharucoBoard(ArTagPose):
|
||||||
def __init__(self, dict_type: int, board_size: tuple[int, int]):
|
def __init__(self, dict_type: int, board_size: tuple[int, int]):
|
||||||
ArTagPose.__init__(self)
|
ArTagPose.__init__(self)
|
||||||
self.board_size = board_size
|
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, self.name = factory(dict_type, board_size)
|
||||||
self.detector, self.board = factory(dict_type, board_size)
|
|
||||||
|
|
||||||
def calibrate_save(self):
|
def get_name(self):
|
||||||
np.savez(f"{self.calib_filename}", mtx=self.mtx, dist=self.dist, rvecs=self.r_vecs, tvecs=self.t_vecs)
|
return self.name
|
||||||
|
|
||||||
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 calibrate_points(self, image: cv.typing.MatLike):
|
def calibrate_points(self, image: cv.typing.MatLike):
|
||||||
success = False
|
success = False
|
||||||
|
|||||||
Reference in New Issue
Block a user