Last active
October 7, 2025 09:16
-
-
Save dkurt/b793b70548ea9fa64da5b7d30a9b70d7 to your computer and use it in GitHub Desktop.
OpenCV: ChArUco camera calibration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://docs.opencv.org/4.x/da/d13/tutorial_aruco_calibration.html | |
| # data: https://drive.google.com/file/d/12IWsHiERr0xGrcAPe9VCkWNMrqEmI3fF/view?usp=drive_link | |
| import cv2 as cv | |
| import os | |
| aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_100) | |
| board = cv.aruco.CharucoBoard((5, 7), | |
| squareLength=110, | |
| markerLength=72, | |
| dictionary=aruco_dict) | |
| detector = cv.aruco.CharucoDetector(board) | |
| obj_points = [] | |
| img_points = [] | |
| for path in os.listdir("images"): | |
| img = cv.imread(f"images\\{path}") | |
| corners, charucoIds, _, _ = detector.detectBoard(img) | |
| #cv.aruco.drawDetectedCornersCharuco(img, corners, charucoIds=charucoIds) | |
| #cv.imwrite(f"debug\\{path}.jpg", img) | |
| if corners is not None and len(corners) > 0: | |
| frame_obj_points, frame_img_points = board.matchImagePoints(corners, charucoIds) | |
| if len(frame_obj_points) > 4: | |
| obj_points.append(frame_obj_points) | |
| img_points.append(frame_img_points) | |
| print("Start calibration") | |
| rms, camera_matrix, dist_coefs, _, _ = cv.calibrateCamera(obj_points, | |
| img_points, | |
| (960, 1280), | |
| None, | |
| None) | |
| print(rms) | |
| print(camera_matrix) | |
| print(dist_coefs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://github.com/opencv/opencv/blob/4.x/samples/python/aruco_detect_board_charuco.py | |
| import numpy as np | |
| import cv2 as cv | |
| aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_100) | |
| board = cv.aruco.CharucoBoard((5, 7), squareLength=0.035, markerLength=0.023, dictionary=aruco_dict) | |
| detector = cv.aruco.CharucoDetector(board) | |
| img = cv.imread("images\\2.jpg") | |
| corners, charucoIds, markerCorners, markerIds = detector.detectBoard(img) | |
| cv.aruco.drawDetectedMarkers(img, markerCorners) | |
| cv.aruco.drawDetectedCornersCharuco(img, corners) | |
| frame_obj_points, frame_img_points = board.matchImagePoints(corners, charucoIds) | |
| camera_matrix = np.array([[984.68630771, 0., 479.32526648], | |
| [ 0., 985.07648021, 642.04072255], | |
| [ 0., 0., 1. ]]) | |
| dist_coeffs = np.array([[ 1.40327092e-01,-7.56241483e-01 ,1.12768928e-03,-4.88750897e-04, 1.31331599e+00]]) | |
| _, rvec, tvec = cv.solvePnP(frame_obj_points, frame_img_points, camera_matrix, dist_coeffs) | |
| cv.drawFrameAxes(img, camera_matrix, dist_coeffs, rvec, tvec, length=0.035 * 3, thickness=3) | |
| cv.imwrite("debug\\axes.jpg", img) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://docs.opencv.org/4.x/df/d4a/tutorial_charuco_detection.html | |
| import cv2 as cv | |
| cols = 5 | |
| rows = 7 | |
| squareLength = 100 | |
| markerLength = 65 | |
| margin = squareLength - markerLength | |
| aruco_dict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_100) | |
| board = cv.aruco.CharucoBoard((cols, rows), squareLength=squareLength, markerLength=markerLength, dictionary=aruco_dict) | |
| img = board.generateImage((cols*squareLength + 2 * margin, rows*squareLength + 2 * margin), margin) | |
| cv.imwrite("img.png", img) |
Author
dkurt
commented
Oct 7, 2025

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment