-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibration.py
More file actions
2041 lines (1708 loc) · 95.3 KB
/
calibration.py
File metadata and controls
2041 lines (1708 loc) · 95.3 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import cv2
import numpy as np
import os
import fnmatch
from scipy.spatial import ConvexHull, distance
from scipy.cluster.vq import kmeans2
from image_annotation import MarkerDetector
class Calibration:
"""Class for camera calibration using ArUco markers."""
markers_required = 0
markers_total = 0
image_folders = {}
left_calibration = None
right_calibration = None
left_fisheye_calibration = None
right_fisheye_calibration = None
stereo_calibration = None
cameras = {"left":1, "right":0}
rectification_alpha = 0.95
def __init__(self, marker_dimensions=(8, 5), square_length=0.05, marker_length=0.037):
self.marker_detector = MarkerDetector(marker_dimensions=marker_dimensions)
self.marker_detector.board = self.marker_detector.create_board(square_length, marker_length)
self.markers_total = self.marker_detector.markers_total
self.markers_required = self.marker_detector.markers_required
self.image_folders = {"left":"images/left", "right":"images/right", "stereo":"images/stereo"}
self.images = {"left":[], "right":[], "stereo":[]}
# self.create_image_folders()
@classmethod
def from_config(cls, config):
"""
Create a Calibration instance from a config object.
Args:
config: ConfigParser object with configuration
Returns:
Configured Calibration instance
"""
marker_dims = (
config.getint('aruco', 'marker_dimensions_width'),
config.getint('aruco', 'marker_dimensions_height')
)
instance = cls(
marker_dimensions=marker_dims,
square_length=config.getfloat('aruco', 'square_length'),
marker_length=config.getfloat('aruco', 'marker_length')
)
# Set camera indices
instance.cameras['left'] = config.getint('cameras', 'left')
instance.cameras['right'] = config.getint('cameras', 'right')
# Set image folders
instance.image_folders['left'] = config.get('paths', 'left_images')
instance.image_folders['right'] = config.get('paths', 'right_images')
instance.image_folders['stereo'] = config.get('paths', 'stereo_images')
instance.image_folders['calibration'] = config.get('paths', 'calibration_output')
# Set rectification alpha
instance.rectification_alpha = config.getfloat('calibration', 'rectification_alpha')
instance.rectification_alpha_left = config.getfloat('calibration', 'rectification_alpha_left')
instance.rectification_alpha_right = config.getfloat('calibration', 'rectification_alpha_right')
# Set individual calibration flags
instance.calib_use_intrinsic_guess = config.getboolean('calibration', 'calib_use_intrinsic_guess')
instance.calib_fix_principal_point = config.getboolean('calibration', 'calib_fix_principal_point')
instance.calib_fix_aspect_ratio = config.getboolean('calibration', 'calib_fix_aspect_ratio')
instance.calib_zero_tangent_dist = config.getboolean('calibration', 'calib_zero_tangent_dist')
instance.calib_rational_model = config.getboolean('calibration', 'calib_rational_model')
instance.calib_thin_prism_model = config.getboolean('calibration', 'calib_thin_prism_model')
instance.calib_tilted_model = config.getboolean('calibration', 'calib_tilted_model')
instance.calib_fix_focal_length = config.getboolean('calibration', 'calib_fix_focal_length')
instance.calib_fix_k1 = config.getboolean('calibration', 'calib_fix_k1')
instance.calib_fix_k2 = config.getboolean('calibration', 'calib_fix_k2')
instance.calib_fix_k3 = config.getboolean('calibration', 'calib_fix_k3')
instance.calib_fix_k4 = config.getboolean('calibration', 'calib_fix_k4')
instance.calib_fix_k5 = config.getboolean('calibration', 'calib_fix_k5')
instance.calib_fix_k6 = config.getboolean('calibration', 'calib_fix_k6')
# Set stereo calibration flags
instance.stereo_fix_intrinsic = config.getboolean('calibration', 'stereo_fix_intrinsic')
instance.stereo_fix_focal_length = config.getboolean('calibration', 'stereo_fix_focal_length')
instance.stereo_fix_principal_point = config.getboolean('calibration', 'stereo_fix_principal_point')
instance.stereo_fix_aspect_ratio = config.getboolean('calibration', 'stereo_fix_aspect_ratio')
instance.stereo_use_intrinsic_guess = config.getboolean('calibration', 'stereo_use_intrinsic_guess')
instance.stereo_same_focal_length = config.getboolean('calibration', 'stereo_same_focal_length')
instance.stereo_zero_tangent_dist = config.getboolean('calibration', 'stereo_zero_tangent_dist')
instance.stereo_rational_model = config.getboolean('calibration', 'stereo_rational_model')
instance.stereo_thin_prism_model = config.getboolean('calibration', 'stereo_thin_prism_model')
instance.stereo_tilted_model = config.getboolean('calibration', 'stereo_tilted_model')
instance.stereo_fix_k1 = config.getboolean('calibration', 'stereo_fix_k1')
instance.stereo_fix_k2 = config.getboolean('calibration', 'stereo_fix_k2')
instance.stereo_fix_k3 = config.getboolean('calibration', 'stereo_fix_k3')
instance.stereo_fix_k4 = config.getboolean('calibration', 'stereo_fix_k4')
instance.stereo_fix_k5 = config.getboolean('calibration', 'stereo_fix_k5')
instance.stereo_fix_k6 = config.getboolean('calibration', 'stereo_fix_k6')
# Set debug output flag
instance.generate_debug_images = config.getboolean('calibration', 'generate_debug_images')
# Set marker color
instance.marker_detector.marker_colour = (
config.getint('aruco', 'marker_colour_b'),
config.getint('aruco', 'marker_colour_g'),
config.getint('aruco', 'marker_colour_r')
)
return instance
def save_calibration(self, name, data, save_format='npz'):
"""
Save calibration data in specified format(s).
Args:
name: Base filename without extension
data: Dictionary containing calibration data
save_format: Format to save - 'npz', 'yaml', or 'both'
"""
# Ensure calibration output folder exists
calib_folder = self.image_folders.get('calibration', 'calibration')
os.makedirs(calib_folder, exist_ok=True)
# Create full path
filepath = os.path.join(calib_folder, name)
if save_format in ('npz', 'both'):
np.savez(f"{filepath}.npz", **data)
print(f"Saved {filepath}.npz")
if save_format in ('yaml', 'both'):
# Use OpenCV's FileStorage for YAML output
fs = cv2.FileStorage(f"{filepath}.yaml", cv2.FILE_STORAGE_WRITE)
for key, value in data.items():
if isinstance(value, np.ndarray):
fs.write(key, value)
elif isinstance(value, list):
# Handle list of arrays (rvecs, tvecs)
if len(value) > 0 and isinstance(value[0], np.ndarray):
# Write as sequence of matrices
fs.write(key, value)
else:
# Regular list
fs.write(key, value)
elif isinstance(value, (int, float, str)):
fs.write(key, value)
fs.release()
print(f"Saved {filepath}.yaml")
def create_image_folders(self):
"""Create image folders for storing calibration images."""
for folder in self.image_folders.values():
if not os.path.exists(folder):
os.makedirs(folder)
print(f"Created folder: {folder}")
else:
for file in os.listdir(folder):
file_path = os.path.join(folder, file)
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted existing files from {file_path}")
def estimate_board_pose(self, charuco_corners, charuco_ids, image_shape):
"""
Estimate 3D pose (rotation, distance) of the calibration board.
Args:
charuco_corners: Detected charuco corners
charuco_ids: Corresponding IDs
image_shape: Tuple of (height, width)
Returns:
Dictionary with 'distance', 'tilt_x', 'tilt_y', 'rotation' or None if estimation fails
"""
if charuco_corners is None or len(charuco_corners) < 4:
return None
# Get 3D object points for the detected corners
obj_points = self.marker_detector.board.getChessboardCorners()[charuco_ids.flatten()]
# Use rough camera matrix estimate for initial pose
h, w = image_shape
focal_length = w # Rough estimate
camera_matrix = np.array([
[focal_length, 0, w / 2],
[0, focal_length, h / 2],
[0, 0, 1]
], dtype=np.float32)
dist_coeffs = np.zeros(5) # Assume no distortion for pose estimation
try:
# Solve PnP to get rotation and translation vectors
success, rvec, tvec = cv2.solvePnP(
obj_points,
charuco_corners,
camera_matrix,
dist_coeffs
)
if not success:
return None
# Convert rotation vector to angles
rotation_matrix, _ = cv2.Rodrigues(rvec)
# Extract Euler angles (in degrees)
# tilt_x: rotation around x-axis (pitch)
# tilt_y: rotation around y-axis (yaw)
# rotation: rotation around z-axis (roll)
sy = np.sqrt(rotation_matrix[0, 0]**2 + rotation_matrix[1, 0]**2)
if sy > 1e-6:
tilt_x = np.arctan2(rotation_matrix[2, 1], rotation_matrix[2, 2])
tilt_y = np.arctan2(-rotation_matrix[2, 0], sy)
rotation = np.arctan2(rotation_matrix[1, 0], rotation_matrix[0, 0])
else:
tilt_x = np.arctan2(-rotation_matrix[1, 2], rotation_matrix[1, 1])
tilt_y = np.arctan2(-rotation_matrix[2, 0], sy)
rotation = 0
# Convert to degrees
tilt_x = np.degrees(tilt_x)
tilt_y = np.degrees(tilt_y)
rotation = np.degrees(rotation)
# Calculate distance (Euclidean distance in 3D)
distance = np.linalg.norm(tvec)
return {
'distance': distance,
'tilt_x': tilt_x,
'tilt_y': tilt_y,
'rotation': rotation,
'tvec': tvec,
'rvec': rvec
}
except:
return None
def analyze_image_coverage(self, images_data, image_shape):
"""
Analyze spatial coverage and pose diversity of calibration images using scipy.
Args:
images_data: List of dicts with 'file', 'corners', 'ids', 'charuco_corners', 'charuco_ids' keys
image_shape: Tuple of (height, width)
Returns:
List of image score dictionaries
"""
if len(images_data) == 0:
return []
h, w = image_shape
cx, cy = w / 2, h / 2
max_radius = np.sqrt(cx**2 + cy**2)
# Define radial zones (4 zones from center to edge)
num_radial_zones = 4
# Define angular sectors (8 sectors around center)
num_angular_sectors = 8
# Collect all corner points across all images for density analysis
all_corners_global = []
# Track coverage score for each image
image_scores = []
for idx, img_data in enumerate(images_data):
corners = img_data['corners']
charuco_corners = img_data.get('charuco_corners')
charuco_ids = img_data.get('charuco_ids')
if corners is None or len(corners) == 0:
image_scores.append({
'idx': idx,
'radial_zones': set(),
'angular_sectors': set(),
'num_corners': 0,
'pose': None,
'hull_area': 0,
'corner_spread': 0
})
continue
# Estimate board pose
pose = self.estimate_board_pose(charuco_corners, charuco_ids, image_shape)
# Analyze spatial coverage using radial and angular sectors
radial_zones = set()
angular_sectors = set()
corner_points = []
for corner in corners:
if len(corner) > 0:
# Corner is already a 2D array, flatten to get [x, y]
pt = corner.reshape(-1)
for i in range(0, len(pt), 2):
if i + 1 < len(pt):
x, y = float(pt[i]), float(pt[i + 1])
corner_points.append((x, y))
all_corners_global.append((x, y))
# Calculate radial zone (distance from center)
dx, dy = x - cx, y - cy
distance_from_center = np.sqrt(dx**2 + dy**2)
radial_zone = min(int((distance_from_center / max_radius) * num_radial_zones), num_radial_zones - 1)
radial_zones.add(radial_zone)
# Calculate angular sector
angle = np.arctan2(dy, dx) # -pi to pi
angle_deg = np.degrees(angle) % 360 # 0 to 360
angular_sector = int(angle_deg / (360 / num_angular_sectors))
angular_sectors.add(angular_sector)
# Calculate convex hull area (indicates spread of corners)
hull_area = 0
if len(corner_points) >= 3:
try:
hull = ConvexHull(corner_points)
hull_area = hull.volume # In 2D, volume is area
except:
hull_area = 0
# Calculate corner spread using pairwise distances
corner_spread = 0
if len(corner_points) >= 2:
pts_array = np.array(corner_points)
# Use maximum pairwise distance as spread metric
dists = distance.pdist(pts_array)
corner_spread = np.max(dists) if len(dists) > 0 else 0
image_scores.append({
'idx': idx,
'radial_zones': radial_zones,
'angular_sectors': angular_sectors,
'num_corners': len(corner_points),
'pose': pose,
'hull_area': hull_area,
'corner_spread': corner_spread
})
# Analyze global corner density using k-means clustering
# Identify under-sampled regions
corner_density_score = {}
if len(all_corners_global) > 0:
# Create spatial grid clusters
num_clusters = min(16, len(all_corners_global)) # 4x4 grid of clusters
if num_clusters >= 2:
try:
corners_array = np.array(all_corners_global)
centroids, labels = kmeans2(corners_array, num_clusters, minit='points')
# Count corners per cluster
unique, counts = np.unique(labels, return_counts=True)
cluster_counts = dict(zip(unique, counts))
# Score images based on how many corners they contribute to sparse clusters
# (clusters with fewer corners are more valuable)
avg_count = np.mean(counts)
for idx, img_data in enumerate(images_data):
if image_scores[idx]['num_corners'] == 0:
corner_density_score[idx] = 0
continue
# This is a simplified approach - in practice would need to track
# which corners belong to which image and which clusters
# For now, just boost images with good spatial spread
corner_density_score[idx] = image_scores[idx]['hull_area'] / (w * h) * 100
except:
for idx in range(len(images_data)):
corner_density_score[idx] = 0
else:
for idx in range(len(images_data)):
corner_density_score[idx] = 0
# Add density score to image scores
for idx in range(len(image_scores)):
image_scores[idx]['density_score'] = corner_density_score.get(idx, 0)
return image_scores
def select_diverse_images(self, image_scores, max_images):
"""
Select images that maximize spatial coverage and pose diversity.
Args:
image_scores: List of score dicts from analyze_image_coverage
max_images: Maximum number of images to select
Returns:
List of selected indices
"""
if len(image_scores) <= max_images:
return [score['idx'] for score in image_scores]
selected_indices = []
covered_radial = set()
covered_angular = set()
# Track pose diversity bins
# Distance bins: near, medium, far
# Tilt bins: 0-15°, 15-30°, 30-45°, 45-60°, 60+°
# Rotation bins: 0-45°, 45-90°, 90-135°, 135-180°
covered_poses = {'distance': set(), 'tilt': set(), 'rotation': set()}
remaining = list(image_scores)
# Greedy selection: maximize coverage and pose diversity
while len(selected_indices) < max_images and remaining:
best_idx = None
best_score = -1
best_item = None
for item in remaining:
# Spatial coverage score
new_radial = item['radial_zones'] - covered_radial
new_angular = item['angular_sectors'] - covered_angular
spatial_score = len(new_radial) * 20 + len(new_angular) * 10
# Convex hull area bonus (larger spread = better)
hull_bonus = item['hull_area'] * 0.001
# Corner spread bonus
spread_bonus = item['corner_spread'] * 0.01
# Density score (fills gaps in coverage)
density_bonus = item.get('density_score', 0) * 0.5
# Pose diversity score
pose_score = 0
distance_bin = None
tilt_bin = None
rotation_bin = None
if item['pose'] is not None:
pose = item['pose']
# Bin distance (3 bins)
distance_bin = min(int(pose['distance'] / 0.5), 2) # 0-0.5m, 0.5-1m, 1m+
if distance_bin not in covered_poses['distance']:
pose_score += 15
# Bin tilt (absolute value, combined x and y)
tilt = np.sqrt(pose['tilt_x']**2 + pose['tilt_y']**2)
tilt_bin = min(int(abs(tilt) / 15), 4) # 0-15°, 15-30°, 30-45°, 45-60°, 60+°
if tilt_bin not in covered_poses['tilt']:
pose_score += 15
# Bin rotation (in-plane rotation)
rotation_bin = int(abs(pose['rotation']) / 45) % 4 # 0-45°, 45-90°, 90-135°, 135-180°
if rotation_bin not in covered_poses['rotation']:
pose_score += 10
# Quality bonus
quality_score = item['num_corners'] * 0.1
# Combined score
total_score = spatial_score + pose_score + quality_score + hull_bonus + spread_bonus + density_bonus
if total_score > best_score:
best_score = total_score
best_idx = item['idx']
best_item = item
if best_item is None:
break
# Update coverage tracking
selected_indices.append(best_idx)
covered_radial.update(best_item['radial_zones'])
covered_angular.update(best_item['angular_sectors'])
if best_item['pose'] is not None:
pose = best_item['pose']
distance_bin = min(int(pose['distance'] / 0.5), 2)
tilt = np.sqrt(pose['tilt_x']**2 + pose['tilt_y']**2)
tilt_bin = min(int(abs(tilt) / 15), 4)
rotation_bin = int(abs(pose['rotation']) / 45) % 4
covered_poses['distance'].add(distance_bin)
covered_poses['tilt'].add(tilt_bin)
covered_poses['rotation'].add(rotation_bin)
remaining.remove(best_item)
# If we need more images, add ones with highest corner counts
if len(selected_indices) < max_images:
remaining_sorted = sorted(remaining, key=lambda x: x['num_corners'], reverse=True)
for item in remaining_sorted[:max_images - len(selected_indices)]:
selected_indices.append(item['idx'])
return sorted(selected_indices)
def calibrate(self, camera_name, fisheye=True, save_format='npz', max_images=30):
"""
Calibrate a single camera using captured images.
Args:
camera_name: Name of the camera ("left" or "right")
fisheye: Whether to use fisheye calibration model
save_format: Format to save calibration - 'npz', 'json', or 'both'
max_images: Maximum number of images to use (default: 30, good balance of accuracy/speed)
"""
folder = self.image_folders[camera_name]
print(f"Opening folder: {folder}")
# Support multiple image formats
image_extensions = ('.png', '.jpg', '.jpeg', '.bmp')
images = [os.path.join(folder, f) for f in os.listdir(folder)
if f.lower().endswith(image_extensions)]
images.sort() # Ensure files are in order
print(f"Found {len(images)} images in {folder}")
if len(images) == 0:
print(f"Error: No images found in {folder}")
print(f"Please capture images first using: python main.py -a capture-{camera_name}")
return
# First pass: analyze all images for coverage
print("Analyzing image coverage and diversity...")
images_data = []
image_shape = None
for image_file in images:
image = cv2.imread(image_file)
if image is None:
continue
if image_shape is None:
image_shape = image.shape[:2]
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
marker_corners, marker_ids, _ = self.marker_detector.detector.detectMarkers(image_gray)
# Interpolate charuco corners for pose estimation
charuco_corners = None
charuco_ids = None
if marker_corners is not None and len(marker_corners) >= self.markers_required:
retval, charuco_corners, charuco_ids = cv2.aruco.interpolateCornersCharuco(
marker_corners, marker_ids, image_gray, self.marker_detector.board)
# Store image data for analysis
images_data.append({
'file': image_file,
'corners': marker_corners,
'ids': marker_ids,
'charuco_corners': charuco_corners,
'charuco_ids': charuco_ids
})
if image_shape is None:
print(f"Error: Could not read any images")
return
# Analyze coverage and select diverse subset
image_scores = self.analyze_image_coverage(images_data, image_shape)
if len(images) > max_images:
selected_indices = self.select_diverse_images(image_scores, max_images)
images_data_selected = [images_data[i] for i in selected_indices]
# Keep track of unused images for backfill
unused_indices = [i for i in range(len(images_data)) if i not in selected_indices]
print(f"Selected {len(images_data_selected)} diverse images from {len(images)} total")
# Show coverage statistics
all_radial = set()
all_angular = set()
pose_distances = []
pose_tilts = []
pose_rotations = []
for idx in selected_indices:
all_radial.update(image_scores[idx]['radial_zones'])
all_angular.update(image_scores[idx]['angular_sectors'])
if image_scores[idx]['pose'] is not None:
pose = image_scores[idx]['pose']
pose_distances.append(pose['distance'])
tilt = np.sqrt(pose['tilt_x']**2 + pose['tilt_y']**2)
pose_tilts.append(tilt)
pose_rotations.append(abs(pose['rotation']))
print(f"Spatial coverage: {len(all_radial)}/4 radial zones, {len(all_angular)}/8 angular sectors")
if pose_distances:
print(f"Pose diversity: distance {min(pose_distances):.2f}-{max(pose_distances):.2f}m, "
f"tilt {min(pose_tilts):.1f}-{max(pose_tilts):.1f}°, "
f"rotation {min(pose_rotations):.1f}-{max(pose_rotations):.1f}°")
else:
images_data_selected = images_data
unused_indices = []
print(f"Using all {len(images_data_selected)} images")
# Second pass: extract calibration data from selected images
all_charuco_corners = []
all_charuco_ids = []
failed_indices = []
total_images = len(images_data_selected)
print("Extracting calibration data...")
for idx, img_data in enumerate(images_data_selected):
display_idx = idx + 1
if display_idx % 10 == 0 or display_idx == total_images:
print(f"Progress: {display_idx}/{total_images} images processed...")
image = cv2.imread(img_data['file'])
if image is None:
failed_indices.append(idx)
continue
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
marker_corners = img_data['corners']
marker_ids = img_data['ids']
if marker_corners is not None and len(marker_corners) >= self.markers_required:
if(len(marker_corners) != len(marker_ids)):
print(f"Error: Marker corners and IDs are not the same length")
failed_indices.append(idx)
continue
retval, charuco_corners, charuco_ids = cv2.aruco.interpolateCornersCharuco(
marker_corners, marker_ids, image_gray, self.marker_detector.board)
if retval > 0:
all_charuco_corners.append(charuco_corners)
all_charuco_ids.append(charuco_ids)
else:
failed_indices.append(idx)
else:
failed_indices.append(idx)
# Backfill with unused images if we lost any during validation
if failed_indices and unused_indices:
print(f"\n{len(failed_indices)} images failed validation, attempting to backfill from unused images...")
backfilled = 0
for unused_idx in unused_indices:
if backfilled >= len(failed_indices):
break
img_data = images_data[unused_idx]
image = cv2.imread(img_data['file'])
if image is None:
continue
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
marker_corners = img_data['corners']
marker_ids = img_data['ids']
if marker_corners is not None and len(marker_corners) >= self.markers_required:
if len(marker_corners) != len(marker_ids):
continue
retval, charuco_corners, charuco_ids = cv2.aruco.interpolateCornersCharuco(
marker_corners, marker_ids, image_gray, self.marker_detector.board)
if retval > 0:
all_charuco_corners.append(charuco_corners)
all_charuco_ids.append(charuco_ids)
backfilled += 1
if backfilled > 0:
print(f"Successfully backfilled {backfilled} replacement images")
final_ignored = len(failed_indices) - (backfilled if failed_indices and unused_indices else 0)
if final_ignored > 0:
print(f"\nIgnored {final_ignored} of {len(images_data_selected)} selected images (could not backfill)")
print(f"Calibrating using {len(all_charuco_corners)} images with valid corners")
if len(all_charuco_corners) == 0:
print(f"Error: No valid calibration images found!")
print(f"Make sure your images contain the ArUco/Charuco board with at least {int(self.markers_required)} markers visible.")
return
if image_shape is None:
print(f"Error: Could not determine image dimensions")
return
if(fisheye == True):
K = np.zeros((3, 3))
D = np.zeros((4, 1))
object_points = []
image_points = []
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
for corners, ids in zip(all_charuco_corners, all_charuco_ids):
if(len(corners) > 0):
object_points.append(self.marker_detector.board.getChessboardCorners()[ids])
image_points.append(corners)
image_count = len(all_charuco_corners)
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(image_count)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(image_count)]
retval, K, D, rvecs, tvecs = cv2.fisheye.calibrate(
object_points,
image_points,
image_shape,
K, # Use initialized zeros, not prior calibration
D, # Use initialized zeros, not prior calibration
rvecs,
tvecs,
cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW,
criteria)
print(f"projection error: {retval}")
print('K: ', K)
print('Distortion coefficients: ', D)
data = {
'camera_label': camera_name,
'image_width': image_shape[1],
'image_height': image_shape[0],
'model': 'fisheye',
'reprojection_error': retval,
'K': K,
'D': D,
'rvecs': rvecs,
'tvecs': tvecs
}
self.save_calibration(f"{camera_name}_fisheye", data, save_format)
else:
# Use robust calibrateCamera with explicit object point mapping
# This matches the approach from the other script for better accuracy
all_obj_corners = self.marker_detector.board.getChessboardCorners()
obj_points = []
img_points = []
for corners, ids in zip(all_charuco_corners, all_charuco_ids):
# Map detected corner IDs to their 3D positions
ids_flat = ids.flatten()
obj_pts = all_obj_corners[ids_flat]
obj_points.append(obj_pts)
img_points.append(corners)
# Build calibration flags
calib_flags = 0
flag_descriptions = []
if hasattr(self, 'calib_use_intrinsic_guess') and self.calib_use_intrinsic_guess:
calib_flags |= cv2.CALIB_USE_INTRINSIC_GUESS
flag_descriptions.append("CALIB_USE_INTRINSIC_GUESS")
if hasattr(self, 'calib_fix_principal_point') and self.calib_fix_principal_point:
calib_flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
flag_descriptions.append("CALIB_FIX_PRINCIPAL_POINT")
if hasattr(self, 'calib_fix_aspect_ratio') and self.calib_fix_aspect_ratio:
calib_flags |= cv2.CALIB_FIX_ASPECT_RATIO
flag_descriptions.append("CALIB_FIX_ASPECT_RATIO")
if hasattr(self, 'calib_zero_tangent_dist') and self.calib_zero_tangent_dist:
calib_flags |= cv2.CALIB_ZERO_TANGENT_DIST
flag_descriptions.append("CALIB_ZERO_TANGENT_DIST")
if hasattr(self, 'calib_rational_model') and self.calib_rational_model:
calib_flags |= cv2.CALIB_RATIONAL_MODEL
flag_descriptions.append("CALIB_RATIONAL_MODEL")
if hasattr(self, 'calib_thin_prism_model') and self.calib_thin_prism_model:
calib_flags |= cv2.CALIB_THIN_PRISM_MODEL
flag_descriptions.append("CALIB_THIN_PRISM_MODEL")
if hasattr(self, 'calib_tilted_model') and self.calib_tilted_model:
calib_flags |= cv2.CALIB_TILTED_MODEL
flag_descriptions.append("CALIB_TILTED_MODEL")
if hasattr(self, 'calib_fix_focal_length') and self.calib_fix_focal_length:
calib_flags |= cv2.CALIB_FIX_FOCAL_LENGTH
flag_descriptions.append("CALIB_FIX_FOCAL_LENGTH")
if hasattr(self, 'calib_fix_k1') and self.calib_fix_k1:
calib_flags |= cv2.CALIB_FIX_K1
flag_descriptions.append("CALIB_FIX_K1")
if hasattr(self, 'calib_fix_k2') and self.calib_fix_k2:
calib_flags |= cv2.CALIB_FIX_K2
flag_descriptions.append("CALIB_FIX_K2")
if hasattr(self, 'calib_fix_k3') and self.calib_fix_k3:
calib_flags |= cv2.CALIB_FIX_K3
flag_descriptions.append("CALIB_FIX_K3")
if hasattr(self, 'calib_fix_k4') and self.calib_fix_k4:
calib_flags |= cv2.CALIB_FIX_K4
flag_descriptions.append("CALIB_FIX_K4")
if hasattr(self, 'calib_fix_k5') and self.calib_fix_k5:
calib_flags |= cv2.CALIB_FIX_K5
flag_descriptions.append("CALIB_FIX_K5")
if hasattr(self, 'calib_fix_k6') and self.calib_fix_k6:
calib_flags |= cv2.CALIB_FIX_K6
flag_descriptions.append("CALIB_FIX_K6")
if calib_flags == 0:
print("Using default calibration flags")
else:
print(f"Using calibration flags: {', '.join(flag_descriptions)}")
# Use cv2.calibrateCamera for more robust optimization
retval, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(
obj_points,
img_points,
image_shape[::-1], # (width, height) format
None,
None,
flags=calib_flags
)
print(f"camera matrix: {camera_matrix}")
print(f"dist coeffs: {dist_coeffs}")
data = {
'camera_label': camera_name,
'image_width': image_shape[1],
'image_height': image_shape[0],
'model': 'pinhole',
'reprojection_error': retval,
'camera_matrix': camera_matrix,
'dist_coeffs': dist_coeffs,
'rvecs': rvecs,
'tvecs': tvecs
}
self.save_calibration(camera_name, data, save_format)
print(f"Calibration complete for {camera_name}, error: {retval}")
def stereo_calibrate(self, save_format='npz', use_config_flags=True):
"""
Perform stereo calibration using matched image pairs.
Args:
save_format: Format to save calibration - 'npz', 'yaml', or 'both'
use_config_flags: Whether to use stereo calibration flags from config (default: True)
"""
# Load individual camera calibrations first
print("Loading individual camera calibrations...")
if self.left_calibration is None or self.right_calibration is None:
self.load_calibrations()
if self.left_calibration is None:
print("Error: Left camera calibration not found. Please run 'calibrate-left' first.")
return
if self.right_calibration is None:
print("Error: Right camera calibration not found. Please run 'calibrate-right' first.")
return
print("Left camera calibration loaded successfully")
print(f" Keys: {list(self.left_calibration.keys())}")
print("Right camera calibration loaded successfully")
print(f" Keys: {list(self.right_calibration.keys())}")
stereo_image_folder = self.image_folders["stereo"]
left_images = []
right_images = []
# Support multiple image formats
image_extensions = ('.png', '.jpg', '.jpeg', '.bmp')
images = [os.path.join(stereo_image_folder, f) for f in os.listdir(stereo_image_folder)
if f.lower().endswith(image_extensions)]
for file in images:
if(fnmatch.fnmatch(file, '*left*')):
left_images.append(file)
else:
right_images.append(file)
left_images.sort() # Ensure files are in order
right_images.sort() # Ensure files are in order
objpoints = [] # 3d point in real world space
imgpoints_left = [] # 2d points in image plane for left camera
imgpoints_right = [] # 2d points in image plane for right camera
imgids_left = []
imgids_right = []
allCorners = {'left': [], 'right': []}
allIds = {'left': [], 'right': []}
print(f"Left images: {len(left_images)}, Right images: {len(right_images)}")
for left_image_file, right_image_file in zip(left_images, right_images):
left_image = cv2.imread(left_image_file)
right_image = cv2.imread(right_image_file)
left_gray = cv2.cvtColor(left_image, cv2.COLOR_BGR2GRAY)
right_gray = cv2.cvtColor(right_image, cv2.COLOR_BGR2GRAY)
# Find the Charuco corners
leftCorners, leftIds, _ = self.marker_detector.detector.detectMarkers(left_gray)
rightCorners, rightIds, _ = self.marker_detector.detector.detectMarkers(right_gray)
print(f"Images: {left_image_file}, {right_image_file}")
print(f"left corners: {len(allCorners['left'])}, left ids: {len(allIds['left'])}, right corners: {len(allCorners['right'])}, right ids: {len(allIds['right'])}")
if leftIds is not None:
retval_left, charuco_corners_left, charuco_ids_left = cv2.aruco.interpolateCornersCharuco(leftCorners, leftIds, left_gray, self.marker_detector.board)
if(retval_left > 0):
allCorners['left'].append(charuco_corners_left)
allIds['left'].append(charuco_ids_left)
if rightIds is not None:
retval_right, charuco_corners_right, charuco_ids_right = cv2.aruco.interpolateCornersCharuco(rightCorners, rightIds, right_gray, self.marker_detector.board)
if(retval_right > 0):
allCorners['right'].append(charuco_corners_right)
allIds['right'].append(charuco_ids_right)
# Match points and perform calibration
matched_object_points = []
matched_corners_left = []
matched_corners_right = []
for i in range(min(len(allCorners['left']), len(allCorners['right']))):
# Ensure matching ids in both left and right images
common_ids = np.intersect1d(allIds['left'][i], allIds['right'][i])
if len(common_ids) > 0:
indices_left = np.isin(allIds['left'][i], common_ids).flatten()
indices_right = np.isin(allIds['right'][i], common_ids).flatten()
matched_object_points.append(self.marker_detector.board.getChessboardCorners()[common_ids, :])
matched_corners_left.append(allCorners['left'][i][indices_left])
matched_corners_right.append(allCorners['right'][i][indices_right])
# Now use matched corners to perform stereo calibration
if len(matched_corners_left) and len(matched_corners_right):
print(f"\nPerforming stereo calibration with {len(matched_corners_left)} matched pairs...")
# Build stereo calibration flags
stereo_flags = 0
flag_descriptions = []
if use_config_flags and hasattr(self, 'stereo_fix_intrinsic') and self.stereo_fix_intrinsic:
stereo_flags |= cv2.CALIB_FIX_INTRINSIC
flag_descriptions.append("CALIB_FIX_INTRINSIC: Trust individual calibrations")
if use_config_flags and hasattr(self, 'stereo_fix_focal_length') and self.stereo_fix_focal_length:
stereo_flags |= cv2.CALIB_FIX_FOCAL_LENGTH
flag_descriptions.append("CALIB_FIX_FOCAL_LENGTH: Fix focal lengths")
if use_config_flags and hasattr(self, 'stereo_fix_principal_point') and self.stereo_fix_principal_point:
stereo_flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
flag_descriptions.append("CALIB_FIX_PRINCIPAL_POINT: Fix principal points")
if use_config_flags and hasattr(self, 'stereo_fix_aspect_ratio') and self.stereo_fix_aspect_ratio:
stereo_flags |= cv2.CALIB_FIX_ASPECT_RATIO
flag_descriptions.append("CALIB_FIX_ASPECT_RATIO: Fix fx/fy ratio")
if use_config_flags and hasattr(self, 'stereo_use_intrinsic_guess') and self.stereo_use_intrinsic_guess:
stereo_flags |= cv2.CALIB_USE_INTRINSIC_GUESS
flag_descriptions.append("CALIB_USE_INTRINSIC_GUESS: Use provided intrinsics as initial guess")
if use_config_flags and hasattr(self, 'stereo_same_focal_length') and self.stereo_same_focal_length:
stereo_flags |= cv2.CALIB_SAME_FOCAL_LENGTH
flag_descriptions.append("CALIB_SAME_FOCAL_LENGTH: Enforce identical focal lengths")
if use_config_flags and hasattr(self, 'stereo_zero_tangent_dist') and self.stereo_zero_tangent_dist:
stereo_flags |= cv2.CALIB_ZERO_TANGENT_DIST
flag_descriptions.append("CALIB_ZERO_TANGENT_DIST: Set p1, p2 to zero")
if use_config_flags and hasattr(self, 'stereo_rational_model') and self.stereo_rational_model:
stereo_flags |= cv2.CALIB_RATIONAL_MODEL
flag_descriptions.append("CALIB_RATIONAL_MODEL: Enable k4, k5, k6")
if use_config_flags and hasattr(self, 'stereo_thin_prism_model') and self.stereo_thin_prism_model:
stereo_flags |= cv2.CALIB_THIN_PRISM_MODEL
flag_descriptions.append("CALIB_THIN_PRISM_MODEL: Enable s1, s2, s3, s4")
if use_config_flags and hasattr(self, 'stereo_tilted_model') and self.stereo_tilted_model:
stereo_flags |= cv2.CALIB_TILTED_MODEL
flag_descriptions.append("CALIB_TILTED_MODEL: Enable tauX, tauY")
if use_config_flags and hasattr(self, 'stereo_fix_k1') and self.stereo_fix_k1:
stereo_flags |= cv2.CALIB_FIX_K1
flag_descriptions.append("CALIB_FIX_K1: Fix k1 coefficient")
if use_config_flags and hasattr(self, 'stereo_fix_k2') and self.stereo_fix_k2:
stereo_flags |= cv2.CALIB_FIX_K2
flag_descriptions.append("CALIB_FIX_K2: Fix k2 coefficient")
if use_config_flags and hasattr(self, 'stereo_fix_k3') and self.stereo_fix_k3:
stereo_flags |= cv2.CALIB_FIX_K3
flag_descriptions.append("CALIB_FIX_K3: Fix k3 coefficient")
if use_config_flags and hasattr(self, 'stereo_fix_k4') and self.stereo_fix_k4:
stereo_flags |= cv2.CALIB_FIX_K4
flag_descriptions.append("CALIB_FIX_K4: Fix k4 coefficient")
if use_config_flags and hasattr(self, 'stereo_fix_k5') and self.stereo_fix_k5:
stereo_flags |= cv2.CALIB_FIX_K5
flag_descriptions.append("CALIB_FIX_K5: Fix k5 coefficient")
if use_config_flags and hasattr(self, 'stereo_fix_k6') and self.stereo_fix_k6:
stereo_flags |= cv2.CALIB_FIX_K6
flag_descriptions.append("CALIB_FIX_K6: Fix k6 coefficient")
if stereo_flags == 0:
print("Using default stereo calibration (no flags)")
else:
print("Using stereo calibration flags:")
for desc in flag_descriptions:
print(f" - {desc}")
ret, camera_matrix_left, dist_coeffs_left, camera_matrix_right, dist_coeffs_right, R, T, E, F = cv2.stereoCalibrate(
objectPoints=matched_object_points,
imagePoints1=matched_corners_left,
imagePoints2=matched_corners_right,
cameraMatrix1=self.left_calibration['camera_matrix'],