-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfile_io.py
executable file
·321 lines (243 loc) · 9.55 KB
/
file_io.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
# A reimplemented version in public environments by Xiao Fu
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import re
from PIL import Image
import sys
import torch
import cv2
import h5py
from utils.depth2normal import *
import json
def read_img(filename):
img = np.array(Image.open(filename))
return img
def read_hdf5(filename):
with h5py.File(filename, "r") as f: file = f["dataset"][:]
return file
def distance2depth(npyDistance:np.ndarray, fitFocal:float):
'''
fitFocal is one of fx or fy in K
'''
intHeight, intWidth = npyDistance.shape
npyImageplaneX = np.linspace((-0.5 * intWidth) + 0.5, (0.5 * intWidth) - 0.5, intWidth).reshape(1, intWidth).repeat(intHeight, 0).astype(np.float32)[:, :, None]
npyImageplaneY = np.linspace((-0.5 * intHeight) + 0.5, (0.5 * intHeight) - 0.5, intHeight).reshape(intHeight, 1).repeat(intWidth, 1).astype(np.float32)[:, :, None]
npyImageplaneZ = np.full([intHeight, intWidth, 1], fitFocal, np.float32)
npyImageplane = np.concatenate([npyImageplaneX, npyImageplaneY, npyImageplaneZ], 2)
npyDepth = npyDistance / np.linalg.norm(npyImageplane, 2, 2) * fitFocal
return npyDepth
def align_normal(normal, depth, K, H, W):
'''
Orientation of surface normals in hypersim is not always consistent
see https://github.com/apple/ml-hypersim/issues/26
'''
# inv K
K = np.array([[K[0], 0 ,K[2]],
[0, K[1], K[3]],
[0, 0, 1]])
inv_K = np.linalg.inv(K)
# reprojection depth to camera points
xy = creat_uv_mesh(H, W)
points = np.matmul(inv_K[:3, :3], xy).reshape(3, H, W)
points = depth * points
points = points.transpose((1,2,0))
# align normal
orient_mask = np.sum(normal * points, axis=2) > 0
normal[orient_mask] *= -1
return normal
def creat_uv_mesh(H, W):
y, x = np.meshgrid(np.arange(0, H, dtype=np.float), np.arange(0, W, dtype=np.float), indexing='ij')
meshgrid = np.stack((x,y))
ones = np.ones((1,H*W), dtype=np.float)
xy = meshgrid.reshape(2, -1)
return np.concatenate([xy, ones], axis=0)
def read_depth_normal_hypersim(depth_path, normal_path, K, metric_scale):
depth = read_hdf5(depth_path).astype(np.float32)
depth[depth>60000] = 0
depth = depth / metric_scale
normal = read_hdf5(normal_path).astype(np.float32)
H, W = normal.shape[:2]
# convert (x right, y up, z backward) to conventional (x right, y down, z forward)
normal[:,:,1:] *= -1
normal = align_normal(normal, depth, K, H, W)
normal /= (np.linalg.norm(normal, ord=2, axis=2, keepdims=True) + 1e-5)
return depth, normal
def read_depth_normal_replica(depth_path, normal_path, K, metric_scale):
depth = cv2.imread(depth_path, -1)
depth[depth>60000] = 0
depth = depth / metric_scale
with open(normal_path, 'rb') as f:
normal = Image.open(f)
normal = np.array(normal.convert(normal.mode), dtype=np.uint8)
invalid_mask = np.all(normal == 128, axis=2)
normal = normal.astype(np.float64) / 255.0 * 2 - 1
normal[invalid_mask, :] = 0
normal /= (np.linalg.norm(normal, ord=2, axis=2, keepdims=True) + 1e-5)
depth[invalid_mask] = 10.
normal[invalid_mask] = np.array([0., 0., -1.])
return depth, normal, invalid_mask
def read_depth_normal_virtual_kitti(depth_path, normal_path, K, metric_scale):
depth = cv2.imread(depth_path, -1)
depth[depth>(150 * metric_scale)] = 0
depth = depth / metric_scale
far_plane = 80
invalid_mask = (depth == 0.)
depth[invalid_mask] = far_plane
depth[depth>far_plane] = far_plane
# focal_length = K[:2]
# depth2normal = surface_normal_from_depth(torch.from_numpy(depth[None,None]).to(torch.float32).cuda(), torch.Tensor(focal_length).cuda())
# depth2normal = depth2normal.cpu().numpy()[0].transpose(1,2,0)
# depth2normal /= (np.linalg.norm(depth2normal, axis=-1, ord=2, keepdims=True) + 1e-5)
normal = cv2.imread(normal_path, cv2.IMREAD_UNCHANGED)
normal = normal.astype(np.float64) / 255.0 * 2 - 1
normal = normal[:,:,::-1]
normal = normal / (np.linalg.norm(normal, ord=2, axis=2, keepdims=True) + 1e-5)
return depth, normal, invalid_mask
def read_depth_normal_simulation_disparity(depth_path, normal_path, K, metric_scale):
depth = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
depth = depth / metric_scale
# far-range clip
far_plane = 510
mask = depth>far_plane
depth[mask] = far_plane
normal = cv2.imread(normal_path, cv2.IMREAD_UNCHANGED)
normal = normal.astype(np.float64) / 255.0 * 2 - 1
normal = normal[:,:,::-1]
normal = normal / (np.linalg.norm(normal, ord=2, axis=2, keepdims=True) + 1e-5)
normal[mask, :] = np.array([0., 0., -1.])
return depth, normal
def read_depth_normal_kenburns(depth_path, normal_path, K, metric_scale):
depth = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
depth[depth>50000] = 0.
depth = depth / metric_scale
invalid_mask = depth == 0.
# far-range clip
far_plane = 80
depth[invalid_mask] = far_plane
depth[depth>far_plane] = far_plane
normal = cv2.imread(normal_path, cv2.IMREAD_UNCHANGED)
normal[~invalid_mask] /= (np.linalg.norm(normal, axis=-1, ord=2)[~invalid_mask][...,None] + 1e-5)
normal[:,:,1] *= -1
normal[invalid_mask, :] = np.array([0., 0., -1.])
return depth, normal, invalid_mask
def depth2disp(left_depth_path):
left_depth = np.array(Image.open(left_depth_path))/(10000)
focal_length = 768.16058349609375
baseline = 0.06
left_disp = focal_length*baseline /left_depth
return left_disp
def unity2blender(normal):
normal_clone = normal.copy()
normal_clone[...,0] = -normal[...,-1]
normal_clone[...,1] = -normal[...,0]
normal_clone[...,2] = normal[...,1]
return normal_clone
def blender2midas(img):
'''Blender: rub
midas: lub
'''
img[...,0] = -img[...,0]
img[...,1] = -img[...,1]
img[...,-1] = -img[...,-1]
return img
def read_camera_matrix_single(json_file):
with open(json_file, 'r', encoding='utf8') as reader:
json_content = json.load(reader)
# NOTE that different from unity2blender experiments.
camera_matrix = np.eye(4)
camera_matrix[:3, 0] = np.array(json_content['x'])
camera_matrix[:3, 1] = -np.array(json_content['y'])
camera_matrix[:3, 2] = -np.array(json_content['z'])
camera_matrix[:3, 3] = np.array(json_content['origin'])
'''
camera_matrix = np.eye(4)
camera_matrix[:3, 0] = np.array(json_content['x'])
camera_matrix[:3, 1] = np.array(json_content['y'])
camera_matrix[:3, 2] = np.array(json_content['z'])
camera_matrix[:3, 3] = np.array(json_content['origin'])
# print(camera_matrix)
'''
return camera_matrix
def depth2disp_cv(left_depth_path):
left_depth = cv2.imread(left_depth_path, cv2.IMREAD_UNCHANGED)/ 10000
focal_length = 768.16058349609375
baseline = 0.06
left_disp = focal_length*baseline /left_depth
return left_disp
def read_disp(filename, subset=False):
# Scene Flow dataset
if filename.endswith('pfm'):
# For finalpass and cleanpass, gt disparity is positive, subset is negative
disp = np.ascontiguousarray(_read_pfm(filename)[0])
if subset:
disp = -disp
# KITTI
elif filename.endswith('png'):
disp = _read_kitti_disp(filename)
elif filename.endswith('npy'):
disp = np.load(filename)
else:
raise Exception('Invalid disparity file format!')
return disp # [H, W]
def _read_pfm(file):
file = open(file, 'rb')
color = None
width = None
height = None
scale = None
endian = None
header = file.readline().rstrip()
if header.decode("ascii") == 'PF':
color = True
elif header.decode("ascii") == 'Pf':
color = False
else:
raise Exception('Not a PFM file.')
dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline().decode("ascii"))
if dim_match:
width, height = list(map(int, dim_match.groups()))
else:
raise Exception('Malformed PFM header.')
scale = float(file.readline().decode("ascii").rstrip())
if scale < 0: # little-endian
endian = '<'
scale = -scale
else:
endian = '>' # big-endian
data = np.fromfile(file, endian + 'f')
shape = (height, width, 3) if color else (height, width)
data = np.reshape(data, shape)
data = np.flipud(data)
return data, scale
def write_pfm(file, image, scale=1):
file = open(file, 'wb')
color = None
if image.dtype.name != 'float32':
raise Exception('Image dtype must be float32.')
image = np.flipud(image)
if len(image.shape) == 3 and image.shape[2] == 3: # color image
color = True
elif len(image.shape) == 2 or len(
image.shape) == 3 and image.shape[2] == 1: # greyscale
color = False
else:
raise Exception(
'Image must have H x W x 3, H x W x 1 or H x W dimensions.')
file.write(b'PF\n' if color else b'Pf\n')
file.write(b'%d %d\n' % (image.shape[1], image.shape[0]))
endian = image.dtype.byteorder
if endian == '<' or endian == '=' and sys.byteorder == 'little':
scale = -scale
file.write(b'%f\n' % scale)
image.tofile(file)
def _read_kitti_disp(filename):
depth = np.array(Image.open(filename))
depth = depth.astype(np.float32) / 256.
return depth
def read_occlusion_mid(filename):
img = Image.open(filename)
img_np = np.array(img)
valid_mask_combine = (img_np<=128).astype(np.float)
return valid_mask_combine