diff --git a/.gitignore b/.gitignore index c99f4a4..8198a97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,167 @@ *.jpg -*.jpeg \ No newline at end of file +*.jpeg +*.pdf +*.png + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/test.py b/test.py index 8bfad72..de52292 100644 --- a/test.py +++ b/test.py @@ -3,6 +3,8 @@ from cv2 import aruco from cv2.typing import MatLike from enum import Enum from pathlib import Path +from loguru import logger +from itertools import chain class ArucoDictionary(Enum): @@ -36,46 +38,63 @@ def create_charuco_board( marker_length: float, dictionary: aruco.Dictionary, ) -> aruco.CharucoBoard: - return aruco.CharucoBoard( # type: ignore + 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("/Users/crosstyan/Downloads/IMG_1505.jpeg") +# IMAGE_PATH = Path("ss.png") +IMAGE_FOLDER = Path("at") +OUTPUT_FOLDER = Path("output") +DICTIONARY = ArucoDictionary.Dict_4X4_50 def main(): - img: MatLike - img = cv2.imread(str(IMAGE_PATH)) - # 10x7 - border_num_x = 10 - border_num_y = 7 - # 115mm square - # 90mm marker - dictionary = ArucoDictionary.Dict_4X4_50 - predifined = aruco.getPredefinedDictionary(dictionary.value) - # 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/4.x/dc/dbb/tutorial_py_calibration.html + OUTPUT_FOLDER.mkdir(exist_ok=True) + images = chain(IMAGE_FOLDER.glob("*.jpeg"), IMAGE_FOLDER.glob("*.png"), IMAGE_FOLDER.glob("*.jpg")) + for img_path in images: + img = cv2.imread(str(img_path)) + # 10x7 + # minus 1 + border_num_x = 9 + border_num_y = 6 + # 115mm square + # 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/4.x/dc/dbb/tutorial_py_calibration.html - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - ret, chess_corners = cv2.findChessboardCorners(gray, (border_num_x, border_num_y), None) - if ret: - cv2.drawChessboardCorners(img, (border_num_x, border_num_y), chess_corners, ret) - cv2.imwrite("output.jpg", img) - else: - print("not found") + # 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 + ret, chess_corners = cv2.findChessboardCorners( + img, + (border_num_x, border_num_y), + flags=cv2.CALIB_CB_ADAPTIVE_THRESH + | cv2.CALIB_CB_NORMALIZE_IMAGE + | cv2.CALIB_CB_FAST_CHECK, + ) + if ret: + cv2.drawChessboardCorners( + img, (border_num_x, border_num_y), chess_corners, ret + ) + cv2.imwrite(str(OUTPUT_FOLDER / f"{img_path.stem}_chess.jpg"), img) + logger.info("Chessboard found for {}", img_path) + else: + logger.warning("Chessboard not found for {}", img_path) + + # predefined_dict = aruco.getPredefinedDictionary(DICTIONARY.value) + # corners, ids, rejected = aruco.detectMarkers(img, predefined_dict) + # board = create_charuco_board(10, 7, 115, 90, predefined_dict) + # 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) - # corners, ids, rejected = aruco.detectMarkers(img, predifined) - # board = create_charuco_board(10, 7, 115, 90, predifined) - # detector = aruco.CharucoDetector(board) - # if ids is not None: - # aruco.drawDetectedMarkers(img, corners, ids) - # _, 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__": main() -