This commit is contained in:
2024-11-21 18:51:53 +08:00
parent 3ca9712cdd
commit 4a8bd957d2

74
test.py
View File

@ -6,6 +6,8 @@ from pathlib import Path
from loguru import logger from loguru import logger
from itertools import chain from itertools import chain
from matplotlib.pyplot import stem
class ArucoDictionary(Enum): class ArucoDictionary(Enum):
Dict_4X4_50 = aruco.DICT_4X4_50 Dict_4X4_50 = aruco.DICT_4X4_50
@ -31,21 +33,7 @@ class ArucoDictionary(Enum):
Dict_ArUco_ORIGINAL = aruco.DICT_ARUCO_ORIGINAL Dict_ArUco_ORIGINAL = aruco.DICT_ARUCO_ORIGINAL
def create_charuco_board( IMAGE_FOLDER = Path("xx")
square_x: int,
square_y: int,
square_length: float,
marker_length: float,
dictionary: aruco.Dictionary,
) -> aruco.CharucoBoard:
return aruco.CharucoBoard( # type: ignore
(square_x, square_y), square_length, marker_length, dictionary
)
# IMAGE_PATH = Path("/Users/crosstyan/Downloads/IMG_1505.jpeg")
# IMAGE_PATH = Path("ss.png")
IMAGE_FOLDER = Path("at")
OUTPUT_FOLDER = Path("output") OUTPUT_FOLDER = Path("output")
DICTIONARY = ArucoDictionary.Dict_4X4_50 DICTIONARY = ArucoDictionary.Dict_4X4_50
@ -55,46 +43,40 @@ def main():
for img_path in images: for img_path in images:
img = cv2.imread(str(img_path)) img = cv2.imread(str(img_path))
# 10x7 # 10x7
# minus 1 # minus 1 when dealing with normal chessboard
border_num_x = 9 border_num_x = 10
border_num_y = 6 border_num_y = 7
# 115mm square # 115mm square
# 90mm marker # 90mm marker
# https://docs.opencv.org/4.x/d9/d6a/group__aruco.html#ga3bc50d61fe4db7bce8d26d56b5a6428a
# https://docs.opencv.org/4.x/df/d4a/tutorial_charuco_detection.html
# https://docs.opencv.org/3.4/df/d4a/tutorial_charuco_detection.html # https://docs.opencv.org/3.4/df/d4a/tutorial_charuco_detection.html
# https://docs.opencv.org/4.x/df/d4a/tutorial_charuco_detection.html
# https://docs.opencv.org/4.x/da/d13/tutorial_aruco_calibration.html
# https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html # https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html
# https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a # https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a
# https://docs.opencv.org/4.x/da/d13/tutorial_aruco_calibration.html
# https://github.com/opencv/opencv/issues/22083 # https://github.com/opencv/opencv/issues/22083
ret, chess_corners = cv2.findChessboardCorners( # OpenCV 4.10.x
img, dictionary = aruco.getPredefinedDictionary(DICTIONARY.value)
(border_num_x, border_num_y), board = aruco.CharucoBoard(
flags=cv2.CALIB_CB_ADAPTIVE_THRESH (border_num_x, border_num_y), 0.115, 0.09, dictionary
| cv2.CALIB_CB_NORMALIZE_IMAGE
| cv2.CALIB_CB_FAST_CHECK,
) )
if ret: detector = aruco.CharucoDetector(board)
cv2.drawChessboardCorners(
img, (border_num_x, border_num_y), chess_corners, ret ch_corners, ch_ids, markers_corners, marker_ids = detector.detectBoard(img)
# https://docs.opencv.org/4.10.0/d9/df5/classcv_1_1aruco_1_1CharucoDetector.html
if ch_corners is not None:
# https://docs.opencv.org/4.x/d4/db2/classcv_1_1aruco_1_1Board.html
aruco.drawDetectedCornersCharuco(
img, ch_corners, ch_ids, (0, 255, 0)
) )
cv2.imwrite(str(OUTPUT_FOLDER / f"{img_path.stem}_chess.jpg"), img) else:
logger.info("Chessboard found for {}", img_path) logger.warning(f"Failed to detect Charuco board in {img_path}")
else: continue
logger.warning("Chessboard not found for {}", img_path) if markers_corners is not None:
aruco.drawDetectedMarkers(img, markers_corners, marker_ids)
# predefined_dict = aruco.getPredefinedDictionary(DICTIONARY.value) output_path = OUTPUT_FOLDER / (f"{img_path.stem}_output.jpg")
# corners, ids, rejected = aruco.detectMarkers(img, predefined_dict) logger.info(f"Saving to {output_path}")
# board = create_charuco_board(10, 7, 115, 90, predefined_dict) cv2.imwrite(str(output_path), img)
# detector = aruco.CharucoDetector(board)
# if ids is not None:
# aruco.drawDetectedMarkers(img, corners, ids)
# ret, ch_corners, ch_ids = aruco.interpolateCornersCharuco(corners, ids, img, board)
# if ch_corners is not None and ch_ids is not None:
# aruco.drawDetectedCornersCharuco(img, ch_corners, ch_ids)
# cv2.imwrite("output.jpg", img)
if __name__ == "__main__": if __name__ == "__main__":
main() main()