-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcalibration_store.py
69 lines (56 loc) · 2.18 KB
/
calibration_store.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
import cv2
def save_coefficients(mtx, dist, path):
""" Save the camera matrix and the distortion coefficients to given path/file. """
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_WRITE)
cv_file.write("K", mtx)
cv_file.write("D", dist)
# note you *release* you don't close() a FileStorage object
cv_file.release()
def save_stereo_coefficients(path, K1, D1, K2, D2, R, T, E, F, R1, R2, P1, P2, Q):
""" Save the stereo coefficients to given path/file. """
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_WRITE)
cv_file.write("K1", K1)
cv_file.write("D1", D1)
cv_file.write("K2", K2)
cv_file.write("D2", D2)
cv_file.write("R", R)
cv_file.write("T", T)
cv_file.write("E", E)
cv_file.write("F", F)
cv_file.write("R1", R1)
cv_file.write("R2", R2)
cv_file.write("P1", P1)
cv_file.write("P2", P2)
cv_file.write("Q", Q)
cv_file.release()
def load_coefficients(path):
""" Loads camera matrix and distortion coefficients. """
# FILE_STORAGE_READ
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_READ)
# note we also have to specify the type to retrieve other wise we only get a
# FileNode object back instead of a matrix
camera_matrix = cv_file.getNode("K").mat()
dist_matrix = cv_file.getNode("D").mat()
cv_file.release()
return [camera_matrix, dist_matrix]
def load_stereo_coefficients(path):
""" Loads stereo matrix coefficients. """
# FILE_STORAGE_READ
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_READ)
# note we also have to specify the type to retrieve other wise we only get a
# FileNode object back instead of a matrix
K1 = cv_file.getNode("K1").mat()
D1 = cv_file.getNode("D1").mat()
K2 = cv_file.getNode("K2").mat()
D2 = cv_file.getNode("D2").mat()
R = cv_file.getNode("R").mat()
T = cv_file.getNode("T").mat()
E = cv_file.getNode("E").mat()
F = cv_file.getNode("F").mat()
R1 = cv_file.getNode("R1").mat()
R2 = cv_file.getNode("R2").mat()
P1 = cv_file.getNode("P1").mat()
P2 = cv_file.getNode("P2").mat()
Q = cv_file.getNode("Q").mat()
cv_file.release()
return [K1, D1, K2, D2, R, T, E, F, R1, R2, P1, P2, Q]