From 5abb78234776a370a26fd1880100462262197af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=B6ldi=20Tam=C3=A1s?= Date: Fri, 17 May 2024 10:57:57 +0200 Subject: [PATCH] fix: cv2.aruco.interpolateCornersCharuco is deprecated (#979) There has been API Changes in the newer releases of opencv2 (from 4.8.0). The PR addresses this by supporting both the old and new APIs. updated Syntax ``` charucodetector = cv2.aruco.CharucoDetector(board) charuco_corners, charuco_ids, marker_corners, marker_ids = charucodetector.detectBoard(image) ``` before 4.8.0 ``` marker_corners, marker_ids, rejectedImgPoints = cv2.aruco.detectMarkers( image, dictionary) retval, charuco_corners, charuco_ids = cv2.aruco.interpolateCornersCharuco( marker_corners, marker_ids, image, board) ``` See the changed examples in the main opencv2 repo: https://github.com/opencv/opencv/blob/f9a59f2592993d3dcc080e495f4f5e02dd8ec7ef/samples/python/calibrate.py#L110 (cherry picked from commit a8cd84027d23d3bd21c506f2538b7a4d29e4ba6f) --- .../src/camera_calibration/calibrator.py | 24 ++++++++++++++----- .../camera_calibration/camera_calibrator.py | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/camera_calibration/src/camera_calibration/calibrator.py b/camera_calibration/src/camera_calibration/calibrator.py index cc2511f54..0793b4355 100644 --- a/camera_calibration/src/camera_calibration/calibrator.py +++ b/camera_calibration/src/camera_calibration/calibrator.py @@ -88,8 +88,13 @@ def __init__(self, pattern="chessboard", n_cols = 0, n_rows = 0, dim = 0.0, mark "7x7_100" : cv2.aruco.DICT_7X7_100, "7x7_250" : cv2.aruco.DICT_7X7_250, "7x7_1000" : cv2.aruco.DICT_7X7_1000}[aruco_dict]) - self.charuco_board = cv2.aruco.CharucoBoard_create(self.n_cols, self.n_rows, self.dim, self.marker_size, - self.aruco_dict) + if cv2.__version__ >= '4.8.0': + self.charuco_board = cv2.aruco.CharucoBoard((self.n_cols, self.n_rows), self.dim, self.marker_size, + self.aruco_dict) + else: + self.charuco_board = cv2.aruco.CharucoBoard_create(self.n_cols, self.n_rows, self.dim, self.marker_size, + self.aruco_dict) + # Make all private!!!!! def lmin(seq1, seq2): @@ -268,10 +273,17 @@ def _get_charuco_corners(img, board, refine): else: mono = img - marker_corners, marker_ids, _ = cv2.aruco.detectMarkers(img, board.aruco_dict) - if len(marker_corners) == 0: - return (False, None, None) - _, square_corners, ids = cv2.aruco.interpolateCornersCharuco(marker_corners, marker_ids, img, board.charuco_board) + + if cv2.__version__ >= '4.8.0': + charucodetector = cv2.aruco.CharucoDetector(board.charuco_board) + square_corners, ids, marker_corners, marker_ids = charucodetector.detectBoard(mono) + else: + marker_corners, marker_ids, _ = cv2.aruco.detectMarkers(img, board.aruco_dict) + + if len(marker_corners) == 0: + return (False, None, None) + _, square_corners, ids = cv2.aruco.interpolateCornersCharuco(marker_corners, marker_ids, img, board.charuco_board, minMarkers=1) + return ((square_corners is not None) and (len(square_corners) > 5), square_corners, ids) def _get_circles(img, board, pattern): diff --git a/camera_calibration/src/camera_calibration/camera_calibrator.py b/camera_calibration/src/camera_calibration/camera_calibrator.py index 4fecabcc0..c6b6abeba 100755 --- a/camera_calibration/src/camera_calibration/camera_calibrator.py +++ b/camera_calibration/src/camera_calibration/camera_calibrator.py @@ -311,7 +311,7 @@ def on_model_change(self, model_select_val): self.c.set_cammodel( CAMERA_MODEL.PINHOLE if model_select_val < 0.5 else CAMERA_MODEL.FISHEYE) def on_scale(self, scalevalue): - if self.c.calibrated: + if self.c and self.c.calibrated: self.c.set_alpha(scalevalue / 100.0) def button(self, dst, label, enable):