forked from TemugeB/python_stereo_camera_calibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calib.py
823 lines (619 loc) · 28.2 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
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
import cv2 as cv
import glob
import numpy as np
import sys
from scipy import linalg
import yaml
from ximea import xiapi
import os
from sys import platform
import argparse
import time
#This will contain the calibration settings from the calibration_settings.yaml file
calibration_settings = {}
#Given Projection matrices P1 and P2, and pixel coordinates point1 and point2, return triangulated 3D point.
def DLT(P1, P2, point1, point2):
A = [point1[1]*P1[2,:] - P1[1,:],
P1[0,:] - point1[0]*P1[2,:],
point2[1]*P2[2,:] - P2[1,:],
P2[0,:] - point2[0]*P2[2,:]
]
A = np.array(A).reshape((4,4))
B = A.transpose() @ A
U, s, Vh = linalg.svd(B, full_matrices = False)
#print('Triangulated point: ')
#print(Vh[3,0:3]/Vh[3,3])
return Vh[3,0:3]/Vh[3,3]
#Open and load the calibration_settings.yaml file
def parse_calibration_settings_file(filename):
global calibration_settings
if not os.path.exists(filename):
print('File does not exist:', filename)
quit()
print('Using for calibration settings: ', filename)
with open(filename) as f:
calibration_settings = yaml.safe_load(f)
#rudimentray check to make sure correct file was loaded
if 'camera0' not in calibration_settings.keys():
print('camera0 key was not found in the settings file. Check if correct calibration_settings.yaml file was passed')
quit()
#Open camera stream and save frames
def save_frames_single_camera(camera_name, devnum):
#create frames directory
if not os.path.exists('frames'):
os.mkdir('frames')
#get settings
camera_device_id = calibration_settings[camera_name]
CAMERAS_ON_SAME_CONTROLLER = 2
#create instance for cameras
cam1 = xiapi.Camera(dev_id=devnum)
#start communication
print('Opening cameras...')
cam1.open_device_by_SN(camera_device_id)
#set interface data rate
interface_data_rate=cam1.get_limit_bandwidth()
camera_data_rate = int(interface_data_rate / CAMERAS_ON_SAME_CONTROLLER)
#set data rate
cam1.set_limit_bandwidth(camera_data_rate)
#print device serial numbers
print('Camera 1 serial number: ' + str(cam1.get_device_sn()))
# create instance for first connected camera
#cam = xiapi.Camera()
# start communication
#print('Opening first camera...')
#cam.open_device()
# settingss
cam1.set_exposure(2000)
cam1.set_gain(10.0)
cam1.set_imgdataformat('XI_MONO8')
cam1.set_width(720)
cam1.set_height(720)
#cam.set_limit_bandwidth_mode()
cam1.set_offsetX(240)
cam1.set_offsetY(152)
# create instance of Image to store image data and metadata
img1 = xiapi.Image()
# start data acquisition
print('Starting data acquisition...')
cam1.start_acquisition()
number_to_save = calibration_settings['mono_calibration_frames']
view_resize = calibration_settings['view_resize']
cooldown_time = calibration_settings['cooldown']
#open video stream and change resolution.
#Note: if unsupported resolution is used, this does NOT raise an error.
#cap = cv.VideoCapture(camera_device_id)
# cap.set(3, width)
# cap.set(4, height)
cooldown = cooldown_time
start = False
saved_count = 0
while True:
cam1.get_image(img1)
# create numpy array with data from camera. Dimensions of the array are
# determined by imgdataformat
data1 = img1.get_image_data_numpy()
# ret, frame = cap.read()
# if ret == False:
# #if no video data is received, can't calibrate the camera, so exit.
# print("No video data received from camera. Exiting...")
# quit()
frame_small = cv.resize(data1, None, fx = 1/view_resize, fy=1/view_resize)
if not start:
cv.putText(frame_small, "Press SPACEBAR to start collection frames", (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
if start:
cooldown -= 1
cv.putText(frame_small, "Cooldown: " + str(cooldown), (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
cv.putText(frame_small, "Num frames: " + str(saved_count), (50,100), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
#save the frame when cooldown reaches 0.
if cooldown <= 0:
savename = os.path.join('frames', camera_name + '_' + str(saved_count) + '.png')
cv.imwrite(savename, data1)
saved_count += 1
cooldown = cooldown_time
cv.imshow('frame_small', frame_small)
k = cv.waitKey(1)
if k == 27:
#if ESC is pressed at any time, the program will exit.
quit()
if k == 32:
#Press spacebar to start data collection
start = True
#break out of the loop when enough number of frames have been saved
if saved_count == number_to_save: break
cv.destroyAllWindows()
#Calibrate single camera to obtain camera intrinsic parameters from saved frames.
def calibrate_camera_for_intrinsic_parameters(images_prefix):
#NOTE: images_prefix contains camera name: "frames/camera0*".
images_names = glob.glob(images_prefix)
#read all frames
images = [cv.imread(imname, 1) for imname in images_names]
#criteria used by checkerboard pattern detector.
#Change this if the code can't find the checkerboard.
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.001)
rows = calibration_settings['checkerboard_rows']
columns = calibration_settings['checkerboard_columns']
world_scaling = calibration_settings['checkerboard_box_size_scale'] #this will change to user defined length scale
#coordinates of squares in the checkerboard world space
objp = np.zeros((rows*columns,3), np.float32)
objp[:,:2] = np.mgrid[0:rows,0:columns].T.reshape(-1,2)
objp = world_scaling* objp
#frame dimensions. Frames should be the same size.
width = images[0].shape[1]
height = images[0].shape[0]
#Pixel coordinates of checkerboards
imgpoints = [] # 2d points in image plane.
#coordinates of the checkerboard in checkerboard world space.
objpoints = [] # 3d point in real world space
for i, frame in enumerate(images):
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
#find the checkerboard
ret, corners = cv.findChessboardCorners(gray, (rows, columns), None)
if ret == True:
#Convolution size used to improve corner detection. Don't make this too large.
conv_size = (11, 11)
#opencv can attempt to improve the checkerboard coordinates
corners = cv.cornerSubPix(gray, corners, conv_size, (-1, -1), criteria)
cv.drawChessboardCorners(frame, (rows,columns), corners, ret)
cv.putText(frame, 'If detected points are poor, press "s" to skip this sample', (25, 25), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
cv.imshow('img', frame)
k = cv.waitKey(0)
if k & 0xFF == ord('s'):
print('skipping')
continue
objpoints.append(objp)
imgpoints.append(corners)
cv.destroyAllWindows()
ret, cmtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, (width, height), None, None)
print('rmse:', ret)
print('camera matrix:\n', cmtx)
print('distortion coeffs:', dist)
return cmtx, dist
#save camera intrinsic parameters to file
def save_camera_intrinsics(camera_matrix, distortion_coefs, camera_name):
#create folder if it does not exist
if not os.path.exists('camera_parameters'):
os.mkdir('camera_parameters')
out_filename = os.path.join('camera_parameters', camera_name + '_intrinsics.dat')
outf = open(out_filename, 'w')
outf.write('intrinsic:\n')
for l in camera_matrix:
for en in l:
outf.write(str(en) + ' ')
outf.write('\n')
outf.write('distortion:\n')
for en in distortion_coefs[0]:
outf.write(str(en) + ' ')
outf.write('\n')
#open both cameras and take calibration frames
def save_frames_two_cams(camera0_name, devnum1, camera1_name, devnum2):
#create frames directory
if not os.path.exists('frames_pair'):
os.mkdir('frames_pair')
#settings for taking data
view_resize = calibration_settings['view_resize']
cooldown_time = calibration_settings['cooldown']
number_to_save = calibration_settings['stereo_calibration_frames']
#open the video streams
# cap0 = cv.VideoCapture(calibration_settings[camera0_name])
# cap1 = cv.VideoCapture(calibration_settings[camera1_name])
#set camera resolutions
# width = calibration_settings['frame_width']
# height = calibration_settings['frame_height']
# cap0.set(3, width)
# cap0.set(4, height)
# cap1.set(3, width)
# cap1.set(4, height)
CAMERAS_ON_SAME_CONTROLLER = 2
#create instance for cameras
cam1 = xiapi.Camera(dev_id=devnum1)
cam2 = xiapi.Camera(dev_id=devnum2)
#start communication
print('Opening cameras...')
cam1.open_device_by_SN(calibration_settings[camera0_name])
cam2.open_device_by_SN(calibration_settings[camera1_name])
#set interface data rate
interface_data_rate=cam1.get_limit_bandwidth()
camera_data_rate = int(interface_data_rate / CAMERAS_ON_SAME_CONTROLLER)
#set data rate
cam1.set_limit_bandwidth(camera_data_rate)
cam2.set_limit_bandwidth(camera_data_rate)
#print device serial numbers
print('Camera 1 serial number: ' + str(cam1.get_device_sn()))
print('Camera 2 serial number: ' + str(cam2.get_device_sn()))
# create instance for first connected camera
#cam = xiapi.Camera()
# start communication
#print('Opening first camera...')
#cam.open_device()
# settings
cam1.set_exposure(2000)
cam1.set_gain(10.0)
cam1.set_imgdataformat('XI_MONO8')
cam1.set_width(720)
cam1.set_height(720)
#cam.set_limit_bandwidth_mode()
cam1.set_offsetX(240)
cam1.set_offsetY(152)
cam2.set_exposure(2000)
cam2.set_gain(10.0)
cam2.set_imgdataformat('XI_MONO8')
cam2.set_width(720)
cam2.set_height(720)
#cam.set_limit_bandwidth_mode()
cam2.set_offsetX(240)
cam2.set_offsetY(152)
# create instance of Image to store image data and metadata
img1 = xiapi.Image()
img2 = xiapi.Image()
# start data acquisition
print('Starting data acquisition...')
number_to_save = calibration_settings['mono_calibration_frames']
view_resize = calibration_settings['view_resize']
cooldown_time = calibration_settings['cooldown']
#open video stream and change resolution.
#Note: if unsupported resolution is used, this does NOT raise an error.
#cap = cv.VideoCapture(camera_device_id)
# cap.set(3, width)
# cap.set(4, height)
cooldown = cooldown_time
start = False
saved_count = 0
while True:
cam1.get_image(img1)
cam2.get_image(img2)
# create numpy array with data from camera. Dimensions of the array are
# determined by imgdataformat
data1 = img1.get_image_data_numpy()
data2 = img2.get_image_data_numpy()
#ret0, frame0 = cap0.read()
#ret1, frame1 = cap1.read()
# if not ret0 or not ret1:
# print('Cameras not returning video data. Exiting...')
# quit()
frame0_small = cv.resize(data1, None, fx=1./view_resize, fy=1./view_resize)
frame1_small = cv.resize(data2, None, fx=1./view_resize, fy=1./view_resize)
if not start:
cv.putText(frame0_small, "Make sure both cameras can see the calibration pattern well", (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
cv.putText(frame0_small, "Press SPACEBAR to start collection frames", (50,100), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
if start:
cooldown -= 1
cv.putText(frame0_small, "Cooldown: " + str(cooldown), (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
cv.putText(frame0_small, "Num frames: " + str(saved_count), (50,100), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
cv.putText(frame1_small, "Cooldown: " + str(cooldown), (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
cv.putText(frame1_small, "Num frames: " + str(saved_count), (50,100), cv.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 1)
#save the frame when cooldown reaches 0.
if cooldown <= 0:
savename = os.path.join('frames_pair', camera0_name + '_' + str(saved_count) + '.png')
cv.imwrite(savename, data1)
savename = os.path.join('frames_pair', camera1_name + '_' + str(saved_count) + '.png')
cv.imwrite(savename, data2)
saved_count += 1
cooldown = cooldown_time
cv.imshow('frame0_small', frame0_small)
cv.imshow('frame1_small', frame1_small)
k = cv.waitKey(1)
if k == 27:
#if ESC is pressed at any time, the program will exit.
quit()
if k == 32:
#Press spacebar to start data collection
start = True
#break out of the loop when enough number of frames have been saved
if saved_count == number_to_save: break
cv.destroyAllWindows()
#open paired calibration frames and stereo calibrate for cam0 to cam1 coorindate transformations
def stereo_calibrate(mtx0, dist0, mtx1, dist1, frames_prefix_c0, frames_prefix_c1):
#read the synched frames
c0_images_names = sorted(glob.glob(frames_prefix_c0))
c1_images_names = sorted(glob.glob(frames_prefix_c1))
#open images
c0_images = [cv.imread(imname, 1) for imname in c0_images_names]
c1_images = [cv.imread(imname, 1) for imname in c1_images_names]
#change this if stereo calibration not good.
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.001)
#calibration pattern settings
rows = calibration_settings['checkerboard_rows']
columns = calibration_settings['checkerboard_columns']
world_scaling = calibration_settings['checkerboard_box_size_scale']
#coordinates of squares in the checkerboard world space
objp = np.zeros((rows*columns,3), np.float32)
objp[:,:2] = np.mgrid[0:rows,0:columns].T.reshape(-1,2)
objp = world_scaling* objp
#frame dimensions. Frames should be the same size.
width = c0_images[0].shape[1]
height = c0_images[0].shape[0]
#Pixel coordinates of checkerboards
imgpoints_left = [] # 2d points in image plane.
imgpoints_right = []
#coordinates of the checkerboard in checkerboard world space.
objpoints = [] # 3d point in real world space
for frame0, frame1 in zip(c0_images, c1_images):
gray1 = cv.cvtColor(frame0, cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
c_ret1, corners1 = cv.findChessboardCorners(gray1, (rows, columns), None)
c_ret2, corners2 = cv.findChessboardCorners(gray2, (rows, columns), None)
if c_ret1 == True and c_ret2 == True:
corners1 = cv.cornerSubPix(gray1, corners1, (11, 11), (-1, -1), criteria)
corners2 = cv.cornerSubPix(gray2, corners2, (11, 11), (-1, -1), criteria)
p0_c1 = corners1[0,0].astype(np.int32)
p0_c2 = corners2[0,0].astype(np.int32)
cv.putText(frame0, 'O', (p0_c1[0], p0_c1[1]), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
cv.drawChessboardCorners(frame0, (rows,columns), corners1, c_ret1)
cv.imshow('img', frame0)
cv.putText(frame1, 'O', (p0_c2[0], p0_c2[1]), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
cv.drawChessboardCorners(frame1, (rows,columns), corners2, c_ret2)
cv.imshow('img2', frame1)
k = cv.waitKey(0)
if k & 0xFF == ord('s'):
print('skipping')
continue
objpoints.append(objp)
imgpoints_left.append(corners1)
imgpoints_right.append(corners2)
stereocalibration_flags = cv.CALIB_FIX_INTRINSIC
ret, CM1, dist0, CM2, dist1, R, T, E, F = cv.stereoCalibrate(objpoints, imgpoints_left, imgpoints_right, mtx0, dist0,
mtx1, dist1, (width, height), criteria = criteria, flags = stereocalibration_flags)
print('rmse: ', ret)
cv.destroyAllWindows()
return R, T
#Converts Rotation matrix R and Translation vector T into a homogeneous representation matrix
def _make_homogeneous_rep_matrix(R, t):
P = np.zeros((4,4))
P[:3,:3] = R
P[:3, 3] = t.reshape(3)
P[3,3] = 1
return P
# Turn camera calibration data into projection matrix
def get_projection_matrix(cmtx, R, T):
P = cmtx @ _make_homogeneous_rep_matrix(R, T)[:3,:]
return P
# After calibrating, we can see shifted coordinate axes in the video feeds directly
def check_calibration(camera0_name, devnum1, camera0_data, camera1_name, devnum2, camera1_data, _zshift = 50.):
cmtx0 = np.array(camera0_data[0])
dist0 = np.array(camera0_data[1])
R0 = np.array(camera0_data[2])
T0 = np.array(camera0_data[3])
cmtx1 = np.array(camera1_data[0])
dist1 = np.array(camera1_data[1])
R1 = np.array(camera1_data[2])
T1 = np.array(camera1_data[3])
P0 = get_projection_matrix(cmtx0, R0, T0)
P1 = get_projection_matrix(cmtx1, R1, T1)
#define coordinate axes in 3D space. These are just the usual coorindate vectors
coordinate_points = np.array([[0.,0.,0.],
[1.,0.,0.],
[0.,1.,0.],
[0.,0.,1.]])
z_shift = np.array([0.,0.,_zshift]).reshape((1, 3))
#increase the size of the coorindate axes and shift in the z direction
draw_axes_points = 5 * coordinate_points + z_shift
#project 3D points to each camera view manually. This can also be done using cv.projectPoints()
#Note that this uses homogenous coordinate formulation
pixel_points_camera0 = []
pixel_points_camera1 = []
for _p in draw_axes_points:
X = np.array([_p[0], _p[1], _p[2], 1.])
#project to camera0
uv = P0 @ X
uv = np.array([uv[0], uv[1]])/uv[2]
pixel_points_camera0.append(uv)
#project to camera1
uv = P1 @ X
uv = np.array([uv[0], uv[1]])/uv[2]
pixel_points_camera1.append(uv)
#these contain the pixel coorindates in each camera view as: (pxl_x, pxl_y)
pixel_points_camera0 = np.array(pixel_points_camera0)
pixel_points_camera1 = np.array(pixel_points_camera1)
CAMERAS_ON_SAME_CONTROLLER = 2
#create instance for cameras
cam1 = xiapi.Camera(dev_id=devnum1)
cam2 = xiapi.Camera(dev_id=devnum2)
#start communication
print('Opening cameras...')
cam1.open_device_by_SN(calibration_settings[camera0_name])
cam2.open_device_by_SN(calibration_settings[camera1_name])
#set interface data rate
interface_data_rate=cam1.get_limit_bandwidth()
camera_data_rate = int(interface_data_rate / CAMERAS_ON_SAME_CONTROLLER)
#set data rate
cam1.set_limit_bandwidth(camera_data_rate)
cam2.set_limit_bandwidth(camera_data_rate)
#print device serial numbers
print('Camera 1 serial number: ' + str(cam1.get_device_sn()))
print('Camera 2 serial number: ' + str(cam2.get_device_sn()))
# create instance for first connected camera
#cam = xiapi.Camera()
# start communication
#print('Opening first camera...')
#cam.open_device()
# settings
cam1.set_exposure(2000)
cam1.set_gain(10.0)
cam1.set_imgdataformat('XI_MONO8')
cam1.set_width(720)
cam1.set_height(720)
#cam.set_limit_bandwidth_mode()
cam1.set_offsetX(240)
cam1.set_offsetY(152)
cam2.set_exposure(2000)
cam2.set_gain(10.0)
cam2.set_imgdataformat('XI_MONO8')
cam2.set_width(720)
cam2.set_height(720)
#cam.set_limit_bandwidth_mode()
cam2.set_offsetX(240)
cam2.set_offsetY(152)
# create instance of Image to store image data and metadata
img1 = xiapi.Image()
img2 = xiapi.Image()
# start data acquisition
#open the video streams
# cap0 = cv.VideoCapture(calibration_settings[camera0_name])
# cap1 = cv.VideoCapture(calibration_settings[camera1_name])
# #set camera resolutions
# width = calibration_settings['frame_width']
# height = calibration_settings['frame_height']
# cap0.set(3, width)
# cap0.set(4, height)
# cap1.set(3, width)
# cap1.set(4, height)
while True:
print('Starting data acquisition...')
#open video stream and change resolution.
#Note: if unsupported resolution is used, this does NOT raise an error.
#cap = cv.VideoCapture(camera_device_id)
# cap.set(3, width)
# cap.set(4, height)
cam1.get_image(img1)
cam2.get_image(img2)
# create numpy array with data from camera. Dimensions of the array are
# determined by imgdataformat
data1 = img1.get_image_data_numpy()
data2 = img2.get_image_data_numpy()
# ret0, frame0 = cap0.read()
# ret1, frame1 = cap1.read()
# if not ret0 or not ret1:
# print('Video stream not returning frame data')
# quit()
#follow RGB colors to indicate XYZ axes respectively
colors = [(0,0,255), (0,255,0), (255,0,0)]
#draw projections to camera0
origin = tuple(pixel_points_camera0[0].astype(np.int32))
for col, _p in zip(colors, pixel_points_camera0[1:]):
_p = tuple(_p.astype(np.int32))
cv.line(data1, origin, _p, col, 2)
#draw projections to camera1
origin = tuple(pixel_points_camera1[0].astype(np.int32))
for col, _p in zip(colors, pixel_points_camera1[1:]):
_p = tuple(_p.astype(np.int32))
cv.line(data2, origin, _p, col, 2)
cv.imshow('frame0', data1)
cv.imshow('frame1', data2)
k = cv.waitKey(1)
if k == 27: break
cv.destroyAllWindows()
def get_world_space_origin(cmtx, dist, img_path):
frame = cv.imread(img_path, 1)
#calibration pattern settings
rows = calibration_settings['checkerboard_rows']
columns = calibration_settings['checkerboard_columns']
world_scaling = calibration_settings['checkerboard_box_size_scale']
#coordinates of squares in the checkerboard world space
objp = np.zeros((rows*columns,3), np.float32)
objp[:,:2] = np.mgrid[0:rows,0:columns].T.reshape(-1,2)
objp = world_scaling* objp
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, (rows, columns), None)
cv.drawChessboardCorners(frame, (rows,columns), corners, ret)
cv.putText(frame, "If you don't see detected points, try with a different image", (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 1)
cv.imshow('img', frame)
cv.waitKey(0)
ret, rvec, tvec = cv.solvePnP(objp, corners, cmtx, dist)
R, _ = cv.Rodrigues(rvec) #rvec is Rotation matrix in Rodrigues vector form
return R, tvec
def get_cam1_to_world_transforms(cmtx0, dist0, R_W0, T_W0,
cmtx1, dist1, R_01, T_01,
image_path0,
image_path1):
frame0 = cv.imread(image_path0, 1)
frame1 = cv.imread(image_path1, 1)
unitv_points = 5 * np.array([[0,0,0], [1,0,0], [0,1,0], [0,0,1]], dtype = 'float32').reshape((4,1,3))
#axes colors are RGB format to indicate XYZ axes.
colors = [(0,0,255), (0,255,0), (255,0,0)]
#project origin points to frame 0
points, _ = cv.projectPoints(unitv_points, R_W0, T_W0, cmtx0, dist0)
points = points.reshape((4,2)).astype(np.int32)
origin = tuple(points[0])
for col, _p in zip(colors, points[1:]):
_p = tuple(_p.astype(np.int32))
cv.line(frame0, origin, _p, col, 2)
#project origin points to frame1
R_W1 = R_01 @ R_W0
T_W1 = R_01 @ T_W0 + T_01
points, _ = cv.projectPoints(unitv_points, R_W1, T_W1, cmtx1, dist1)
points = points.reshape((4,2)).astype(np.int32)
origin = tuple(points[0])
for col, _p in zip(colors, points[1:]):
_p = tuple(_p.astype(np.int32))
cv.line(frame1, origin, _p, col, 2)
cv.imshow('frame0', frame0)
cv.imshow('frame1', frame1)
cv.waitKey(0)
return R_W1, T_W1
def save_extrinsic_calibration_parameters(R0, T0, R1, T1, prefix = ''):
#create folder if it does not exist
if not os.path.exists('camera_parameters'):
os.mkdir('camera_parameters')
camera0_rot_trans_filename = os.path.join('camera_parameters', prefix + 'camera0_rot_trans.dat')
outf = open(camera0_rot_trans_filename, 'w')
outf.write('R:\n')
for l in R0:
for en in l:
outf.write(str(en) + ' ')
outf.write('\n')
outf.write('T:\n')
for l in T0:
for en in l:
outf.write(str(en) + ' ')
outf.write('\n')
outf.close()
#R1 and T1 are just stereo calibration returned values
camera1_rot_trans_filename = os.path.join('camera_parameters', prefix + 'camera1_rot_trans.dat')
outf = open(camera1_rot_trans_filename, 'w')
outf.write('R:\n')
for l in R1:
for en in l:
outf.write(str(en) + ' ')
outf.write('\n')
outf.write('T:\n')
for l in T1:
for en in l:
outf.write(str(en) + ' ')
outf.write('\n')
outf.close()
return R0, T0, R1, T1
if __name__ == '__main__':
# if len(sys.argv) != 2:
# print('Call with settings filename: "python3 calibrate.py calibration_settings.yaml"')
# quit()
#Open and parse the settings file
parse_calibration_settings_file("calibration_settings.yaml")
"""Step1. Save calibration frames for single cameras"""
save_frames_single_camera('camera0', 0) #save frames for camera0
save_frames_single_camera('camera1', 1) #save frames for camera1
print("cali saved")
"""Step2. Obtain camera intrinsic matrices and save them"""
#camera0 intrinsics
images_prefix = os.path.join('frames', 'camera0*')
cmtx0, dist0 = calibrate_camera_for_intrinsic_parameters(images_prefix)
save_camera_intrinsics(cmtx0, dist0, 'camera0') #this will write cmtx and dist to disk
#camera1 intrinsics
images_prefix = os.path.join('frames', 'camera1*')
cmtx1, dist1 = calibrate_camera_for_intrinsic_parameters(images_prefix)
save_camera_intrinsics(cmtx1, dist1, 'camera1') #this will write cmtx and dist to disk
"""Step3. Save calibration frames for both cameras simultaneously"""
save_frames_two_cams('camera0', 0, 'camera1', 1) #save simultaneous frames
"""Step4. Use paired calibration pattern frames to obtain camera0 to camera1 rotation and translation"""
frames_prefix_c0 = os.path.join('frames_pair', 'camera0*')
frames_prefix_c1 = os.path.join('frames_pair', 'camera1*')
R, T = stereo_calibrate(cmtx0, dist0, cmtx1, dist1, frames_prefix_c0, frames_prefix_c1)
"""Step5. Save calibration data where camera0 defines the world space origin."""
#camera0 rotation and translation is identity matrix and zeros vector
R0 = np.eye(3, dtype=np.float32)
T0 = np.array([0., 0., 0.]).reshape((3, 1))
save_extrinsic_calibration_parameters(R0, T0, R, T) #this will write R and T to disk
R1 = R; T1 = T #to avoid confusion, camera1 R and T are labeled R1 and T1
#check your calibration makes sense
camera0_data = [cmtx0, dist0, R0, T0]
camera1_data = [cmtx1, dist1, R1, T1]
check_calibration('camera0', 0, camera0_data, 'camera1', 1, camera1_data, _zshift = 60.)
"""Optional. Define a different origin point and save the calibration data"""
# #get the world to camera0 rotation and translation
# R_W0, T_W0 = get_world_space_origin(cmtx0, dist0, os.path.join('frames_pair', 'camera0_4.png'))
# #get rotation and translation from world directly to camera1
# R_W1, T_W1 = get_cam1_to_world_transforms(cmtx0, dist0, R_W0, T_W0,
# cmtx1, dist1, R1, T1,
# os.path.join('frames_pair', 'camera0_4.png'),
# os.path.join('frames_pair', 'camera1_4.png'),)
# #save rotation and translation parameters to disk
# save_extrinsic_calibration_parameters(R_W0, T_W0, R_W1, T_W1, prefix = 'world_to_') #this will write R and T to disk