forked from TemugeB/python_stereo_camera_calibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstereoCalib.py
72 lines (55 loc) · 2.7 KB
/
stereoCalib.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
import cv2 as cv
import glob
import numpy as np
def stereo_calibrate(mtx1, dist1, mtx2, dist2, frames_folder):
#read the synched frames
images_names = glob.glob(frames_folder)
images_names = sorted(images_names)
c1_images_names = images_names[:len(images_names)//2]
c2_images_names = images_names[len(images_names)//2:]
c1_images = []
c2_images = []
for im1, im2 in zip(c1_images_names, c2_images_names):
_im = cv.imread(im1, 1)
c1_images.append(_im)
_im = cv.imread(im2, 1)
c2_images.append(_im)
#change this if stereo calibration not good.
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.0001)
rows = 5 #number of checkerboard rows.
columns = 8 #number of checkerboard columns.
world_scaling = 1. #change this to the real world square size. Or not.
#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 = c1_images[0].shape[1]
height = c1_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 frame1, frame2 in zip(c1_images, c2_images):
gray1 = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)
c_ret1, corners1 = cv.findChessboardCorners(gray1, (5, 8), None)
c_ret2, corners2 = cv.findChessboardCorners(gray2, (5, 8), 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)
cv.drawChessboardCorners(frame1, (5,8), corners1, c_ret1)
cv.imshow('img', frame1)
cv.drawChessboardCorners(frame2, (5,8), corners2, c_ret2)
cv.imshow('img2', frame2)
k = cv.waitKey(500)
objpoints.append(objp)
imgpoints_left.append(corners1)
imgpoints_right.append(corners2)
stereocalibration_flags = cv.CALIB_FIX_INTRINSIC
ret, CM1, dist1, CM2, dist2, R, T, E, F = cv.stereoCalibrate(objpoints, imgpoints_left, imgpoints_right, mtx1, dist1,
mtx2, dist2, (width, height), criteria = criteria, flags = stereocalibration_flags)
print(ret)
return R, T
R, T = stereo_calibrate(mtx1, dist1, mtx2, dist2, 'synched/*')