-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_kitti_disp_to_pfm.py
111 lines (83 loc) · 3.25 KB
/
convert_kitti_disp_to_pfm.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
import os
import re
import sys
import imageio
import numpy as np
KITTI_2012_ROOT = r'D:\engineering_thesis_data\Kitti_2012\data_stereo_flow'
KITTI_2015_ROOT = r'D:\engineering_thesis_data\Kitti_2015\data_scene_flow'
def readPFM(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 writePFM(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
print('greyscale')
else:
raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.')
file.write('PF\n' if color else 'Pf\n'.encode())
file.write('%d %d\n'.encode() % (image.shape[1], image.shape[0]))
endian = image.dtype.byteorder
if endian == '<' or endian == '=' and sys.byteorder == 'little':
scale = -scale
file.write('%f\n'.encode() % scale)
image.tofile(file)
def main():
for root, _, files in os.walk(f'{KITTI_2012_ROOT}/training/disp_noc'):
for file in files:
if file.endswith('.png'):
new_name = file[:-4] + '.pfm'
depth_png = imageio.v2.imread(f'{root}\{file}')
depth_png = depth_png.astype(np.float32) / 256.0
writePFM(f'{root}\{new_name}', depth_png)
img, scale = readPFM(f'{root}\{new_name}')
print(img.shape, img.max(), img.min(), img.dtype)
for root, _, files in os.walk(f'{KITTI_2015_ROOT}/training/disp_noc_0'):
for file in files:
if file.endswith('.png'):
new_name = file[:-4] + '.pfm'
depth_png = imageio.v2.imread(f'{root}\{file}')
depth_png = depth_png.astype(np.float32) / 256.0
writePFM(f'{root}\{new_name}', depth_png)
for root, _, files in os.walk(f'{KITTI_2015_ROOT}/training/disp_noc_1'):
for file in files:
if file.endswith('.png'):
new_name = file[:-4] + '.pfm'
depth_png = imageio.v2.imread(f'{root}\{file}')
depth_png = depth_png.astype(np.float32) / 256.0
writePFM(f'{root}\{new_name}', depth_png)
if __name__ == "__main__":
main()