-
Notifications
You must be signed in to change notification settings - Fork 0
/
aruco_calibrator.py
64 lines (51 loc) · 2.23 KB
/
aruco_calibrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import numpy as np
import cv2
# ------------------------------
# ENTER YOUR REQUIREMENTS HERE:
ARUCO_DICT = cv2.aruco.DICT_4X4_250
SQUARES_VERTICALLY = 7
SQUARES_HORIZONTALLY = 5
SQUARE_LENGTH = 0.03
MARKER_LENGTH = 0.015
# ...
PATH_TO_YOUR_IMAGES = 'calib'
# ------------------------------
def calibrate_and_save_parameters():
# Define the aruco dictionary and charuco board
dictionary = cv2.aruco.getPredefinedDictionary(ARUCO_DICT)
board = cv2.aruco.CharucoBoard((SQUARES_VERTICALLY, SQUARES_HORIZONTALLY),
SQUARE_LENGTH, MARKER_LENGTH, dictionary)
params = cv2.aruco.DetectorParameters()
# Load PNG images from folder
image_files = [os.path.join(PATH_TO_YOUR_IMAGES, f) for f in
os.listdir(PATH_TO_YOUR_IMAGES) if f.endswith(".jpg")]
image_files.sort() # Ensure files are in order
all_charuco_corners = []
all_charuco_ids = []
for image_file in image_files:
image = cv2.imread(image_file)
image_copy = image.copy()
marker_corners, marker_ids, _ = cv2.aruco.detectMarkers(image,
dictionary,
parameters=params)
# If at least one marker is detected
if marker_ids is not None and len(marker_ids) > 0:
cv2.aruco.drawDetectedMarkers(image_copy, marker_corners,
marker_ids)
charuco_retval, charuco_corners, charuco_ids = \
cv2.aruco.interpolateCornersCharuco(
marker_corners, marker_ids, image, board)
if charuco_retval:
all_charuco_corners.append(charuco_corners)
all_charuco_ids.append(charuco_ids)
# Calibrate camera
retval, camera_matrix, dist_coeffs, rvecs, tvecs = \
cv2.aruco.calibrateCameraCharuco(
all_charuco_corners, all_charuco_ids, board, image.shape[:2], None,
None)
# Save calibration data
np.save('camera_matrix.npy', camera_matrix)
np.save('dist_coeffs.npy', dist_coeffs)
cv2.destroyAllWindows()
calibrate_and_save_parameters()