-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.py
198 lines (157 loc) · 7.89 KB
/
helper_functions.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
import numpy as np
import cv2
from struct import *
def _read_bin(bin_file):
with open(bin_file,'rb') as f:
float_size = 4
cor = f.read(float_size*3)
cors = unpack('fff',cor) # origin in world coordinates
cam = f.read(float_size*16)
cams = unpack('ffffffffffffffff', cam) # camera pose
uint_size = 4
vox = f.read()
numC = int(len(vox)/uint_size)
checkVoxValIter = unpack('I'*numC, vox)
checkVoxVal = checkVoxValIter[0::2]
checkVoxIter = checkVoxValIter[1::2]
voxels = [i for (val, repeat) in zip(checkVoxVal,checkVoxIter) for i in np.tile(val, repeat)]
voxels = np.array(voxels, dtype=np.int16)
return cors, cams, voxels
def _read_mat(mat_file, return_as_flat=True, return_as_float=False):
with h5py.File(mat_file, 'r') as f:
data = f['depth_mat']
amodal_list = []
for i in data:
depth = np.transpose(i, (1,0))
depth = np.ceil(depth*1000).astype(np.uint16)
if return_as_flat == True:
depth = np.reshape(depth, (-1,))
if return_as_float == True:
depth = depth.astype(np.float32)/1000
amodal_list.append(depth)
f.close()
amodal_list = np.array(amodal_list)
return amodal_list
def _read_bitshift(depth_path, return_as_flat=False, return_as_float=False):
depth = cv2.imread(depth_path, -1)
lower_depth = depth >> 3
higher_depth = (depth % 8) << 13
real_depth = (lower_depth | higher_depth)
if return_as_flat:
real_depth = np.reshape(real_depth, (-1,))
if return_as_float:
real_depth = real_depth.astype(np.float32)/1000
return real_depth
def _2Dto3D(bin_file, depth_img, return_as='3D', mapping_as='pcd'):
# parameters
img_height, img_width = (480, 640)
img_scale = 1.0
vox_unit = 0.02
vox_size = (240,144,240)
cam_K = np.array([518.8579 / img_scale, 0., img_width / (2 * img_scale),
0., 518.8579 / img_scale, img_height / (2 * img_scale),
0., 0., 1.], dtype=np.float32)
# cam_K = np.array([518.8579, 0, 325.58,
# 0, 519.4696, 253.74,
# 0, 0, 1], dtype=np.float32)
vox_origin, cam_pose = _get_bin_info(bin_file)
depth_mapping = np.ones((img_height, img_width), dtype=np.int32) * -1
mask = np.zeros_like(depth_img, dtype=np.bool_)
img_y = np.repeat(np.expand_dims(np.arange(depth_img.shape[0]), axis=1), depth_img.shape[1], axis=1)
img_x = np.repeat(np.expand_dims(np.arange(depth_img.shape[1]), axis=0), depth_img.shape[0], axis=0)
point_cam_x = (img_x - cam_K[2]) * depth_img / cam_K[0]
point_cam_y = (img_y - cam_K[5]) * depth_img / cam_K[4]
point_cam_z = depth_img
point_base_x = cam_pose[0 * 4 + 0] * point_cam_x + cam_pose[0 * 4 + 1] * point_cam_y + cam_pose[0 * 4 + 2] * point_cam_z;
point_base_y = cam_pose[1 * 4 + 0] * point_cam_x + cam_pose[1 * 4 + 1] * point_cam_y + cam_pose[1 * 4 + 2] * point_cam_z;
point_base_z = cam_pose[2 * 4 + 0] * point_cam_x + cam_pose[2 * 4 + 1] * point_cam_y + cam_pose[2 * 4 + 2] * point_cam_z;
point_base_x = point_base_x + cam_pose[0 * 4 + 3];
point_base_y = point_base_y + cam_pose[1 * 4 + 3];
point_base_z = point_base_z + cam_pose[2 * 4 + 3];
if mapping_as == 'pcd':
z = (point_base_x - vox_origin[0]) / vox_unit
x = (point_base_y - vox_origin[1]) / vox_unit
y = (point_base_z - vox_origin[2]) / vox_unit
elif mapping_as == 'voxel':
z = np.floor((point_base_x - vox_origin[0]) / vox_unit)
x = np.floor((point_base_y - vox_origin[1]) / vox_unit)
y = np.floor((point_base_z - vox_origin[2]) / vox_unit)
z = z.astype(np.int32)
x = x.astype(np.int32)
y = y.astype(np.int32)
for i in range((480*640)):
pix_x, pix_y = i // 640, i % 640
if x[pix_x,pix_y] >= 0 and x[pix_x,pix_y] < vox_size[0] \
and y[pix_x,pix_y] >= 0 and y[pix_x,pix_y] < vox_size[1] \
and z[pix_x,pix_y] >= 0 and z[pix_x,pix_y] < vox_size[2]:
vox_idx = z[pix_x,pix_y] * vox_size[0] * vox_size[1] \
+ y[pix_x,pix_y] * vox_size[0] \
+ x[pix_x,pix_y]
depth_mapping[pix_x,pix_y] = vox_idx
mask[pix_x,pix_y] = 1
if return_as == '1D':
return np.reshape(depth_mapping, (-1,))
elif return_as == '2D':
return depth_mapping
elif return_as == '3D':
mask = np.expand_dims(mask, axis=0)
zxy = np.stack((z,x,y), axis=0)
zxy = zxy * mask
return zxy, mask
def _3Dto2D(bin_path, pcd):
# parameters
img_height, img_width = (480, 640)
img_scale = 1.0
vox_unit = 0.02
vox_size = (240,144,240)
cam_K = np.array([518.8579 / img_scale, 0., img_width / (2 * img_scale),
0., 518.8579 / img_scale, img_height / (2 * img_scale),
0., 0., 1.], dtype=np.float32)
# pcd = np.asarray(pcd.points)
vox_origin, cam_pose = _get_bin_info(bin_path)
point_base_x = pcd[:,0] * vox_unit + vox_origin[0]
point_base_y = pcd[:,1] * vox_unit + vox_origin[1]
point_base_z = pcd[:,2] * vox_unit + vox_origin[2]
point_base_x = point_base_x - cam_pose[0 * 4 + 3]
point_base_y = point_base_y - cam_pose[1 * 4 + 3]
point_base_z = point_base_z - cam_pose[2 * 4 + 3]
point_cam_x = cam_pose[0 * 4 + 0] * point_base_x + cam_pose[1 * 4 + 0] * point_base_y + cam_pose[2 * 4 + 0] * point_base_z
point_cam_y = cam_pose[0 * 4 + 1] * point_base_x + cam_pose[1 * 4 + 1] * point_base_y + cam_pose[2 * 4 + 1] * point_base_z
point_cam_z = cam_pose[0 * 4 + 2] * point_base_x + cam_pose[1 * 4 + 2] * point_base_y + cam_pose[2 * 4 + 2] * point_base_z
pixel_x = cam_K[0] * (point_cam_x / point_cam_z) + cam_K[2]
pixel_y = cam_K[4] * (point_cam_y / point_cam_z) + cam_K[5]
pixel_x = np.round_(pixel_x).astype(np.int16)
pixel_y = np.round_(pixel_y).astype(np.int16)
return pixel_x, pixel_y
def _gen_normal(depth_path):
def _details_and_fov(img_height, img_width, img_scale, vox_scale):
vox_details = np.array([0.02 * vox_scale, 0.24], np.float32)
camera_fov = np.array([518.8579 / img_scale, 0., img_width / (2 * img_scale),
0., 518.8579 / img_scale, img_height / (2 * img_scale),
0., 0., 1.], dtype=np.float32)
return vox_details, camera_fov
def _diff_vec(img, axis=0):
img_diff = np.diff(img, 1, axis)
img_diff_l = img_diff[1:, :] if axis == 0 else img_diff[:, 1:]
img_diff_h = img_diff[:-1, :] if axis == 0 else img_diff[:, :-1]
img_diff = img_diff_l + img_diff_h
pad_tuple = ((1, 1), (0, 0), (0, 0)) if axis == 0 else ((0, 0), (1, 1), (0, 0))
padded = np.lib.pad(img_diff, pad_tuple, 'edge')
return padded
depth = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
lower_depth = depth >> 3
higher_depth = (depth % 8) << 13
real_depth = (lower_depth | higher_depth).astype(np.float32) / 1000
_, fov = _details_and_fov(*real_depth.shape, 1, 1)
img_x = np.repeat(np.expand_dims(np.arange(real_depth.shape[0]), axis=1), real_depth.shape[1], axis=1)
img_y = np.repeat(np.expand_dims(np.arange(real_depth.shape[1]), axis=0), real_depth.shape[0], axis=0)
point_cam_x = (img_x - fov[2]) * real_depth / fov[0]
point_cam_y = (img_y - fov[5]) * real_depth / fov[4]
points = np.stack([point_cam_x, point_cam_y, real_depth], axis=2)
diff_y = _diff_vec(points, axis=0)
diff_x = _diff_vec(points, axis=1)
normal = np.cross(diff_x, diff_y)
normal_factor = np.expand_dims(np.linalg.norm(normal, axis=2), axis=-1)
normal = np.where((normal_factor == 0.) | np.isnan(normal_factor), (0, 0, 0), normal / normal_factor)
normal = (np.clip((normal + 1) / 2, 0, 1) * 65535).astype(np.uint16)
return normal