Skip to content

Commit f5eb3c4

Browse files
committed
Added stereo evaluation script
1 parent 24bdb8a commit f5eb3c4

File tree

2 files changed

+983
-0
lines changed

2 files changed

+983
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import numpy as np
2+
import re
3+
import sys
4+
5+
def read_pfm(file):
6+
file = open(file, "rb")
7+
8+
color = None
9+
width = None
10+
height = None
11+
scale = None
12+
endian = None
13+
14+
header = file.readline().rstrip()
15+
if header.decode("ascii") == "PF":
16+
color = True
17+
elif header.decode("ascii") == "Pf":
18+
color = False
19+
else:
20+
raise Exception("Not a PFM file.")
21+
22+
dim_match = re.search(r"(\d+)\s(\d+)", file.readline().decode("ascii"))
23+
if dim_match:
24+
width, height = map(int, dim_match.groups())
25+
else:
26+
raise Exception("Malformed PFM header.")
27+
28+
scale = float(file.readline().rstrip())
29+
if scale < 0: # little-endian
30+
endian = "<"
31+
scale = -scale
32+
else:
33+
endian = ">" # big-endian
34+
35+
data = np.fromfile(file, endian + "f")
36+
shape = (height, width, 3) if color else (height, width)
37+
return np.flip(np.reshape(data, shape), axis=0), scale
38+
39+
def write_pfm(file, image, scale = 1. / 255):
40+
image = np.flip(image, axis=0)
41+
file = open(file, "wb")
42+
43+
color = None
44+
45+
if image.dtype.name != "float32":
46+
raise Exception("Image dtype must be float32.")
47+
48+
if len(image.shape) == 3 and image.shape[2] == 3: # color image
49+
color = True
50+
elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # greyscale
51+
color = False
52+
else:
53+
raise Exception("Image must have H x W x 3, H x W x 1 or H x W dimensions.")
54+
55+
file.write(b"PF\n" if color else b"Pf\n")
56+
file.write(b"%d %d\n" % (image.shape[1], image.shape[0]))
57+
58+
endian = image.dtype.byteorder
59+
60+
if endian == "<" or endian == "=" and sys.byteorder == "little":
61+
scale = -scale
62+
63+
file.write(b"%f\n" % scale)
64+
65+
image.tofile(file)

0 commit comments

Comments
 (0)