-
Notifications
You must be signed in to change notification settings - Fork 0
/
calib.py
51 lines (41 loc) · 1.74 KB
/
calib.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
from cv2 import aruco
import numpy as np
def match_stereo_charuco(
l_images: list[np.array],
r_images: list[np.array],
detector: aruco.CharucoDetector
) -> tuple[list[np.array], list[np.array], list[np.array],
list[np.array], list[np.array], np.array]:
obj_point_sets = []
l_img_point_sets = []
r_img_point_sets = []
l_corner_sets = []
r_corner_sets = []
included_mask = np.ones(len(l_images), dtype=bool)
for i, (l_image, r_image) in enumerate(zip(l_images, r_images)):
l_corners, l_ids, _, _ = detector.detectBoard(l_image)
r_corners, r_ids, _, _ = detector.detectBoard(r_image)
if l_ids is None or r_ids is None:
included_mask[i] = 0
continue
# Find common IDs
common_ids = set(l_ids.flatten()).intersection(set(r_ids.flatten()))
if not common_ids:
included_mask[i] = 0
continue
# Filter out corners that are common in both images
l_common = np.isin(l_ids.flatten(), list(common_ids), True)
r_common = np.isin(r_ids.flatten(), list(common_ids), True)
l_corners = l_corners[l_common]
r_corners = r_corners[r_common]
ids = l_ids[l_common]
board = detector.getBoard()
obj_points, l_img_points = board.matchImagePoints(l_corners, ids)
_, r_img_points = board.matchImagePoints(r_corners, ids)
obj_point_sets.append(obj_points)
l_img_point_sets.append(l_img_points)
r_img_point_sets.append(r_img_points)
l_corner_sets.append(l_corners)
r_corner_sets.append(r_corners)
return (obj_point_sets, l_img_point_sets, r_img_point_sets,
l_corner_sets, r_corner_sets, included_mask.astype(bool))