-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdepth_to_disp.py
104 lines (77 loc) · 2.71 KB
/
depth_to_disp.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
import cv2
import json
from os.path import join, splitext, split
from os import listdir
import tifffile as tiff
import numpy as np
import matplotlib.pyplot as plt
def tiff_reader(tiff_file):
raw = tiff.imread(tiff_file)
img_l = raw[:1024,:,:]
img_r = raw[1024:,:,:]
return img_l, img_r
def coor_to_disp(coor, Q):
# parse Q
Q = np.array(Q)
fl = Q[2,3]
bl = 1 / Q[3,2]
cx = -Q[0,3]
cy = -Q[1,3]
print('fl: ', fl, 'bl: ', bl, 'cx: ', cx, 'cy: ', cy)
size = coor.shape[:2] # size[0] = 1024 size[1] = 1280
X = coor[:,:,0]
Y = coor[:,:,1]
Z = coor[:,:,2]
#disp = np.zeros(size)
all_disp = np.zeros((size[0],size[1],2))
disp = np.zeros(size)
for i in range(size[0]):
for j in range(size[1]):
x = X[i,j]
y = Y[i,j]
z = Z[i,j]
if (z != 0):
d = fl * bl / z
p_x = fl * x / z + cx
p_y = fl * y / z + cy
if (p_x < size[1] and p_y < size[0]):
all_disp[int(p_y), int(p_x),0] += d
all_disp[int(p_y), int(p_x),1] += 1
"""
if (p_x <= size[1] and p_y <= size[0]):
print('px: ', p_x, 'py: ', p_y, 'disp: ', d)
disp[int(p_y), int(p_x)] = d
"""
for i in range(size[0]):
for j in range(size[1]):
if all_disp[i,j,1] != 0:
disp[i,j] = all_disp[i,j,0] / all_disp[i,j,1]
#print(disp.max(), disp.min())
#plt.imshow(disp)
#plt.show()
return disp
def read_Q(reprojection_file):
with open(reprojection_file) as json_file:
data = json.load(json_file)
Q = data['reprojection-matrix']
return Q
def depth_to_disparity(path):
rootpath = path
keyframe_list = ['keyframe_4']
for kf in keyframe_list:
reprojection_filepath = join(rootpath, kf) + '/data/reprojection_data'
coor_filepath = join(rootpath,kf) + '/data/scene_points'
disp_filepath = join(rootpath,kf) + '/data/disparity'
frame_list = listdir(reprojection_filepath)
for i in range(len(frame_list)):
reprojection_data = reprojection_filepath + '/frame_data%.6d.json' % i
coor_data = coor_filepath + '/scene_points%.6d.tiff' % i
disp_data = disp_filepath + '/frame_data%.6d.tiff' % i
print('Saving disparity to:', disp_data)
Q = read_Q(reprojection_data)
img_l, img_r = tiff_reader(coor_data)
disp = coor_to_disp(img_l, Q)
cv2.imwrite(disp_data, disp)
if __name__ == '__main__':
path = '/media/eikoloki/TOSHIBA EXT/MICCAI_SCARED/dataset2'
depth_to_disparity(path)