-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesio.py
93 lines (73 loc) · 2.28 KB
/
filesio.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
import numpy as np
import cv2
import glob
num_cams = 36
class CameraParams:
def __init__(self, R, t, K, P):
self.__R = R
# t here is the camera origin point w.r.t world coordinate system
self.__t = t
self.__K = K
self.P = P
def getRotation(self):
return self.__R
def getMotion(self):
return self.__t
def getIntrinsic(self):
return self.__K
class ImageData:
def __init__(self, folder_path, method=1):
self.__data = []
# first get the all the paths of files
files_paths = sorted(glob.glob(folder_path + "/*.jpg"))
for i in files_paths:
data = cv2.imread(i, method)
self.__data.append(data)
def get_data(self):
return self.__data
# return the parameter of camera parameters
def get_cam_param(filename):
fs = cv2.FileStorage(filename, cv2.FILE_STORAGE_READ)
if not fs.isOpened():
print("File not open")
# intrinsic matrix
all_cam_param = []
for i in range(num_cams):
s_index = str(i).zfill(3)
# projection matrix
P = fs.getNode("viff" + s_index + "_matrix").mat()
K, R, t = cv2.decomposeProjectionMatrix(P)[0:3]
t = np.squeeze(t)
np.divide(t, t[3], out=t)
# print(T.shape)
t = t[0:-1]
cam_params = CameraParams(R, t, K, P)
all_cam_param.append(cam_params)
# print(array_T)
fs.release()
# fs = cv2.FileStorage("squirrel.yml", cv2.FILE_STORAGE_WRITE)
# fs.write("Rotations", "[")
# fs.write("Rotations", np.array(array_R))
# fs.write("Rotations", "]")
# fs.write("Motions", np.array(array_T))
# fs.release()
return all_cam_param
# save numpy array
def save_array(path, arr, format="%d"):
with open(path, "w") as out:
out.write("# Shape {0}\n".format(arr.shape))
if arr.ndim <= 2:
np.savetxt(path, arr, fmt=format)
else:
for i in arr:
out.write("# new slice\n")
np.savetxt(out, i, fmt=format)
# read numpy array
def read_array(path, shape, dtype=None):
temp = np.loadtxt(path, dtype=dtype)
if len(shape) <= 2:
return temp
else:
return temp.reshape(shape)
if __name__ == "__main__":
get_cam_param("viff.xml")